Inserting Text after bookmark in C#.net

Thanx for your support.

Finally i am using the bookmarkcollection method to get my output.
Only one issue is coming in output .

When i am reading first bookmark data in string , second bookmark name is also gets read in the fist string.

(I have attached the demo file from where i read the bookmarks data)
ExportedFile (1).zip (107.6 KB)

Can you please suggest where i am wrong in below code.

                    Document doc = new Document(fname);
                    BookmarkCollection bmCollection = doc.Range.Bookmarks;
                    foreach (Bookmark bm in bmCollection)
                    {
                        ArrayList nodes = ExtractContent1(bm.BookmarkStart, bm.BookmarkEnd);
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < nodes.Count; i++)
                        {
                            try
                            {
                                Node node = (Node)nodes[i];
                                if (node.IsComposite && ((CompositeNode)node).ChildNodes.Count > 0)
                                {
                                    sb.Append(node.ToString(SaveFormat.Html));
                                }
                            }
                            catch (Exception ex)
                            {
                                string er = ex.Message;
                            }

                        }
                        string bookmarkName = bm.Name;
                        string bookmarkHtml = sb.ToString();
                        if (bookmarkName == "CFHeadings")
                        {
                            txtAddress1.Text = bookmarkHtml;
                        }
                        if (bookmarkName == "Insurance_FRR_Heading")
                        {
                            txtAddress2.Text = bookmarkHtml;
                        }
                    }

@pravinghadge,

Thanks for your inquiry. The BookmarkEnd node of CFHeadings is after the BookmarkStart node of Insurance_FRR_Heading. You can check it in document.xml by unzipping your DOCX. Aspose.Words imports the document correctly. Please check the attached DOM image for detail. bookmark.png (17.3 KB)

You can fix this issue by inserting the BookmarkEnd node of CFHeadings before BookmarkStart node of Insurance_FRR_Heading. Please check the following code snippet.

Document doc = new Document(MyDir + "ExportedFile (1).docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Bookmark CFHeadings = doc.Range.Bookmarks["CFHeadings"];
if (CFHeadings.BookmarkEnd.PreviousSibling.NodeType == NodeType.BookmarkStart)
{
    builder.MoveTo(CFHeadings.BookmarkEnd.PreviousSibling);
    builder.EndBookmark("CFHeadings");
}

BookmarkCollection bmCollection = doc.Range.Bookmarks;

foreach (Bookmark bm in bmCollection)
{
1 Like

Thanx a lot tahir.manzoor

Issue has been resolved

@pravinghadge,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.