iText PDF:如何在PDF中创建指向其他部分的内部链接 [英] iText PDF : How to create internal links to other sections in PDF

查看:312
本文介绍了iText PDF:如何在PDF中创建指向其他部分的内部链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用itext 5生成pdf文件,并使用Anchor创建指向pdf中不同页面的内部链接.链接可以正常工作,但是,在Adobe PDF Reader中,将鼠标指针放在链接的底部边缘时,"W"出现在手动"工具的顶部,并且在单击时会在Web浏览器中打开一个新文件,但不会重定向到链接页面.这是示例代码.请建议如何禁用在浏览器中打开内部链接.

I'm using itext 5 to generate a pdf file and using Anchor to create internal links to different pages inside pdf. links are working fine but, In adobe PDF reader when mouse pointer is placed at the bottom edge of the link,'W' appears on top of Hand tool and when clicked it opens a new file in web browser but it is not redirecting to the linked page. Here is a sample code. Please suggest how to disable internal links opening in browser.

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;


public class AnchorExample {

public static void main(String[] args) {

Document document = new Document();

try {
    PdfWriter.getInstance(document,
            new FileOutputStream("Anchor2.pdf"));

    document.open();


        Anchor anchor =
        new Anchor("Jump down to next paragraph");
        anchor.setReference("#linkTarget");
        Paragraph paragraph = new Paragraph();
        paragraph.add(anchor);
        document.add(paragraph);

        document.newPage();

        Anchor anchorTarget =
        new Anchor("This is the target of the link above");
        anchorTarget.setName("linkTarget");
        Paragraph targetParagraph = new Paragraph();
        targetParagraph.setSpacingBefore(50);

        targetParagraph.add(anchorTarget);
        document.add(targetParagraph);


    document.close();
} catch (DocumentException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

}
}

推荐答案

使用Anchor进行内部链接是过去的事情.我看到您使用的是iText的较早版本,因为Anchor类(直到iText 5才可用)已由Link类(在iText 7中引入)代替.请参阅 iText概述7个构建模块类.

Using Anchor for internal links is something of the old days. I see that you're using an ancient version of iText, since the Anchor class (available up until iText 5) was replaced by the Link class (introduced in iText 7). See the overview of the iText 7 building block classes.

即使在iText 5中,也不常用Anchor作为本地目标.最好使用Chunk代替Anchor,使用setLocalDestination()方法定义目标,并使用setLocalGoto()方法定义链接:

Even in iText 5, it's not common to use Anchor for local destinations. It is better to use a Chunk instead of an Anchor, to use the setLocalDestination() method to define the target, and to use the setLocalGoto() method to define the link:

Chunk chunk = new Chunk("Contact information");
chunk.setLocalGoto("contact");  
document.add(new Paragraph(chunk));
document.newPage();
chunk chunk1 = new Chunk("Contact information");
chunk1.setLocalDestination("contact");
Chapter chapter = new Chapter(new Paragraph(chunk1),1);    
chapter.setNumberDepth(0);
document.add(chapter);

另请参阅几乎重复的问题使用itext java将锚点添加到pdf .就像您从问题而不是从答案复制代码一样(这确实很奇怪).

See also the almost duplicate question Add anchor to pdf using itext java. It's as if you copied your code from the question instead of from the answer (which is really strange).

这篇关于iText PDF:如何在PDF中创建指向其他部分的内部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆