How to Extract Content of Bookmark and Export them into Text format using .NET

Hi,
I have a document and want to get the bookmark text. The bookmark also contains a (time) field. The problem is that the text function of the bookmark returns the formula as text, but I just want to have the field result. How is that possible?

@jan.net

Please ZIP and attach your input Word document along with expected output TXT document. We will then provide you code example according to your requirement.

@tahir.manzoor
The example document is attached.
bookmark field example.zip (12.3 KB)

@jan.net

Please read the following article about extracting content from bookmark.
Extract Content from a Bookmark

Once you extract the content from bookmark, please use Document.Save method to save the document to TXT file format.

Document dstDoc = Common.GenerateDocument(doc, extractedNodesExclusive);
dstDoc.Save(MyDir + "21.3.txt", SaveFormat.Text);

@tahir.manzoor
Sorry for my late reply. The ExtractContent function runs in an NullReferenceException beacuse currNode.GetAncestor returns null. Should there always be a section?
It is not necessary to get the result in a textfile. I just want the text store in a variable for further processing. I think the NodeImporter (used in the GenerateDocument) is not the right way because it expects a source and destination document. How can I achieve this?

@jan.net

Please ZIP and attach your input Word document and bookmark name here for testing. We will investigate the issue and provide you more information on it.

@tahir.manzoor
The word document is already attachted in post #3. The name of the bookmark in this document is “Text_manuell”. I could fix the exception through a null check and the output now is the expected.

But that’s not what I want. As written in my previous answer I want the text hold in a variable for further processing.

@jan.net

To avoid the shared exception, please use the updated ExtractContentHelper code from here:
https://github.com/aspose-words/Aspose.Words-for-.NET

Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "bookmark field example.docx");
Bookmark bookmark = (Bookmark)doc.Range.Bookmarks["Text_manuell"];
List<Node> extractedNodesInclusive = ExtractContentHelper.ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
Document dstDoc = ExtractContentHelper.GenerateDocument(doc, extractedNodesInclusive);
String txt = dstDoc.ToString(SaveFormat.Text);
Console.WriteLine(txt);
1 Like

@tahir.manzoor
Thanks for your help.
I still added the following line to get the actual field value. Now it works as expected :wink:

dstDoc.UpdateFields();

@jan.net

Thanks for your feedback. It is nice to hear from you that your problem has been solved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.