How to edit bookmarks in a Word template using DocumentFormat.OpenXml and save it as a new PDF file?
<p>I'm really having trouble in editing bookmarks in a Word template using <code>Document.Format.OpenXML</code> and then saving it to a new PDF file.
I cannot use <code>Microsoft.Word.Interop</code> as it gives a COM error on the server.</p>

<p>My code is this:</p>

<pre><code>public static void CreateWordDoc(string templatePath, string destinationPath, Dictionary&lt;string, dynamic&gt; dictionary)
{

    byte[] byteArray = File.ReadAllBytes(templatePath);
    using (MemoryStream stream = new MemoryStream())
    {
        stream.Write(byteArray, 0, (int)byteArray.Length);
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
        {
            var bookmarks = (from bm in wordDoc.MainDocumentPart.Document.Body.Descendants&lt;BookmarkStart&gt;()
                             select bm).ToList();

            foreach (BookmarkStart mark in bookmarks)
            {

                if (mark.Name != "Table" &amp;&amp; mark.Name != "_GoBack")
                {

                 UpdateBookmark(dictionary, mark);//Not doing anything

                }
                else if (mark.Name != "Table")
                {
                    //  CreateTable(dictionary, wordDoc, mark);
                }
            }
           File.WriteAllBytes("D:\\RohitDocs\\newfile_rohitsingh.docx", stream.ToArray());

            wordDoc.Close();

        }
        // Save the file with the new name

    }
}

private static void UpdateBookmark(Dictionary&lt;string, dynamic&gt; dictionary, BookmarkStart mark)
{
    string name = mark.Name;
    string value = dictionary[name];

    Run run = new Run(new Text(value));
    RunProperties props = new RunProperties();

    props.AppendChild(new FontSize() { Val = "20" });
    run.RunProperties = props;
    var paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(run);
    mark.Parent.InsertAfterSelf(paragraph);

    paragraph.PreviousSibling().Remove();
    mark.Remove();
}
</code></pre>

<p>I was trying to replace bookmarks with my text but the <code>UpdateBookmark</code> method doesn't work. I'm writing stream and saving it because I thought if bookmarks are replaced then I can save it to another file.</p>