Using the following code, I'm trying to create a document in which pages 2 and 3 are landscape while the others are portrait. All should be 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();
}
}
I'm having several issues with this.
First, if I set the orientation once at the beginning, all the pages come out the correct size, but once I add the 2nd and 3rd changes to PageLayout.Orientation, suddenly ALL my pages are the wrong size.
Second, inserting the sections does weird things with my headers and footers. The first page of the 3rd section acts like it's the first page of the document and takes the first page header and footer.
Finally, adding the 2nd and 3rd changes to PageLayout.Orientation doesn't actually change the page orientations. As you can see in the commented out code, I've also tried setting new page heights and widths after changing the layout. Doing so makes my pages go back to the correct size, but in no way affects the orientation.
What am I missing? Any help would be greatly appreciated.
Finally! I've worked out a usable solution which I'll place here in hopes it'll help someone else.
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();
Creating the different sections as separate documents and inserting them into the main document worked well and solved all my problems.
来源:https://stackoverflow.com/questions/33176221/novacode-docx-different-page-orientations-within-the-same-document