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.