5

I have a docx document that I want to modify via OpenXML SDK. This document has a table with bookmarks in its cells. The cells have specific font settings, let's say it's Times New Roman, 14pt. When I try to insert some text like this:

    public void ReplaceBookmark(string bookMarkName, string text)
    {
        var bookmarkStart =
            _document.MainDocumentPart.RootElement.Descendants<BookmarkStart>()
        .Where(p => p.Name == bookMarkName)
        .FirstOrDefault();
        if (bookmarkStart == null)
            return;

        bookmarkStart.InsertAfterSelf(new Run(new Text(text)));            
    }

the texts is inserted, but its style is set Calibri, 11pt (the default style). How can I insert the text so that the font settings are preserved? The important thing is that i shouldn't define any style settings in the code, but use those of the original document instead.

Thanks.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Nu-hin
  • 691
  • 1
  • 7
  • 12

1 Answers1

2

I did a little investigating of the docx file format. Obviously I can't talk specifically about the file you are using... but I thought you might be interested in what I found.

If you create a copy of the docx file and give it a .zip extension, you can then extract out the contents of the document. In my case, and probably in yours too, the main part of the document is in the extracted file [Extraction Base Path]\word\document.xml.

The following XML snippet is the one that appears to apply to the first cell in the table (for the document I created):

<w:tc>
    <w:tcPr>
        <w:tcW w:w="3192" w:type="dxa"/>
    </w:tcPr>
    <w:p w:rsidR="006C4430" w:rsidRPr="006C4430" w:rsidRDefault="006C4430">
        <w:pPr>
            <w:rPr>
                <w:rFonts w:ascii="Ariel" w:hAnsi="Ariel"/>
                <w:sz w:val="28"/>
                <w:szCs w:val="28"/>
            </w:rPr>
        </w:pPr>
        <w:bookmarkStart w:id="0" w:name="First"/>
        <w:bookmarkEnd w:id="0"/>
        <w:r w:rsidRPr="006C4430">
            <w:rPr>
                <w:rFonts w:ascii="Bauhaus 93" w:hAnsi="Bauhaus 93"/>
                <w:sz w:val="28"/>
                <w:szCs w:val="28"/>
            </w:rPr>
            <w:t>Here is some text</w:t>
        </w:r>
    </w:p>
</w:tc>

As you can see, the w:bookmarkStart and w:bookmarkEnd tags occur outside of the table's actual content (which appear to be the w:r tag following the bookmark). This happened even though I had selected the entire cell contents before creating the bookmark. As a result, I suspect that any call made to bookmarkStart.InsertAfterSelf will, as you saw, use the default font settings, not any settings associated with the table cell. It looks to me as if you would need to either navigate to the run immediately after the bookmark, and insert your text there, or copy the settings (presumably the contents of the w:rPr tag) from the next run into the new run that you create.

Hopefully this will point you in the appropriate direction. Good luck!

Richard J Foster
  • 4,278
  • 2
  • 32
  • 41