Search code examples
c#openxmldocxopenxml-sdk

Append characters to new page in `docx` document


I am trying to append a string to the end of a docx document in a new page.

Here is the code I use now:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(path/fname, true))

var body = wordDoc.MainDocumentPart.Document.Body;
var para = body.AppendChild(new Paragraph());
var run = para.AppendChild(new Run());

var txt = "Document Signed by User" + Environment.NewLine;
run.AppendChild(new Text(txt));

But it appends the text in the end of the document and not in a new page.

Edit

Used the solution proposed by Daniel A. White:

var para = body.AppendChild(new Paragraph());
var run = para.AppendChild(new Run());
var plc = run.AppendChild(new Break() { Type = BreakValues.Page });
var txt = "Document Signed by User: " + user.User" + Environment.NewLine;
plc.AppendChild(new Text(txt));

But I got this error :

Non-composite elements do not have child elements


Solution

  • Insert a Break into your Run with Type set to BreakValues.Page.

    run.AppendChild(new Break() { Type = BreakValues.Page });