Shawn
Shawn

Reputation: 228

DocX C# library changing page format on InsertSectionPageBreak

I am working with DocX a library for creating Microsoft .docx files inside c#. https://docx.codeplex.com/

I am loading an preexisting file into the program and then adding content. It was the easiest way to get a pre defined header. I noticed that if I use InsertSectionPageBreak the page format of all pages created in this way will change from A4 to letter.

I tried with a fresh template file (empty file) and the results were the same.

var doc = DocX.Load(fileName);
doc.InsertSectionPageBreak();
doc.InsertSectionPageBreak();
doc.InsertSectionPageBreak();
doc.SaveAs(path + filenaming + ".docx");

Everything else is working fine. file creation, saving content.

The default value for A4 is doc.PageWidth = 800; but its not working on sectionPageBreak pages.

Upvotes: 1

Views: 1497

Answers (1)

Konstantin
Konstantin

Reputation: 806

I found solution! For me it works with the following code:

        Novacode.Paragraph p = docx.InsertParagraph(someText, false, someFormat);
        p.InsertPageBreakAfterSelf();

or just add empty paragraph:

        Novacode.Paragraph p = docx.InsertParagraph(string.Empty, false);
        p.InsertPageBreakAfterSelf();

Thus if we insert page without inserting section it will no break the format!

Upvotes: 4

Related Questions