DocX icon indicating copy to clipboard operation
DocX copied to clipboard

Page breaks and layout size

Open highfield opened this issue 8 years ago • 4 comments

I wish to add a page break in my document, and to achieve that I'm using the "InsertSectionPageBreak" method. That actually breaks the flow to the next page, but the size of the page layout of the new page isn't the same as the first one. In other words, the very first page is an A4-size layout, and if I do NOT add page breaks, any further page will follow that size. If I add a page break on the first page, the 2nd and the 3rd are Letter-sized, whereas the 4th is an A4 back. Tested with the NuGet release 1.0.0.19. Any clue? Very nice job, BTW!

highfield avatar Mar 22 '16 14:03 highfield

You may need to change the layout of the page after new section break. There should be an option in code to do that.

PrzemyslawKlys avatar May 09 '16 08:05 PrzemyslawKlys

@PrzemyslawKlys This doesn't help. In the following code, the first and last pages are A4, but the middles ones will be Letter.

byte[] bytes;

using (var wordStream = new MemoryStream())
using (var wordDoc = DocX.Create(wordStream))
{
    wordDoc.PageLayout.Orientation = Orientation.Portrait;
    wordDoc.InsertParagraph("Page 1: A4");

    wordDoc.InsertSectionPageBreak();
    wordDoc.PageLayout.Orientation = Orientation.Portrait;
    wordDoc.InsertParagraph("Page 2: Letter");

    wordDoc.InsertSectionPageBreak();
    wordDoc.PageLayout.Orientation = Orientation.Portrait;
    wordDoc.InsertParagraph("Page N: A4");

    wordDoc.Save();

    bytes = wordStream.ToArray();
}

return bytes;

capesean avatar Dec 22 '16 09:12 capesean

Just encountered this issue myself. The solution that worked for me, is that I replaced the following code:

wordDoc.InsertSectionPageBreak();

With:

wordDoc.InsertParagraph().InsertPageBreakAfterSelf();

xrisdoc avatar Jun 12 '17 09:06 xrisdoc

Just encountered this issue myself. The solution that worked for me, is that I replaced the following code:

wordDoc.InsertSectionPageBreak();

With:

wordDoc.InsertParagraph().InsertPageBreakAfterSelf();

Works like a charm

narasimhakashyap20 avatar Sep 28 '20 09:09 narasimhakashyap20