Search code examples
c#ms-wordopenxmlopenxml-sdk

How to correctly apply style to Open Office XML Bookmark text


I have prepared a MS Word document template ,and inserted bookmarks where I would like to insert text. Currently, I am able to insert text, but all native styling is gone (ie: font size/style) I was expecting the styles to be inherited.

I've been referring to this question for inspiration, but I receive malformed XML:

<w:rFonts w:ascii="Playfair Display" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" />

Here is a snippet of the bookmark start/end I am working with:

<?xml version="1.0" encoding="UTF-8"?>
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" w:rsidR="00300E4C" w:rsidRDefault="00582F11">
   <w:pPr>
      <w:spacing w:before="265" />
      <w:ind w:left="2822" w:right="1122" />
      <w:jc w:val="center" />
      <w:rPr>
         <w:rFonts w:ascii="Playfair Display" />
         <w:i />
         <w:sz w:val="88" />
      </w:rPr>
   </w:pPr>
   <w:bookmarkStart w:name="F_4000" w:id="0" />
   <w:bookmarkEnd w:id="0" />
   <w:r>
      <w:rPr>
         <w:rFonts w:ascii="Playfair Display" />
         <w:i />
         <w:color w:val="113628" />
         <w:sz w:val="88" />
      </w:rPr>
      <w:t xml:space="preserve"> </w:t>
   </w:r>
   <w:bookmarkStart w:name="F_4002" w:id="1" />
   <w:bookmarkEnd w:id="1" />
</w:p>

When using the following code:

IEnumerable<OpenXmlElement> elementsAfter = bookmark.ElementsAfter();
IEnumerable<OpenXmlElement> insideBookmark = elementsAfter.TakeWhile(element => !(element is BookmarkEnd));
foreach (OpenXmlElement element in insideBookmark)
{
    element.RemoveAllChildren();
}

OpenXmlElement previousSibling = bookmark.PreviousSibling();
while (previousSibling is BookmarkStart || previousSibling is BookmarkEnd)
{
    previousSibling = previousSibling.PreviousSibling();
}

//Get previous font.
var runProperties = previousSibling.GetFirstChild<ParagraphMarkRunProperties>().GetFirstChild<RunFonts>();
//var runProperties = previousSibling.GetFirstChild<RunProperties>(); - if its simple element.

// Clone.
var newProperty = (RunFonts)runProperties.Clone();

// Create container with properties
// This is where I run into malformed XML for newProperty
var container = new Run(text)
{
    RunProperties = new RunProperties() { RunFonts = newProperty }
};

EDIT: After inserting some text into the bookmark locations here is the XML (the paragraph XML that contains the XML)

<w:p w:rsidR="00300E4C" w:rsidRDefault="00582F11" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  <w:pPr>
    <w:spacing w:before="265" />
    <w:ind w:left="2822" w:right="1122" />
    <w:jc w:val="center" />
    <w:rPr>
      <w:rFonts w:ascii="Playfair Display" />
      <w:i />
      <w:sz w:val="88" />
    </w:rPr>
  </w:pPr>
  <w:r>
    <w:t>First</w:t>
  </w:r>
  <w:bookmarkStart w:name="F_4000" w:id="0" />
  <w:bookmarkEnd w:id="0" />
  <w:r>
    <w:rPr>
      <w:rFonts w:ascii="Playfair Display" />
      <w:i />
      <w:color w:val="113628" />
      <w:sz w:val="88" />
    </w:rPr>
    <w:t xml:space="preserve"> </w:t>
  </w:r>
  <w:r>
    <w:t>Last</w:t>
  </w:r>
  <w:bookmarkStart w:name="F_4002" w:id="1" />
  <w:bookmarkEnd w:id="1" />
</w:p>

I'm just trying to grab the native (already active) style, but I think the bookmark is breaking it up somehow. Any suggestions are much appreciated.


Solution

  • The way I eventually figured out is below. The biggest help was using the Open XML SDK Productivity Tool to figure out what sort of structure I need to be inserting into. The two spots I needed to input text into were situated in a strange manner, so it was easier to do them in one go, and for the rest of the elements I just InsertAfterSelf()

    if (bookMarkEnd.Key == "F_4000" || bookMarkEnd.Key == "F_4002")
    {
         OpenXmlElement previousSibling = bookMarkEnd.Value.Parent.PreviousSibling();
    
         while (previousSibling is BookmarkStart || previousSibling is BookmarkEnd)
         {
             previousSibling = previousSibling.PreviousSibling();
         }
    
         OpenXmlElement next = bookMarkEnd.Value.NextSibling<Run>();
    
         if (next != null)
         {
             next.GetFirstChild<Text>().Text = bookMarkEnd.Key + " " + "F_4002";
         }
    }