I am looking for a way to insert some text after a bookmark in a word doc using openxml. So far, i have been able to locate the bookmark using the following:
var
using
bookMarkToWriteAfter.Parent.InsertAfterSelf(run);
you are trying to work with XML directly which is not always advisable with OpenXML.
Try This..
Body body = mainPart.Document.GetFirstChild();
var paras = body.Elements();
//Iterate through the paragraphs to find the bookmarks inside
foreach (var para in paras)
{
var bookMarkStarts = para.Elements();
var bookMarkEnds = para.Elements();
foreach (BookmarkStart bookMarkStart in bookMarkStarts)
{
if (bookMarkStart.Name == bookmarkName)
{
//Get the id of the bookmark start to find the bookmark end
var id = bookMarkStart.Id.Value;
var bookmarkEnd = bookMarkEnds.Where(i => i.Id.Value == id).First();
var runElement = new Run(new Text("Hello World!!!"));
para.InsertAfter(runElement, bookmarkEnd);
}
}
}
mainPart.Document.Save();