Replace Bookmark Text Spanning across Multiple Paragraphs - C# .NET

Greetings,

I have a .doc file with various bookmarks that I need to fill in with Aspose.word. I followed the instruction as stated here but as you can see in the files I attached the result is horrific. The text intended appearing in the column before it. I understand the .doc file’s bookmark is kind of weird (maybe nested?), but please note the document is provided by other parties so I cannot modify it.

Are there any ways to do it without altering the .doc file?

Thanks and Happy Holidays in advance

20191218.zip (26.3 KB)

Sorry I forgot to mention. I am using C# and this is the code I used to set the text in a bookmark:

DocumentBuilder builder;
Document doc;

var docStream = new System.IO.FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
doc = new Document(docStream);
docStream.Close();

builder = new DocumentBuilder(doc);
var bookmark = doc.Range.Bookmarks[“bookmarkName”];
builder.MoveToBookmark(“bookmarkName”);
bookmark.Remove();
builder.Write(“text”);

doc.Save(outputFilePath);

@zincyeung,

For example, the “WORKSHEET9” bookmark in your Word document spans across two Paragraphs i.e. its BookmarkStart is inside first Paragraph while its BookmarkEnd is inside another Paragraph. The following code will produce expected output in this case:

Document doc = new Document("E:\\Temp\\20191218\\input.doc");
DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark bm = doc.Range.Bookmarks["WORKSHEET9"];

ArrayList list = new ArrayList();

Node curNode = bm.BookmarkStart.NextPreOrder(doc);
while (curNode != null)
{
    if (curNode.Equals(bm.BookmarkEnd))
        break;

    if (curNode.NodeType == NodeType.Run)
        list.Add(curNode);

    curNode = curNode.NextPreOrder(doc);
}

foreach (Run run in list)
    run.Remove();

builder.MoveToBookmark("WORKSHEET9", true, false);
builder.Write("Test Title");

doc.Save("E:\\Temp\\20191218\\19.12.docx");