Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java使用itext在现有pdf中设置本地文件超链接_Java_Pdf_Hyperlink_Itext - Fatal编程技术网

Java使用itext在现有pdf中设置本地文件超链接

Java使用itext在现有pdf中设置本地文件超链接,java,pdf,hyperlink,itext,Java,Pdf,Hyperlink,Itext,我试图在现有PDF中提供一个超链接,单击该链接将打开该文件。如何做到这一点 我尝试了以下代码,它对外部超链接(如)工作正常,但对本地文件超链接(如D:/intro.pdf)不起作用 我正在使用itext pdf库。 代码: String in = "D:/introduction.pdf"; String out = "D:/introduction.pdf"; try { PdfReader reader = new P

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

我尝试了以下代码,它对外部超链接(如)工作正常,但对本地文件超链接(如D:/intro.pdf)不起作用

我正在使用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)]
如上图所示(第2页intro.pdf的图片)


提前感谢。

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

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

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

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"); 这也不是工人。。。要么将URI与
setAnchor()
一起使用,要么将简单文件路径与
setRemoteGoto()
一起使用。请看一看示例,不要混合解决方案。让我试试这个示例。如果我正在创建新的pdf,那么它可以正常工作,但在现有的pdfi中不工作。请尝试以下方法:Chunk page1=新Chunk(“单独文档23213N”);第1页setRemoteGoto(“intro.pdf”,1);ColumnText.showTextAligned(画布,Element.ALIGN_左,新短语(第1页),10,10,0);
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();