Free Support Forum - aspose.com

How to bookmark each question

Dear Team,
I need to find question number in word document .
Each question is preceded by a number of numbers and punctuation,such as 1. or 2、or 3.
Some question text may contain the same numbers and punctuation
How to bookmark each question using Aspose.Words .Please give solution and advice for this scenario.

input:input.zip (248.2 KB)

expected output:output.zip (248.4 KB)

@zhaoyimei,

Thanks for your inquiry. We suggest you please read following articles.
Working with Bookmarks
Moving the Cursor

In your case, we suggest you following solution.

  1. Get the paragraph’s collection using Document.GetChildNodes(NodeType.Paragraph, true).

  2. Iterate over this collection and check if paragraph’s text is started with number. You can check it using following code snippet.

    String digit = para.ToString(SaveFormat.Text).Trim().Substring(0, 1);
    bool isDigit = digit.Any(c => char.IsDigit©);

  3. Move the cursor to the paragraph and insert the StartBookmark node using DocumentBuilder.StartBookmark method.

  4. Move the cursor to the next paragraph that is started with number and insert the EndBookmark node using DocumentBuilder.EndBookmark method.

Hope this helps you.

Thank you so much。Based on the solution you provided, I basically achieved what I wanted。But there are some problems.。For example, when there is a data table in a paragraph, it does not exclude。How to get a list of paragraphs in a document, except in tables, graphics, and images。

input、output Word document and code :Data.zip (496.2 KB)

Thank you so much for your help, it worked for us. I appreciate your constant guidance.Expecting better solutions

@zhaoyimei,

Thanks for your inquiry. You can exclude the paragraphs that has images and child node of table using following code snippet. Hope this helps you.

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
if (para.GetAncestor(NodeType.Table) != null)
continue;
if (para.GetChildNodes(NodeType.Shape, true).Count > 0)
continue;

// your code to add bookmark

}