Read data between bookmarks from word document in c#

Hi,

I am trying to read data between 2 bookmarks except Numbering in word document:

My issue is: I am able to read data between 2 bookmarks but only issue is that numbering is also getting read.
I want to skip the numbering data while reading document.

                SetLicencse();
                Document doc = new Document(fname);
                DocumentBuilder builder = new DocumentBuilder(doc);
                builder.MoveToDocumentEnd();
                builder.StartBookmark("DocumentEnd");
                builder.EndBookmark("DocumentEnd");
                BookmarkCollection bmCollection = doc.Range.Bookmarks;
                string bookmarkName = string.Empty;
                string bookmarkHtml = string.Empty;
                for (int i = 0; i < bmCollection.Count - 1; i++)
                {
                    BookmarkEnd start = doc.Range.Bookmarks[i].BookmarkEnd;
                    BookmarkStart end = doc.Range.Bookmarks[i + 1].BookmarkStart;

                    ArrayList nodes = ExtractContent(start, end, true);
                    Document htmlDoc = GenerateDocument(doc, nodes);
                    String sb = htmlDoc.ToString(SaveFormat.Html);
 bookmarkName = bmCollection[i].Name;
                    string a,b;
                    if (bookmarkName == "CurrentMinutes")
                    {
                        a = sb.ToString();
                    }
                    else if (bookmarkName == "BriefCompanyBackground")
                    {
                        b = sb.ToString();
                    }
                }

For Eg:
Word Document Data:
1.1 Summary

1.2 [Current Minutes ]
Current Minutes is need to defined here

1.3 [Brief Company Background, Business Summary]

Here 1.1, 1.2 , 1.3 are Numbering defined in word document.

Output while Reading Data:
“a” variable:
Current Minutes is need to defined here

1.3

Issue in output:
Here i don’t want 1.3 numbering

@pravinghadge,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 19.3 generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word.
  • Please also create a simplified standalone console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

Hi Hafeez,

Thanks for your reply.

Please find attached zip fileAspose.zip (249.8 KB)

@pravinghadge,

You can workaround this problem by using the following code:

private void FillTextBoxes()
{
    string fname = textBox1.Text;
    Document doc = new Document(fname);
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToDocumentEnd();
    builder.StartBookmark("DocumentEnd");
    builder.EndBookmark("DocumentEnd");
    BookmarkCollection bmCollection = doc.Range.Bookmarks;
    string bookmarkName = string.Empty;
    string bookmarkHtml = string.Empty;
    for (int i = 0; i < bmCollection.Count - 1; i++)
    {
        BookmarkEnd start = doc.Range.Bookmarks[i].BookmarkEnd;
        BookmarkStart end = doc.Range.Bookmarks[i + 1].BookmarkStart;

        ArrayList nodes = ExtractContent(start, end, true);

        foreach (Node node in nodes)
        {
            if (node.NodeType == NodeType.Paragraph)
            {
                Paragraph para = (Paragraph)node;
                if (para.IsListItem)
                {
                    para.ListFormat.List = null;
                }
            }
        }

        Document htmlDoc = GenerateDocument(doc, nodes);
        String sb = htmlDoc.ToString(SaveFormat.Html);
        bookmarkName = bmCollection[i].Name;
        if (bookmarkName == "CurrentMinutes")
        {
            wbCurrentMinutes.DocumentText = sb.ToString();
        }
        else if (bookmarkName == "BriefCompanyBackground")
        {
            wbBriefCompany.DocumentText = sb.ToString();
        }
        else if (bookmarkName == "RatingDerivation")
        {
            wbRatingDerivation.DocumentText = sb.ToString();
        }

    }
}

Thank you @awais.hafeez

Issue has been resolved.

Only one thing i have noticed extra “Top margin” blank space is coming.

I have attached file for the sameTop margin issue.jpg (45.9 KB)

@pravinghadge,

The following code should remove the extra blank line at the start:

private void FillTextBoxes()
{
    string fname = textBox1.Text;
    Document doc = new Document(fname);
    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToDocumentEnd();
    builder.StartBookmark("DocumentEnd");
    builder.EndBookmark("DocumentEnd");
    BookmarkCollection bmCollection = doc.Range.Bookmarks;
    string bookmarkName = string.Empty;
    string bookmarkHtml = string.Empty;
    for (int i = 0; i < bmCollection.Count - 1; i++)
    {
        BookmarkEnd start = doc.Range.Bookmarks[i].BookmarkEnd;
        BookmarkStart end = doc.Range.Bookmarks[i + 1].BookmarkStart;

        ArrayList nodes = ExtractContent(start, end, true);

        foreach (Node node in nodes)
        {
            if (node.NodeType == NodeType.Paragraph)
            {
                Paragraph para = (Paragraph)node;
                if (para.IsListItem)
                {
                    para.ListFormat.List = null;
                }
            }
        }

        Document htmlDoc = GenerateDocument(doc, nodes);
        String sb = "";
        bookmarkName = bmCollection[i].Name;
        if (bookmarkName == "CurrentMinutes")
        {   
            htmlDoc.FirstSection.Body.FirstParagraph.Remove();
            sb = htmlDoc.ToString(SaveFormat.Html);
            wbCurrentMinutes.DocumentText = sb.ToString();
        }
        else if (bookmarkName == "BriefCompanyBackground")
        {
            htmlDoc.FirstSection.Body.FirstParagraph.Remove();
            sb = htmlDoc.ToString(SaveFormat.Html);
            wbBriefCompany.DocumentText = sb.ToString();
        }
        else if (bookmarkName == "RatingDerivation")
        {
            htmlDoc.FirstSection.Body.FirstParagraph.Remove();
            sb = htmlDoc.ToString(SaveFormat.Html);
            wbRatingDerivation.DocumentText = sb.ToString();
        }
    }
}

Thanks a lot Awais.hafeez