boxable icon indicating copy to clipboard operation
boxable copied to clipboard

Support <a href> tag in cell content for URLs

Open flurinBoonea opened this issue 9 years ago • 7 comments

Some of the HTML tags are supported (eg. <p>,<i>,<b>,<br>,<ul>,<ol>,<li>).

Would it be possible to support links in table cells ? Or to be more specific the <a href=...> tag ?

flurinBoonea avatar Sep 21 '16 15:09 flurinBoonea

I tried to add a label "enhancement" but can't seem to figure it out ;)

flurinBoonea avatar Sep 21 '16 16:09 flurinBoonea

I think that wouldn't be so hard to implement. I will do tomorrow some quick tests and then give you more info about this issue.

Frulenzo avatar Sep 21 '16 19:09 Frulenzo

Okey, I have little test case how PDFBox handle URLs in the PDF. I must point out that it is not (not!) test case with already implemented a href support in the Boxable (table)

@Test
    public void SampleTest10() throws IOException {
        PDRectangle pageSize = PDRectangle.A4;
        PDPage page = new PDPage(pageSize);
        PDDocument doc = new PDDocument();
        doc.addPage(page);

        PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
        borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
        PDAnnotationLink txtLink = new PDAnnotationLink();
        txtLink.setBorderStyle(borderULine);

        PDActionURI action = new PDActionURI();
        action.setURI("www.google.com");
        txtLink.setAction(action);

        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(10);
        position.setLowerLeftY(pageSize.getUpperRightY() - 20); 
        position.setUpperRightX(100); 
        position.setUpperRightY(pageSize.getUpperRightY() - 10); 
        txtLink.setRectangle(position);
        PDColor color = new PDColor(new float[]{0, 0, 255}, PDDeviceRGB.INSTANCE);
        txtLink.setColor(color);
        page.getAnnotations().add(txtLink);

        PDPageContentStream cos = new PDPageContentStream(doc, page, true, true);
        cos.setNonStrokingColor(Color.BLUE);
        cos.beginText();
        cos.setFont(PDType1Font.HELVETICA, 12);
        cos.newLineAtOffset(10,pageSize.getUpperRightY() - 20);
        cos.showText("www.google.com");
        cos.endText();
        cos.closePath();
        cos.close();

        try {
            // Save the document
            File file = new File("target/SampleTest10.pdf");
            System.out.println("Sample file saved at : " + file.getAbsolutePath());
            Files.createParentDirs(file);
            doc.save(file);
            doc.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

PDF output : SampleTest10.pdf

Main problem is that many Oktoberfest are now open/beggining in the Germany :smile: but I will try to implement this enhancement asap. If someone has more time on his hands I put this quick example from above as a little guide.

Frulenzo avatar Sep 22 '16 07:09 Frulenzo

Thanks - agreed the test is great in terms of showing the URL capability of PDFBox but as of now doesn't show how to include a URL within of an actual table cell of boxable. Highly appreciate the quick feedback!

flurinBoonea avatar Sep 22 '16 12:09 flurinBoonea

BTW - happy to donate to a good cause for someone to implement this!

flurinBoonea avatar Oct 14 '16 11:10 flurinBoonea

Hmm, it starts to be problematic when you have really long a href value. For example

<a href="www.google.com">here is my ahref value that redirect you on google page</a> 

That would be definetly broken down in multiple lines and that cause pain with drawing proper "hyperlink rectangle"

Frulenzo avatar Oct 25 '16 14:10 Frulenzo

I did find a small workaround - PDF Viewer from Adobe will make any http:// or https:// text clickable - not very nice, but good enough for some use cases.

flurinBoonea avatar Nov 29 '16 23:11 flurinBoonea