We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Read Contents between 2 bookmarks using c#.net

Hi All,

I am trying to get data of the word document after bookmark

I have tried below code. But it is not working.

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 == “PR_CurrentRatings”)
{
KRD_Text1.Text = bookmarkHtml;
}
if (bookmarkName == “PR_RatingDrivers”)
{
KRI_Text1.Text = bookmarkHtml;
}
}

@pravinghadge

Thanks for your inquiry. Please read the following article.
Extract Content from a Bookmark

We suggest you please use the following code example to extract the contents and convert them to HTML. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
BookmarkCollection bmCollection = doc.Range.Bookmarks;

foreach (Bookmark bm in bmCollection)
{
    ArrayList nodes = Common.ExtractContent(bm.BookmarkStart, bm.BookmarkEnd, true);
    Document htmlDoc = Common.GenerateDocument(doc, nodes);
    String html = htmlDoc.ToString(SaveFormat.Html);
}

Thanks for your reply Tahir.

My Requirement is little different.

I have multiple bookmarks in my word document.

I want to read contents of word document after bookmarks defined.

Please find my word document test.zip (23.0 KB)

In this attached word document, I have “Overview,Current,Key Rivers” bookmarks.

After “Overview” Bookmark, i want to get the text of it.

so i should get “Overview” bookmark data as
: "The Outlook is Stable. The instrument-wise rating actions are given below: "

For “Current” BookMark, i should get table .

For “Key Rivers” i should get:
Strong and Diversified Consolidated Business Profile: AIL has three major business segments namely speciality chemicals (78%), pharmaceuticals (15%) and home/ personal care (7%). Under each business segment, there are various end-products which are used in diversified sectors. The company also has a diversified geographical mix with exports accounting for 45% of the consolidated revenue. The major export markets for the company are - North America (26%), Europe (25%), China (10%), Japan (10%) and 29% from the rest of the world.”

@pravinghadge

Thanks for your inquiry. Please use the following code example to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.StartBookmark("DocumentEnd");
builder.EndBookmark("DocumentEnd");
BookmarkCollection bmCollection = doc.Range.Bookmarks;

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 = Common.ExtractContent(start, end, true);
    Document htmlDoc = Common.GenerateDocument(doc, nodes);
    htmlDoc.Save(MyDir + "output_" + i + ".html");
    String html = htmlDoc.ToString(SaveFormat.Html);
}

Thanks Tahir.manzoor for your support.

I almost got my solution.

Only issue is here for every bookmark new html is getting saved.

Can i get different solution for extracting html content withour saving html document

@pravinghadge

Thanks for your inquiry. You do not need to save HTML document to disk. Please use Document.ToString(SaveFormat.Html) as shown below.

String html = htmlDoc.ToString(SaveFormat.Html);