Appending Content

Here we modify the document as it is read - appending some new content onto the end.

We read a DOCX file and pull out all of the paragraph and text runs. We add a new paragraph onto the end of the document - one that containes the words "The End". We duplicate the style of the last paragraph but change the font size. Finally we save the output.

Appending Text to a Word document.
C#
string src = TestEnvironment.FixturePath("MaryLamb.docx");
string dst = TestEnvironment.OutputPath("LongerMary.pdf");
Doc doc = new Doc();
doc.SetFile(src);
// find the end of the document
ParagraphFrame lastPara = null;
TextRunFrame lastRun = null;
doc.DocumentFrame.Scan((f) => {
    if (f is ParagraphFrame)
        lastPara = (ParagraphFrame)f;
    if (f is TextRunFrame)
        lastRun = (TextRunFrame)f;
});
Frame parent = lastPara.Parent;
// add a paragraph at the end
ParagraphFrame para = new ParagraphFrame();
para.StyleStore = lastPara.StyleStore;
TextRunFrame run = new TextRunFrame();
run.Text = "The End";
run.StyleStore = lastRun.StyleStore.Clone();
run.StyleStore.Entries["FontSize"] = "36";
LineFrame line = new LineFrame();
line.Adopt(line.Children.AddLast(run));
para.Adopt(para.Children.AddLast(line));
parent.Adopt(parent.Children.AddLast(para));
// save
doc.SaveAs(dst);