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