【问题标题】:Java Set local file Hyperlink in existing pdf using itextJava使用itext在现有pdf中设置本地文件超链接
【发布时间】:2013-08-05 20:19:44
【问题描述】:

我正在尝试在现有 PDF 中提供超链接,单击该超链接将打开文件。如何做到这一点?

我尝试遵循代码,它适用于像 http://www.google.com 这样的外部超链接,但不适用于像 D:/intro.pdf 这样的本地文件超链接。 p>

我正在使用 itext pdf 库。

代码:

        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) {

        }

我也尝试使用如下注释:

                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);

我还尝试了@Bruno Lowagie 的以下代码评论:[它在给定页面上创建链接,但在 intro.pdf 文件中,当我单击同一页面上的链接时(intro.pdf) ]
根据上图(intro.pdf 页码-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();

提前致谢。

【问题讨论】:

    标签: java pdf hyperlink itext


    【解决方案1】:

    您需要指定协议。对于网页,您的 URI 以 http:// 开头;对于文件,您的 URI 应以 file:// 开头。

    但是,由于您要链接的文件也是 PDF 文件,您可能不想使用 setAnchor() 方法。您应该改用setRemoteGoto() 方法。请参阅MovieLinks2 示例。

    如果您想添加指向现有文档的链接,请执行以下操作:

    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();
    

    如果您查看 PDF 文档,您会看到名为 hello_link.pdf 的新文件包含引用旧文件 hello.pdf 的链接注释:

    【讨论】:

    • imdb.setRemoteGoto("file:///D:/intro.pdf","1");这也不起作用
    • 呃...要么使用带有setAnchor() 的URI,要么使用带有setRemoteGoto() 的简单文件路径。请看示例,不要混用解决方案。
    • 让我试试这个例子。
    • 如果我要创建新的 pdf,那么它可以正常工作,但不能在现有的 pdf 中工作
    • 我试过这个:Chunk page1 = new Chunk("separate document23213n"); page1.setRemoteGoto("intro.pdf", 1); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(page1), 10, 10, 0);
    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2013-06-18
    • 2019-08-05
    相关资源
    最近更新 更多