Read Formatted Text from Bookmark to Database

Hi Allan,

Thanks for your inquiry. I believe, the following code change will fix this issue:

Document doc = new Document(MyDir + @"Global+warming.docx");
BookmarkCollection bmCollection = doc.Range.Bookmarks;
DataTable dtUnitCurriculum = new DataTable();
foreach(Bookmark bm in bmCollection)
{
    ArrayList nodes = ExtractContent1(bm.BookmarkStart, bm.BookmarkEnd);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i <nodes.Count; i++)
    {
        Node node = (Node) nodes[i];
        if (node.IsComposite)
        {
            sb.Append(node.ToString(SaveFormat.Html));
            if (((CompositeNode) node).LastChild.NodeType == NodeType.BookmarkEnd)
                i = nodes.IndexOf(((CompositeNode) node).LastChild.PreviousPreOrder(doc)) + 1;
            else
                i = nodes.IndexOf(((CompositeNode) node).LastChild) + 1;
            continue;
        }
        sb.Append(node.ToString(SaveFormat.Html));
    }
    string bookmarkName = bm.Name;
    string bookmarkHtml = sb.ToString();
}

Best regards,

Hi,
One more issue I’ve come through recently. I have bookmarks inside a table, each row contains a bookmark and text associated with the bookmark. It reads properly until the line break. It throws an “Object reference is not set to instance of the object” exception when it tries to read an empty line and the next line starts from next page. I have attached the document from which i faced the issue. Need solution and your help will be appreciated.

Regards
Allan.

Hi Allan,

Thanks for your inquiry. I think, you can simply add the following check to avoid this exception.

……

for (int i = 0; i <nodes.Count; i++)
{
    Node node = (Node) nodes[i];
    if (node.IsComposite && ((CompositeNode) node).ChildNodes.Count> 0)
    {
        sb.Append(node.ToString(SaveFormat.Html));

……

I hope, this helps.

Best regards,