Add a paragraph before last paragaph using OpenXML SDK and C#

Thought this would be a dirt simple OpemXML SDK task....
Basically:
How do I add a new paragraph before the last paragraph or, as you will see below, before the Section Properites element? I have been using the Append and AppendChild methods and they work as advertised. BUT, I want to add paragraphs before the Sections Properites NOT after it.
Not so Basic:
I am using a "for" loop in C# for the content
FinalCCContent[i].content

Open in new window


So using a "for" loop how do I continually add a paragraph just before the last one?

Oh and my LINQ skills are only slowly emerging - Eric White I will understand your examples yet!!!   So simple LINQ and functional programming style examples are welcome.

Thanks in advance to anyone who can help me out here.

David Sudbury

Details follow:
========================================

I have a template that has essentially nothing in it:
<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:p w:rsidR="0024040D" w:rsidRDefault="0024040D">
    <w:bookmarkStart w:name="_GoBack" w:id="0" />
    <w:bookmarkEnd w:id="0" />
  </w:p>
  <w:sectPr w:rsidR="0024040D">
    <w:pgSz w:w="12240" w:h="15840" />
    <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0" />
    <w:cols w:space="708" />
    <w:docGrid w:linePitch="360" />
  </w:sectPr>
</w:body>

Open in new window

I create a new WordprocessingDocument based on that template. This all works just fine. But then I add a few paragraphs using the AppendChild method using this code:
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Heading3" };
paragraphProperties1.AppendChild(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "The Test Chapter Title";
run1.AppendChild(text1);
paragraph1.AppendChild(paragraphProperties1);
paragraph1.AppendChild(run1);
body.AppendChild(paragraph1);

paragraph1 = new Paragraph();
paragraphProperties1 = new ParagraphProperties();
paragraphStyleId1 = new ParagraphStyleId() { Val = "Normal" };
paragraphProperties1.Append(paragraphStyleId1);
run1 = new Run();
text1 = new Text();
text1.Text = "Chapter Goal: This is the chapter goal";
run1.AppendChild(text1);
paragraph1.AppendChild(run1);
body.AppendChild(paragraph1);

Open in new window

It runs fine, if a little verbose. (I promise to tidy it up) But upon examination I see that the Section Properties is not at the bottom of the Body element but stuck right after the first paragraph:
<w:body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:p w:rsidR="0024040D" w:rsidRDefault="0024040D">
    <w:bookmarkStart w:name="_GoBack" w:id="0" />
    <w:bookmarkEnd w:id="0" />
  </w:p>
//-----------------------------------------------------------------------------------------------------------------
//How do I APPEND before this...???
//-----------------------------------------------------------------------------------------------------------------
<w:sectPr w:rsidR="0024040D">
    <w:pgSz w:w="12240" w:h="15840" />
    <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0" />
    <w:cols w:space="708" />
    <w:docGrid w:linePitch="360" />
  </w:sectPr>
  <w:p>
    <w:pPr>
      <w:pStyle w:val="Heading3" />
    </w:pPr>
    <w:r>
      <w:t>The Test Chapter Title</w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:t>Chapter Goal: This is the chapter goal</w:t>
    </w:r>
  </w:p>
</w:body>

Open in new window

As I add more and more paragraphs they dutifully append to the bottom, but the pesky Section Properties remains in the wrong spot. The result being my code creates a fixable but corrupt file structure - the Section Properties element should be the last one. MS Word will notify me of the corrupt nature of my file and then move the Section Properties to the last spot.  But, darn it I should be able to create and place paragraphs in the correct spot.

I have used PrependChild but then I have to reverse the logic in my code and make it HUGELY harder to figure out and problem solve.
dsudburyAsked:
Who is Participating?
 
teebonConnect With a Mentor Product ManagerCommented:
Hi David,

Try get the last paragraph by the following code:

Paragraph p =
  doc.MainDocumentPart.Document.Body.Descendants<Paragraph>()
  .LastOrDefault();

Then, append to the last paragraph by p.append
0
 
dsudburyAuthor Commented:
Yes this is the solution and it is just fine. I must remember the Descendants axis.

Incidently this answer, which I thought would eliminate my corruption problem, is correct but it is not the reason why my code blew up my word file!!

You see I was including revision ID's with "everything" They certainly are not needed but if you include them in your code they must be correctly dealt with. Which I did not. So eliminate them an viola it all works swimmingly.

Thanks for being there!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.