【问题标题】:Access Internal PDF links from html text block inside pdf从 pdf 中的 html 文本块访问内部 PDF 链接
【发布时间】:2019-11-27 11:45:12
【问题描述】:

我正在使用 itext 5。我有一个带有 HTML 样式的字符串和一个指向 pdf 中第 2 章的链接。

String text = "<p><strong>Jack </strong>and <strong>Jill </strong>went up the hill, then down the hill, around the hill then to <a href="Chapter 2">Chater 2</a>.</p>";

我正在使用 HTMLWorker 将 html 解析为字符串,并使用带有 localGoto 的块设置第 2 章的本地目的地。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;

public class InternalLinkExample {

    public static void main(String[] args) {

        Document document = new Document();

        try {

            PdfWriter.getInstance(document, new FileOutputStream("InternalLink.pdf"));

            String text = "<p><strong>Jack </strong>and <strong>Jill </strong>went up the hill, then down the hill, around the hill then to <a href=#\"Chapter2\">Chater 2</a>.</p>";

            document.open();

            HTMLWorker htmlWorker = new HTMLWorker(document);
            try {
                htmlWorker.parse(new StringReader(text));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            document.newPage();

            Chunk chunk = new Chunk("Chapter 2 Jack");
            chunk.setLocalDestination("Chapter2");  
            document.add(chunk);

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

    }
}

当我使用 iText 生成带有给定字符串的 pdf 并在 adobe PDF 查看器中打开 PDF 内部链接时,它会引发安全警告并且无法打开。但是,当我使用谷歌浏览器打开 pdf 时,我可以访问链接。

我想使用 adobe pdf 查看器访问内部链接。所以请让我知道如何从 Html 字符串访问内部链接。另外,我正在升级到 Itext 7,如果该解决方案适用于 Itext 7,将会很有帮助。

【问题讨论】:

  • 你的问题不清楚。您是简单地将 HTML 转换为 PDF,还是仅使用 HTML 到元素的转换来对元素应用一些样式,同时自己生成文档?或者您是否修改现有文档?
  • @AlexeySubach 我只是使用 HTML 进行元素转换,以便在生成 pdf 时对元素应用一些样式。
  • @AlexeySubach 我已经用实际代码更新了这个问题。

标签: java pdf itext itext7


【解决方案1】:

iText 7 中的代码看起来与 iText 5 中的代码非常相似。确保将井号 (#) 包含在 href 属性值中(双引号内),即 &lt;a href="#Chapter2"&gt;

以下是如何在 iText 7 中生成指向锚点的链接的完整代码。生成的文档中的链接在 Acrobat 中可以正常工作。

PdfDocument pdfDocument = new PdfDocument(new PdfWriter("C:/path/to.pdf"));
Document document = new Document(pdfDocument);

String text = "<p><strong>Jack </strong>and <strong>Jill </strong>went up the hill, then down the hill, around the hill then to <a href=\"#Chapter2\">Chapter 2</a>.</p>";

List<IElement> elements = HtmlConverter.convertToElements(text);

for (IElement element : elements) {
    if (element instanceof IBlockElement) {
        document.add((IBlockElement) element);
    }
}

document.add(new AreaBreak());

Text chapterTitle = new Text("Chapter 2 Jack").setDestination("Chapter2");
document.add(new Paragraph(chapterTitle));

document.close();

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 2013-07-30
    • 2011-06-19
    • 1970-01-01
    • 2010-11-17
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    相关资源
    最近更新 更多