Hyperlink to bookmark needed to be changed

Hi there,
Please consider the scenario:
document1 is containing a bookmark = {sales}, and
document2 is also containing a bookmark = {sales},
document2 also has a hyperlink in it to the bookmark {sales}.

Then in merged document two bookmarks would be present = {sales(doc1), sales_0(doc2)} and document2’s hyperlink would point to sales bookmark which is now defined in document1 part. Since the document2’s bookmark is renamed to sales_0.

So, given this, we assumed that document2’s hyperlink should point to sales_0 as well and not the sales. So what we meant is - Is there a way to make changes in hyperlink target-bookmark name, if the bookmark name is changed while merging the documents.

So, document2’s hyperlink should point to document2’s {sales} bookmark even if it got renamed during merging process.

Thanks.

Hi Praneeth,

Thanks for your inquiry.

Please note that Aspose.Words tries to mimic
the same behavior as MS Word do. MS Word allows only unique names for
bookmarks. If you change the name of a bookmark to a name that already
exists in the document, no error will be given and only the first
bookmark will be stored when you save the document. When you add a
cloned Paragraph into the document, its bookmark will not be stored due
to same name.

The bookmark name will not change after merging document. If you want to change hyperlink target-bookmark name, please use following code example. I have attached the input and output document with this post for your kind reference.

Document doc = new Document(MyDir + "Test.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// In Text.docx, first field is hyperlink
Field field = ((FieldStart)doc.getChild(NodeType.FIELD_START, 0, true)).getField();
ArrayList nodes = new ArrayList();
Node currentnode = field.getStart();
while (currentnode != field.getSeparator())
{
    Node node = currentnode.getNextSibling();
    currentnode = node;
    if (node.getNodeType() == NodeType.RUN)
        nodes.add(node);
}
for (Node node :(Iterable)nodes)
{
    node.remove();
}
builder.moveTo(field.getSeparator());
builder.write(" HYPERLINK \\l \"bm2\" ");
doc.updateFields();
doc.save(MyDir + "Out.docx");