-1

I used WordprocessingDocument to add the paragraph at the end of a word after the last paragraph, but I need to add this paragraph at the end of the 15th page in a word document.

Below is my code adding the paragraph at the end of the document:

using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
{
    gjenerimi = randomstring(14);
    var body = wDoc.MainDocumentPart.Document.Body;
    var lastParagraf = body.Elements<Paragraph>().LastOrDefault();
    var run = new Run();

    run.AppendChild(new Text(DateTime.Now.ToString() + " , "));
    run.AppendChild(new Text(gjenerimi + " , "));
    run.AppendChild(new Text(merreshifren()));

    lastParagraf.AppendChild(run);
}
McGuireV10
  • 9,572
  • 5
  • 48
  • 64
Arbenita Musliu
  • 95
  • 4
  • 11
  • Please describe what's not working with the code you've shown. What are you expecting and what is the result? What is your question? – Rufus L Mar 27 '18 at 13:54
  • For starters your going to have to check pages. – Linda Lawton - DaImTo Mar 27 '18 at 13:55
  • I want the paragraph to be added in the end of the page above the last page of document not at the end of document as it is displaying now. – Arbenita Musliu Mar 27 '18 at 13:58
  • Okay then update your question – Christlin Panneer Mar 27 '18 at 14:36
  • Working in the Open XML file it's impossible to know where any page ends. Word dynamically lays out the pages when the document is opened and edited - this information is not saved in the closed document. You can only be sure of where a page breaks if a manual break has been inserted. If that's not the case in your document(s) then you can achieve this only using "the interop" when the document is opened in the Word application. (And even then it's only a "shapshot" as further editing could change that.) – Cindy Meister Mar 27 '18 at 14:56
  • Hello Cindy. How can I use Interop for adding the paragraph at the end of the 15th page? – Arbenita Musliu Mar 28 '18 at 12:10
  • is the answer working? – Christlin Panneer Apr 02 '18 at 14:53

1 Answers1

1

From your comment, you need to add a paragraph at the last page, not at the end of the page.

To achieve this you need to Append a new Paragraph to your Body then create Run from that Paragraph object, then Append Text on the Run object.

Example

Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("This is my text"));
wordprocessingDocument.Close();

Update (according to the request by question author in the comment section)

If you want to add text before the last paragraph, kindly follow the steps below.

Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
List<Paragraph> paragraphs = body.OfType<Paragraph>()
    .Where(p => p.InnerText != "")
    .ToList();
if(paragraphs.Count > 1)
{
    Paragraph beforeLast = paragraphs[paragraphs.Count - 2];
    Run run = beforeLast.AppendChild(new Run());
    run.AppendChild(new Break()); //Line Break
    run.AppendChild(new Text("This is my text paragraph, before the last one new"));
    run.AppendChild(new Break());
}
Christlin Panneer
  • 1,599
  • 2
  • 19
  • 31
  • My word document has 16 pages.I need to add the paragraph at the end of the 15th page.I added the paragraph at the end of the document, but I don't need that. – Arbenita Musliu Mar 27 '18 at 15:50
  • in that case, you can count page breaks between paragraphs. But we can't guarantee that every page contains page break. So its impossible to add after a page. Sorry!! – Christlin Panneer Mar 28 '18 at 05:58
  • var lastParagraf = body.Elements().LastOrDefault(); This line tells where to add the paragraph. Is there any way to tell the paragraph to be added at the 15th page or before the last paragraph in the last page of the document? – Arbenita Musliu Mar 28 '18 at 06:20
  • you can add before the last paragraph. I'll update the Code. – Christlin Panneer Mar 29 '18 at 17:17
  • update the question, so others can find it useful. Thanks. – Christlin Panneer Mar 29 '18 at 17:41