简体   繁体   中英

Java Set local file Hyperlink in existing pdf using itext

I am trying to provide a hyper link in a existing PDF, which when clicked will open the file. How can this be done?

I have try following Code it work fine for external hyper link like http://www.google.com but not working for local file hyperlink like D:/intro.pdf .

i am using itext pdf library.

Code :

        String in = "D:/introduction.pdf";
        String out = "D:/introduction.pdf";

        try {
            PdfReader reader = new PdfReader(in);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfStamper stamper = new PdfStamper(reader, baos);


            PdfContentByte canvas=stamper.getOverContent(6);
            Chunk imdb = new Chunk("Local Link");
            imdb.setAnchor("http://www.google.com"); // this work
         // imdb.setAnchor("D://intro.pdf");  // this does not work

            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(imdb), 100, 10, 0);



            stamper.close();
            FileOutputStream fileOutputStream = new FileOutputStream(out);


            IOUtils.write(baos.toByteArray(), fileOutputStream);
        } catch (Exception e) {

        }

i have also try using Annotation as below :

                PdfAnnotation annotation;

                PdfName aa=new PdfName("test test");
                annotation = PdfAnnotation.createLink(stamper.getWriter(),
                        new Rectangle(50f, 750f, 180f, 800f),aa,PdfAction.gotoRemotePage("file:///D:/intro.pdf","1", false, true));


                annotation.setTitle("Click Here");

                stamper.addAnnotation(annotation, 1);

I have also try below code comment by @ Bruno Lowagie : [ it create link on given page but in intro.pdf file and when i click on link it on same page (intro.pdf)] 见下图
as per above image ( image of intro.pdf page number-2 )

                PdfReader reader1 = new PdfReader("D://introduction.pdf");
                PdfStamper stamper1 = new PdfStamper(reader1, new FileOutputStream("D://intro.pdf"));
                PdfAnnotation link1 = PdfAnnotation.createLink(stamper1.getWriter(),
                    new Rectangle(136, 780, 559, 806), PdfAnnotation.HIGHLIGHT_INVERT,
                    new PdfAction("D://introduction.pdf", 1));
                link1.setTitle("Click Here");
                stamper1.addAnnotation(link1, 2);
                stamper1.close();

Thanks in advance.

You need to specify the protocol. For web pages, your URI starts with http:// ; for files your URI should start with file:// .

However, as the file you want to link to is also a PDF file, you probably don't want to use the setAnchor() method. You should use the setRemoteGoto() method instead. See the MovieLinks2 example.

If you want to add a link to an existing document, this is how to do it:

PdfReader reader = new PdfReader("hello.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("hello_link.pdf"));
PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
    new Rectangle(36, 790, 559, 806), PdfAnnotation.HIGHLIGHT_INVERT,
    new PdfAction("hello.pdf", 1));
stamper.addAnnotation(link, 1);
stamper.close();

If you look inside the PDF document, you'll see that the new file named hello_link.pdf contains a Link annotation that refers to the old file hello.pdf:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM