Free Support Forum - aspose.com

Bookmark start is created in one node and bookmark end is created in another node

Hi,

I tried creating bookmarks in my document.

builder.startBookmark((String)row.getAttribute(“UniqueId”));
builder.writeln((String)row.getAttribute(“Title”));
builder.endBookmark((String)row.getAttribute(“UniqueId”));

Attached the generated document for reference.
This document has bookmark end tags in other nodes rather than in the node where bookmark start exist.

REP_10036_10020_1.docx.zip (16.1 KB)

@mvpraveen88,

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

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

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

Attached a sample standalone application.

Data folder has input document.

Output folder has generated output document in docx format.
If we save this generated docx file in xml format, we observe that book mark start nodes begin at the place we have inserted them, but the bookmark end nodes is inserted in a new paragraph node.

We want to close the bookmark end tag in the same node.

BookMarkSample.zip (74.4 KB)

@mvpraveen88,

Please check if the following code is acceptable for you?

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Document ipDoc = new Document("D:\\BookMarkSample\\input.docx");

Style newStyle;
for(int i=1;i<=9;i++){
    newStyle = doc.getStyles().addCopy(doc.getStyles().get("Heading "+i));
    newStyle.setName("SecTitle"+i);
    newStyle = doc.getStyles().addCopy(doc.getStyles().get("Heading "+i));
    newStyle.setName("ClauseTitle"+i);
}
builder.insertTableOfContents("\\TOC \\h \\z \\t \"SecTitle1,1,SecTitle2,2,SecTitle3,3,SecTitle4,4,SecTitle5,5,SecTitle6,6,SecTitle7,7,SecTitle8,8,SecTitle9,9,ClauseTitle1,1,ClauseTitle2,2,ClauseTitle3,3,ClauseTitle4,4,ClauseTitle5,5,ClauseTitle6,6,ClauseTitle7,7,ClauseTitle8,8,ClauseTitle9,9\" ");
builder.insertBreak(BreakType.PAGE_BREAK);

builder.getParagraphFormat().setStyle(doc.getStyles().get("SecTitle1"));
builder.startBookmark("HEAD_101");
builder.write("Header 1");
builder.endBookmark("HEAD_101");
builder.writeln();

builder.getParagraphFormat().setStyle(doc.getStyles().get("ClauseTitle1"));
builder.startBookmark("SUB_HEAD_1001");
builder.write("SubHeader 1");
builder.endBookmark("SUB_HEAD_1001");
builder.writeln();

builder.startBookmark("TEXT:: SUB_HEAD_1001");
builder.insertDocument(ipDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
builder.endBookmark("TEXT:: SUB_HEAD_1001");

doc.updateFields();

doc.save("D:\\BookMarkSample\\awjava-18.9-new.docx");

Hi hafeez,

Thanks for your prompt reply.
We have tried this approach… If we observe this output document also… bookmark start is in paragraph node and bookmark end is out side paragraph node…
Also writing a new line doesnt help us… because we will be using this document to upload with some changes…
When the document gets uploaded we will highlight the differences between existing and uploaded document… So this new line is also displayed as a difference when uploaded without any change in data also…

thanks,
Praveen.

@mvpraveen88,

Thanks for the details. Please check if the following new code is acceptable for you?

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Document ipDoc = new Document("D:\\BookMarkSample\\input.docx");

Style newStyle;
for(int i=1;i<=9;i++){
    newStyle = doc.getStyles().addCopy(doc.getStyles().get("Heading "+i));
    newStyle.setName("SecTitle"+i);
    newStyle = doc.getStyles().addCopy(doc.getStyles().get("Heading "+i));
    newStyle.setName("ClauseTitle"+i);
}
builder.insertTableOfContents("\\TOC \\h \\z \\t \"SecTitle1,1,SecTitle2,2,SecTitle3,3,SecTitle4,4,SecTitle5,5,SecTitle6,6,SecTitle7,7,SecTitle8,8,SecTitle9,9,ClauseTitle1,1,ClauseTitle2,2,ClauseTitle3,3,ClauseTitle4,4,ClauseTitle5,5,ClauseTitle6,6,ClauseTitle7,7,ClauseTitle8,8,ClauseTitle9,9\" ");
builder.insertBreak(BreakType.PAGE_BREAK);

builder.getParagraphFormat().setStyle(doc.getStyles().get("SecTitle1"));
BookmarkStart bmStart1 = builder.startBookmark("HEAD_101");
builder.writeln("Header 1");
BookmarkEnd end1 = builder.endBookmark("HEAD_101");
// force insertion of BookmarkEnd at the end of same Paragraph (this BookmarkStart belongs to)
bmStart1.getParentNode().insertAfter(end1, bmStart1.getParentNode().getLastChild());

builder.getParagraphFormat().setStyle(doc.getStyles().get("ClauseTitle1"));
BookmarkStart bmStart2 = builder.startBookmark("SUB_HEAD_1001");
builder.writeln("SubHeader 1");
BookmarkEnd end2= builder.endBookmark("SUB_HEAD_1001");

bmStart2.getParentNode().insertAfter(end2, bmStart2.getParentNode().getLastChild());

builder.startBookmark("TEXT:: SUB_HEAD_1001");
builder.insertDocument(ipDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
builder.endBookmark("TEXT:: SUB_HEAD_1001");

doc.updateFields();

doc.save("D:\\BookMarkSample\\awjava-18.9.docx");