I want to create pdf file from a word file. In that i am adding some content along with I have to add a icon which is clickable and open a link. it will be lengthy and it should hide. content should be like
(paperclip icon) Attachment file name
here paperclip icon should be clickable with link. i have tried code below but icon is coming(not in proper place) and not clickable while opening in chrome (it automatically open in chrome whiling licking the download file)
public static void attachmentAsLink(DocumentBuilder builder, Resource resource, String attachmentTitle, PDFAttachmentsProtoDataVO attachmentsProtoDataVO, AsposePrintOptionVo printOptionVo, boolean firstAttachment) {
try {
// builder.getFont().setColor(Color.blue);
builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
if (firstAttachment) {
builder.writeln();
builder.getFont().setBold(true);
// builder.getFont().setUnderline(Underline.SINGLE);
builder.writeln("Below are the links to Attachment/s -");
builder.getFont().setBold(false);
}
if (printOptionVo.getRequestFrom().equals(HttpRequestOrigin.ANNOTATION)) {
addAttachmentAsHyperLink(builder, resource, attachmentTitle, printOptionVo.getUser());
} else {
addingAttachmentIconForTrackingLinkLocation(builder, resource, attachmentTitle, attachmentsProtoDataVO);
}
} catch (Exception e) {
logger.error("error while adding attachment hyperlink for attachment title: {} and the resource path: {}",
CRLFCustomCleanser.replaceCRLF(attachmentTitle),
CRLFCustomCleanser.replaceCRLF(resource.getPath()),
e);
}
}
can you please hemp me generate proper code for open the link both in adobe reader and chrome browser and current the position.
NOTE: attachment will be more that one and it will be printed each line and each will have a different link with paper clip icon
@Gptrnt You can use the following code to insert image with a clickable link:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert image and assign a link to it.
Shape s = builder.insertImage("C:\\Temp\\test.png");
s.setHRef("https://metrics.aspose.com/");
doc.save("C:\\Temp\\out.pdf");
Summary
The paper‑clip icon is created as a floating Shape. A floating shape does not take part in the paragraph layout, therefore it can overlap the following text and the hyperlink (HRef) attached to the shape is not activated in the generated PDF. Insert the image as an inline shape and then add the hyperlink.
Why it happens
new Shape(builder.getDocument(), ShapeType.IMAGE) creates a floating image.
A floating shape is positioned independently of the surrounding runs, so it can cover the text that follows.
When the shape overlaps the text, many PDF viewers ignore the HRef that is set on the shape, so the icon is not clickable.
Fix
private static void addAttachmentAsHyperLink(
DocumentBuilder builder, Resource resource,
String attachmentTitle, User user) throws Exception {
String secureUrl = AppConfiguration.getDefaultPropertyAsString(BASE_URL_KEY) +
"cimplyfive/apis/secure-file?token=" + user.getLinkToken() +
"&path=" + resource.getPath();
// Insert the paper‑clip icon as an inline image
Shape icon = builder.insertImage(PAPER_CLIP_ICON_PATH);
icon.setWidth(14);
icon.setHeight(14);
icon.setWrapType(WrapType.INLINE); // make it inline
icon.setHRef(secureUrl); // hyperlink on the icon
icon.setTarget("_blank");
// Space between the icon and the title (optional)
builder.write(" ");
// Insert the attachment title as a separate hyperlink (optional)
builder.getFont().setColor(Color.BLUE);
builder.getFont().setUnderline(Underline.SINGLE);
builder.insertHyperlink(attachmentTitle, secureUrl, false);
}
Steps to verify
Generate the document (PDF, DOCX, etc.) with the code above.
Open the file in a PDF viewer that supports external links (e.g., Adobe Reader, Edge, Chrome).
Click the paper‑clip icon – it should open the URL defined in secureUrl.
If the icon is still not clickable, provide a minimal reproducible sample (source code that creates the PDF and the resulting file) for further analysis.
Please validate this behaviour with your exact SDK version and a minimal repro, as support can vary by release.