wordpress hit counter
Replacing Text of a Bookmark in an OpenXML WordprocessingML Document - OpenXML Developer - Blog - OpenXML Developer

Replacing Text of a Bookmark in an OpenXML WordprocessingML Document

Blog

Samples, Demos, and Reference Articles

Replacing Text of a Bookmark in an OpenXML WordprocessingML Document

Rate This
  • Comments 2

In a previous screen-cast, I showed how to retrieve the text of an OpenXML WordprocessingML document.  In this post and screen-cast, I introduce some code that enables you to replace the text of a bookmark.

This blog post and screen-cast build on the previous screen-cast.  In that screen-cast, I used an approach of ‘flattening the paragraphs’.  This is an approach of transforming the WordprocessingML to another form that is easier to work with.  This other form is not valid WordprocessingML, but it is much easier to process the w:bookmarkStart and w:bookmarkEnd elements, and the elements between them.  Then, after setting the text of the bookmark, the code then transforms the WordprocessingML back to valid markup.

This screen-cast will make more sense if you first view the previous screen-cast.  In that screen-cast, I introduce the notion of ‘flattening the paragraphs’, and discuss the algorithm.

As I have progressed in my understanding of OpenXML markup, and various approaches to processing the markup, a recurring theme is that it often is most effective to first do an intermediate transform of the markup to another form that is easier to process.  Flattening paragraphs is one of these intermediate transforms that can prove useful in a number of scenarios.  One of  my upcoming projects is to do a survey of all the ways you can do an intermediate transform of OpenXML to make it easier to implement various scenarios.

Attachment: ReplaceBookmarkText.zip
  • Hey Eric,

    First of all i will thank you for your great videos. I have learned allot so far.

    I am making a project where i get a predefined template file (.docx) with several bookmarks in it. I will then make a copy of the file and replace the bookmarks with some external data.

    When i have bookmarks placed in center of the document it fails. The bookmark text replacing is working, but the two files are not identical.

    I have compared the two files in Productivity Tool. The copy of the template does not contain the html code for centering tekst, but the template file does.

    Du you have any suggestion on fixing the problem ?

    (I have also looked in the ECMA-376, but i could not figure it out).

  • Hi Eric

    As someone else has pointed out in the comments that there is an issue with replacing bookmarks that are "center justified". The bookmark text replaces the text but the text gets default left justified. I was able to trace the issue in the code that is causing this. There is a filter in the method "FlattenParagraphsTransform". The line "Where(e => e.Name != W.pPr)" is causing the issue. I commented the line and the code works fine and my bookmark text stays center justified.With the filter in place the following formatting is lost:

     <w:pPr>

           <w:jc w:val="center" />

      </w:pPr>

    My questions is why is this filter there and what issues can arise when its commented out. Any help is greatly appreciated.

    Thanks

    Amit

    Below is a sample chunk of my OpenXML document:

    <w:document xmlns:wpc="schemas.microsoft.com/.../wordprocessingCanvas" xmlns:mc="schemas.openxmlformats.org/.../2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="schemas.openxmlformats.org/.../relationships" xmlns:m="schemas.openxmlformats.org/.../math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="schemas.microsoft.com/.../wordprocessingDrawing" xmlns:wp="schemas.openxmlformats.org/.../wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="schemas.openxmlformats.org/.../main" xmlns:w14="schemas.microsoft.com/.../wordml" xmlns:wpg="schemas.microsoft.com/.../wordprocessingGroup" xmlns:wpi="schemas.microsoft.com/.../wordprocessingInk" xmlns:wne="schemas.microsoft.com/.../wordml" xmlns:wps="schemas.microsoft.com/.../wordprocessingShape" mc:Ignorable="w14 wp14">

     <w:body>

       <w:p w:rsidR="00B72D3F" w:rsidRDefault="008E793D" />

       <w:p w:rsidR="00BA5142" w:rsidRDefault="00BA5142" />

       <w:p w:rsidR="00BA5142" w:rsidRDefault="00BA5142" />

       <w:p w:rsidR="00BA5142" w:rsidRDefault="00BA5142" w:rsidP="005564E2">

         <w:pPr>

           <w:jc w:val="center" />

         </w:pPr>

       </w:p>

       <w:p w:rsidR="00BA5142" w:rsidRDefault="00BA5142" w:rsidP="005564E2">

         <w:pPr>

           <w:jc w:val="center" />

         </w:pPr>

       </w:p>

       <w:p w:rsidR="00BA5142" w:rsidRDefault="00BA5142" w:rsidP="005564E2">

         <w:pPr>

           <w:jc w:val="center" />

           <w:rPr>

             <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial" />

             <w:color w:val="008000" />

             <w:sz w:val="52" />

             <w:szCs w:val="52" />

           </w:rPr>

         </w:pPr>

         <w:bookmarkStart w:id="0" w:name="bmCompanyName" />

         <w:r w:rsidRPr="00BA5142">

           <w:rPr>

             <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial" />

             <w:color w:val="008000" />

             <w:sz w:val="52" />

             <w:szCs w:val="52" />

           </w:rPr>

           <w:t>COMPANY</w:t>

         </w:r>

         <w:bookmarkEnd w:id="0" />

       </w:p>

Page 1 of 1 (2 items)