Extracting Content using Bookmark is not working for Table

Hi All,

I am trying to extract content by using bookmark and generating document for each bookmark.

I have attached the sample document(SampleTable.xml) which contains 3 bookmarks.
Bookmark 3 is having a table whereas bookmark 1 and 2 are paragraphs . when I try to extract the content and generate the document for bookmark3 its not extracting the full table details. Instead its showing only the last two rows in the generated document attached(SampleTable_Bookmark3.xml) for reference.

Code Used for extract content using bookmark and generating document:

public static void main (String[] arg) throws Exception {
String sourceFile = “C:\TableDoc\SampleTable.xml”;
com.aspose.words.LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(LoadFormat.WORD_ML);
Document srcDoc = new Document(sourceFile, loadOptions);
BookmarkCollection bc = srcDoc.getRange().getBookmarks();
for (int i = 0; i < bc.getCount(); i++) {
Bookmark b = bc.get(i);
String bName = bc.get(i).getName();
ArrayList srcExtractedNodesInclusive = extractContent(b.getBookmarkStart(), b.getBookmarkEnd(), true
Document srcChoreoDoc = generateDocument(srcDoc, srcExtractedNodesInclusive);
srcChoreoDoc.save(“C:\TableDoc\SampleTable_”+bName+".xml");
}
}

Thanks,
Muthulakshmi M

Hi Muthulakshmi,


Thanks for your inquiry. You can safely use the following code as a workaround if the first node in the first Cell of first Row in Table is BookmarkStart node:

String sourceFile = getMyDir() + “SampleTable.xml”;
com.aspose.words.LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(LoadFormat.WORD_ML);
Document srcDoc = new Document(sourceFile, loadOptions);
BookmarkCollection bc = srcDoc.getRange().getBookmarks();
for (int i = 0; i < bc.getCount(); i++) {
Bookmark b = bc.get(i);
String bName = bc.get(i).getName();

Table tbl = (Table) b.getBookmarkStart().getAncestor(NodeType.TABLE);
if (tbl != null) {
Paragraph para = new Paragraph(srcDoc);
para.getParagraphBreakFont().setSize(1);
para.appendChild(b.getBookmarkStart());

tbl.getParentNode().insertBefore(para, tbl);
}

ArrayList srcExtractedNodesInclusive = extractContent(b.getBookmarkStart(), b.getBookmarkEnd(), true);
Document srcChoreoDoc = generateDocument(srcDoc, srcExtractedNodesInclusive);
srcChoreoDoc.save(getMyDir() + “SampleTable_”+bName+".xml");
}

Hope, this helps.

Best regards,

Hi,
I have problem to extract content if the bookmark is inside table cell.
The method extractContent return empty collection.

Hi Muthulakshmi,


Thanks for your inquiry and sorry for the delayed response.

You attached following documents in your first post:

SampleTable.xml
SampleTable_Boomark1.xml
SampleTable_Boomark2.xml
SampleTable_Boomark3.xml

Please provide more details with which document and what bookmark you’re getting this problem with (specify document and bookmark names)? Have you tried Aspose.Words for Java 16.10.0? Does that resolve your issue?

Best regards,

Hi,
Thanks for you response.
I will provide you with more details.
I have used the method “extractContentFromBookmark” to copy content of specified bookmark called grpBookmark into this same document at the end of this bookmark. This bookmark also include another bookmarks to fill the template.
This is something like this:
[grpBookmark_start]
someText[nameBookmark_start]name[nameBookmark_end],someText[surnameBookmark_start]surname[surnameBookmark_end][grpBookmark_end]
The problem is that the copied part of the document doesn’t contains the end tag of the last inner bookmark. I mean “surnameBookmark_end”. When I tried to remove this bookmark I got exception after calling bookmark.remove() or bookmark.setText(’’).

It seems for me that in XML file the [surnameBookmark_end] is after [grpBookmark_end].
<w:bookmarkStart w:id=“17” w:name=“grpBookmark”/>

<w:bookmarkStart w:id=“27” w:name=“surnameBookmark”/>

<w:bookmarkEnd w:id=“16”/>
<w:bookmarkEnd w:id=“17”/>
<w:bookmarkEnd w:id=“27”/>

What should be done to include also the end tag for the inner bookmark while copying.



Hi Muthulakshmi,


Firstly, please note that it is by MS Word design that a Word document must have unique names for bookmarks. You cannot insert two bookmarks with the same names.

Secondly, please attach the following resources here for testing:

  • Your input Word document containing bookmarks (grpBookmark, nameBookmark_start etc)
  • Your expected document showing the desired output (where extracted content is inserted at the bottom of input document) here for our reference. You can create expected document using Microsoft Word.

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

Best regards,

Hi awais.hafeez,
Please check documents included in zip archive “WordEngine.zip”.

BR

I have resolved my previous problem by changing the method to copy content of bookmark.
I have created a sample app to fill my template. (source_doc.docx)
In summary there is a table with some static text and one cell with bookmarks to created dynamic content.
[grp_start]
[entry_start]

[name_start]name[name_end], [surname_start]surname[surname_end] -
[city_start]city[city_end][delimiter_start]enter new line[delimiter_end]
[entry_start]
[grp_end]
The
source code fill in loop these bookmarks (name, surname, city,
delimiter) and copy content of bookmark entry to end of bookmark grp.
The last entry should be without delimiter (new line).
At the end the template bookmark definition should be removed.
To test the source code please put the aspose lib jar into folder lib (aspose_words-16.8.0-jdk16.jar).
The expected document should look like expected_doc.docx but my source generate something like generated_doc.docx.
The created rows are not ordered correclty. I don’t know why but copied bookmark replace the template bookmark.
How I should copy the content of bookmark entry to specific place at the end of bookmark grp?
How I can remove the template bookmark?

So, In summary I need to set text on some of the bookmarks in table cell and insert content of grp bookmark at the end of this bookmark in table cell. This action is in loop eg for many users.

Is there anybody to be able to help me?

Hi Jurek,


Jurek:
So, In summary I need to set text on some of the bookmarks in table cell and insert content of grp bookmark at the end of this bookmark in table cell. This action is in loop eg for many users.


I have fixed some errors in your document (see attachment in this post). Are you able to set content of individual bookmarks now?

Best regards,