0
votes

I have a dynamically generated docx file.

Need write the text strictly to end of page.

With Microsoft.Interop i insert Paragraphs before text:

int kk = objDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref wMissing);
while (objDoc.ComputeStatistics(WdStatistic.wdStatisticPages, ref wMissing) != kk + 1)
                {
                    objWord.Selection.TypeParagraph();

                }
                objWord.Selection.TypeBackspace();

But i can't use same code with Open XML, because pages.count calculated only by word. Using interop impossible, because it so slowwwww.

1

1 Answers

0
votes

There are 2 options of doing this in Open XML.

  1. create Content Place holder from Microsoft Office Developer Tab at the end of your document and now you can access this Content Place Holder programatically and can place any text in it.

  2. you can append text driectly to your word document where it will be inserted at the end of your text. In this approach you got to write all the stuff to your document first and once you are done than you can append your document the following way

//

public void WriteTextToWordDocument()
{    
    using(WordprocessingDocument doc = WordprocessingDocument.Open(documentPath, true))
    {
        MainDocumentPart mainPart = doc.MainDocumentPart;
        Body body = mainPart.Document.Body;
        Paragraph paragraph = new Paragraph();
        Run run = new Run();
        Text myText = new Text("Append this text at the end of the word document");
        run.Append(myText);
        paragraph.Append(run);
        body.Append(paragraph);

        // dont forget to save and close your document as in the following two lines
        mainPart.Document.Save();
        doc.Close();
    }
}

I haven't tested the above code but hope it will give you an idea of dealing with word document in OpenXML. Regards,