gpt4 book ai didi

novacode-docx - Novacode DocX 同一文档中的不同页面方向

转载 作者:行者123 更新时间:2023-12-04 17:46:32 31 4
gpt4 key购买 nike

使用以下代码,我尝试创建一个文档,其中第 2 页和第 3 页为横向,而其他页为纵向。所有尺寸都应为 8.5"x 11"。

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (DocX document = DocX.Create(ms))
{
document.PageLayout.Orientation = Novacode.Orientation.Portrait;
document.PageWidth = 816F;
document.PageHeight = 1056F;

document.MarginTop = 50F;
document.MarginRight = 50F;
document.MarginBottom = 75F;
document.MarginLeft = 50F;

document.AddHeaders();
document.AddFooters();
document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = false;

Header header_first = document.Headers.first;
Header header_main = document.Headers.odd;
Footer footer_main = document.Footers.odd;

Novacode.Table tHeaderFirst = header_first.InsertTable(2, 1);
tHeaderFirst.Design = TableDesign.None;
tHeaderFirst.AutoFit = AutoFit.Window;

Paragraph pHeaderFirst = header_first.Tables[0].Rows[0].Cells[0].Paragraphs[0];
Novacode.Image imgHeaderFirst = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-front.jpg"));
pHeaderFirst.InsertPicture(imgHeaderFirst.CreatePicture());

Novacode.Table tHeaderMain = header_main.InsertTable(2, 1);
tHeaderMain.Design = TableDesign.None;
tHeaderMain.AutoFit = AutoFit.Window;

Paragraph pHeader = header_main.Tables[0].Rows[0].Cells[0].Paragraphs[0];
Novacode.Image imgHeader = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-internal-portrait.jpg"));
pHeader.InsertPicture(imgHeader.CreatePicture());

Paragraph pFooter = footer_main.Paragraphs.First();
pFooter.Alignment = Alignment.center;
pFooter.Append("Page ");
pFooter.AppendPageNumber(PageNumberFormat.normal);
pFooter.Append("/");
pFooter.AppendPageCount(PageNumberFormat.normal);

Paragraph p1 = document.InsertParagraph("test");
p1.InsertPageBreakAfterSelf();
document.InsertSection(true);
document.PageLayout.Orientation = Novacode.Orientation.Landscape;
//document.PageWidth = 1056F;
//document.PageHeight = 816F;

Paragraph p2 = document.InsertParagraph("test");
p2.InsertPageBreakAfterSelf();

Paragraph p3 = document.InsertParagraph("test");
p3.InsertPageBreakAfterSelf();

document.InsertSection(true);
document.PageLayout.Orientation = Novacode.Orientation.Portrait;
//document.PageWidth = 816F;
//document.PageHeight = 1056F;

Paragraph p4 = document.InsertParagraph("test");
p4.InsertPageBreakAfterSelf();

Paragraph p5 = document.InsertParagraph("test");
p5.InsertPageBreakAfterSelf();

Paragraph p6 = document.InsertParagraph("test");
p6.InsertPageBreakAfterSelf();

document.Save();
}
}

我有几个问题。

首先,如果我在开始时设置了一次方向,那么所有页面的大小都是正确的,但是一旦我将第二个和第三个更改添加到 PageLayout.Orientation 中,突然我所有的页面都是错误的大小。

其次,插入部分对我的页眉和页脚做了奇怪的事情。第三部分的第一页就像文档的第一页一样,并采用第一页的页眉和页脚。

最后,向 PageLayout.Orientation 添加第二个和第三个更改实际上并没有改变页面方向。正如您在注释掉的代码中看到的那样,我还尝试在更改布局后设置新的页面高度和宽度。这样做会使我的页面恢复到正确的大小,但绝不会影响方向。

我错过了什么?任何帮助将不胜感激。

最佳答案

最后!我已经制定了一个可用的解决方案,我将其放在此处,希望对其他人有所帮助。

document.PageLayout.Orientation = Novacode.Orientation.Portrait;
document.PageWidth = 816F;
document.PageHeight = 1056F;
document.MarginTop = 50F;
document.MarginRight = 50F;
document.MarginBottom = 75F;
document.MarginLeft = 50F;

document.AddHeaders();
document.AddFooters();
document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = false;

Header header_first = document.Headers.first;
Header header_main = document.Headers.odd;
Footer footer_main = document.Footers.odd;

Paragraph pHeaderFirst = header_first.Paragraphs.First();
Novacode.Image imgHeaderFirst = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-front.jpg"));
pHeaderFirst.Alignment = Alignment.center;
pHeaderFirst.SpacingAfter(25);
pHeaderFirst.AppendPicture(imgHeaderFirst.CreatePicture());

Paragraph pHeader = header_main.Paragraphs.First();
Novacode.Image imgHeader = document.AddImage(ctx.Server.MapPath("~/proposal-assets/header-internal-portrait.jpg"));
pHeader.Alignment = Alignment.center;
pHeader.SpacingAfter(25);
pHeader.AppendPicture(imgHeader.CreatePicture());

Paragraph pFooter = footer_main.Paragraphs.First();
pFooter.Alignment = Alignment.center;
pFooter.Append("Page ");
pFooter.AppendPageNumber(PageNumberFormat.normal);
pFooter.Append("/");
pFooter.AppendPageCount(PageNumberFormat.normal);

Paragraph p1 = document.InsertParagraph("test");

System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
DocX document2 = DocX.Create(ms2);
document2.PageLayout.Orientation = Novacode.Orientation.Landscape;
document2.PageWidth = 1056F;
document2.PageHeight = 816F;
document2.MarginTop = 50F;
document2.MarginRight = 50F;
document2.MarginBottom = 75F;
document2.MarginLeft = 50F;
Paragraph p2 = document2.InsertParagraph("test --- doc 2");
p2.InsertPageBreakAfterSelf();
Paragraph p3 = document2.InsertParagraph("test --- doc 2");
document2.Save();

document.InsertSection();
document.InsertDocument(document2);


System.IO.MemoryStream ms3 = new System.IO.MemoryStream();
DocX document3 = DocX.Create(ms3);
document3.PageLayout.Orientation = Novacode.Orientation.Portrait;
document3.PageWidth = 816F;
document3.PageHeight = 1056F;
document3.MarginTop = 50F;
document3.MarginRight = 50F;
document3.MarginBottom = 75F;
document3.MarginLeft = 50F;
Paragraph p4 = document3.InsertParagraph("test");
p4.InsertPageBreakAfterSelf();
Paragraph p5 = document3.InsertParagraph("test");
p5.InsertPageBreakAfterSelf();
Paragraph p6 = document3.InsertParagraph("test");
document3.Save();

document.InsertSection();
document.InsertDocument(document3);

document.Save();

将不同的部分创建为单独的文档并将它们插入到主文档中,效果很好,解决了我所有的问题。

关于novacode-docx - Novacode DocX 同一文档中的不同页面方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176221/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com