[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-11
2
Medium Priority
?
3,170 Views
Last Modified: 2012-09-11
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.
0
Comment
Question by:dsudbury
2 Comments
 
LVL 9

Accepted Solution

by:
teebon earned 2000 total points
ID: 38389794
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
 

Author Closing Comment

by:dsudbury
ID: 38389851
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

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question