We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Field.Update does not Update Hyperlink Field using Java

Code:

FieldHyperlink field = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, true);
String getMyDir =  "C:\\Users\\admin\\Desktop\\";
field = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, true);
field.setAddress(getMyDir + "Screenshot_2.png");
field.setScreenTip("Open " + field.getAddress());
// field.setTarget("iframe_3");
field.setOpenInNewWindow(true);
field.isImageMap(false);
field.update();

builder.getDocument().updateFields();

And as a result, I get : (I create 2 the same links)

aspose_2.png (18.8 KB)
After when I pres F9 in MsWord I get correct text which present location to file instead reference not valid…
aspose_1.png (10.2 KB)
Aspose version 21.5

@aolo23

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

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

PS: To attach these resources, please zip and upload them.

full example

    private static void hyperlinks() throws Exception {
        Document doc = new Document();
        License lic = new License();
        lic.setLicense(System.getProperty("user.dir")+"/src/main/resources/Aspose.Words.Java.lic");

        doc.removeAllChildren();
        Section section = new Section(doc);
        doc.appendChild(section);
        DocumentBuilder builder = new DocumentBuilder(doc);

        Shape box = new Shape(doc, ShapeType.TEXT_BOX);
        box.setWidth(153);
        box.setHeight(150.0);
        box.setWrapType(WrapType.NONE);
        box.setTop(200);
        box.setLeft(100);
        box.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        box.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        box.appendChild(new Paragraph(builder.getDocument()));
        builder.insertNode(box);

        FieldHyperlink field = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, true);
        String getMyDir =  "C:\\Users\\admin\\Desktop\\";
        field.setAddress(getMyDir + "Screenshot_2.png");
        field.setScreenTip("Open " + field.getAddress());
        field.setOpenInNewWindow(true);
        field.isImageMap(false);
        field.update();

        builder.getDocument().updateFields();

        builder.moveTo(box.getLastParagraph());
        FieldHyperlink field2 = (FieldHyperlink) builder.insertField(FieldType.FIELD_HYPERLINK, true);
        field2.setAddress(getMyDir + "Screenshot_2.png");
        field2.setScreenTip("Open " + field.getAddress());
        field2.setOpenInNewWindow(true);
        field2.isImageMap(false);
        field2.update();

        OoxmlSaveOptions options = new OoxmlSaveOptions();
        options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT);
        builder.getDocument()
                .save(System.getProperty("user.dir") + File.separator + "hiperlinks_test_"+System.currentTimeMillis()+".docx", options);
    }

Current state:
In the out doc document, as a result, we can see the text Error! Hyperlink reference not valid but it still works, which mean when we press CTRL + click on this text it will open the target image.

Fix in MsWord:
After pres F9 text Error! Hyperlink reference not valid is changing into the directory of file (as it was set in code)

Expectation
The text should show tooltip correctly without additional refreshing ms word

@aolo23

We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-22360. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-22360) have been fixed in this Aspose.Words for .NET 21.7 update and this Aspose.Words for Java 21.7 update.

No, it wasn’t …
Working workaround …

Run visibleHyperlinkText = new Run(builder.getDocument(), "the same link text...");
Node node = box.getLastParagraph(), lastRun = null;
while (node != null && node.getNodeType() != NodeType.FIELD_SEPARATOR) {
     node = node.getPreviousSibling();
     if (lastRun == null && node.getNodeType() == NodeType.RUN) lastRun = node;
}
Optional.ofNullable(lastRun).ifPresent(Node::remove);
childParagraph.insertAfter(visibleHyperlinkText , node);

@aolo23

It is nice to hear from you that you have solved your issue.

We have tested the scenario using the code example shared in this thread and have not found any issue with output document.

If you are facing different issue, please ZIP and attach your input, output and expected output documents along with code example to reproduce your issue at our end. We will investigate the issue and provide you more information on it.