phax / ph-pdf-layout

Java library for creating fluid page layouts with Apache PDFBox. Supporting multi-page tables, different page layouts etc.
Apache License 2.0
64 stars 11 forks source link

How to add Hyperlink ? #13

Closed rgarg-atheer closed 2 years ago

rgarg-atheer commented 4 years ago

Hi,

Thanks for the library, works like charm. is there a way i can add hyperlink ?

Thanks, Raghvendra

phax commented 4 years ago

Good question :) I never thought about this. Do you mean links to websites or Links within the document?

rgarg-atheer commented 4 years ago

Links to websites

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

martin19 commented 2 years ago

Hi, This has been open for a while, I'd be interested in looking into this/doing some research. PDFBox offers hyperlink capability (see here https://stackoverflow.com/questions/21021502/how-to-add-hyperlink-in-pdf-using-pdfbox). So I guess it is a matter of including it in pdf-layout. I think links should work (at least) for image and text objects - but since ph-pdf-layout implements a nice object hierarchy I think the best would be to make it work for any object (PLElement?) in the pdf document.

I think the API could be roughly something like:

new PLExternalLink (new PLText ("sometext", r10)).setHref("http://www.foo.com");

new PLExternalLink (new PLImage (ImageIO.read (ClassPathResource.getInputStream ("images/test1.jpg")), 50, 50)).setHref("http://www.foo.com");

Links in a pdf need to specify some rectangle in document coordinates. This rectangle would need to cover the wrapped element(s). Thus, the link element would need to compute its childrens bounding box and accordingly set the link rectangle. I'd call the object PLExternalLink because as you mention above there might be also in-document links which share some characteristics (clickable bounding box) but differ in others (need link target inside the same document).

I'd be happy to discuss further.

phax commented 2 years ago

@martin19 yes indeed, it took some time ;-)

This is the output created by the test file https://github.com/phax/ph-pdf-layout/blob/master/src/test/java/com/helger/pdflayout/element/link/PLExternalLinkTest.java : basic.pdf

I hope it suits your needs. If so, I can build a 6.0.1 release.

The only open issue is imho, if the annotion link should be rendered BEFORE the main content or AFTER the main content. Currently it is done afterwards.

martin19 commented 2 years ago

@phax really cool, I didn't realize you were actually working on this already. Thank you!

phax commented 2 years ago

As you can see on the previous commits, I did some "tries" but never took the time to integrate it. Now it's in :)

phax commented 2 years ago

Release 6.0.1 is on its way to Maven Central. Happy PDF creation :)