使用Itext链接Portable Collection的pdf [英] Linking pdfs of Portable Collection using Itext

查看:73
本文介绍了使用Itext链接Portable Collection的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可移植的收藏夹,总共有两个pdf文档.我的要求是,如果我打开一个pdf文档并单击链接或任何文本,它应该重定向到该可移植Collection的另一个pdf.

I have portable collection in that total of two pdf document is there. My requirement is that if I open one pdf document and click the link or any text, it should redirect to another pdf of that portable Collection.

例如:考虑一个可移植集合中有两个pdf,分别是Programming.pdf和Java.pdf.如果单击Programming.pdf中的任何特定文本或变量,则应将其重定向到Java.pdf.有可能...?.

For example: Consider there are two pdfs inside a portable collection namely Programming.pdf and Java.pdf. If I click any particular text or variable inside Programming.pdf, it should redirect to Java.pdf. Is it possible...?.

推荐答案

要在可移植集合中的文档之间导航,您必须采用嵌入式Go-To操作.它们特别包含一个 T arget字典,它实际上可以是目标字典链的起点,以在具有任意嵌入深度的集合成员之间导航.要在同级文档之间导航,您需要一个目标词典向上移至父文档,而另一目标词典又从该父文档向下移至所需的同级.有关详细信息,请参见. ISO 32000-2第12.6.4.4节嵌入式转到动作".

To navigate between documents in a portable collection, you have to employ Embedded Go-To actions. They in particular contain a Target dictionary which actually can be the start of a chain of target dictionaries to navigate between collection members with arbitrary embedding depths. To navigate between sibling documents, you need one target dictionary going up to the parent document containing another target dictionary going down again from that parent to the desired sibling. For details cf. ISO 32000-2 section 12.6.4.4 "Embedded Go-To actions".

在iText 5.x中,您可以构建如下的嵌入式goto动作:

In iText 5.x you build such embedded goto actions like this:

PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
sibbling.setEmbeddedFileName(linkId);
PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
Chunk chunk = new Chunk("Go to " + linkId + ".");
PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
dest.addFirst(new PdfNumber(0));
PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
chunk.setAction(action);
document.add(chunk);

(来自例如,以下测试创建了一个包含四个文档A,B,C和D的投资组合,它们全部包含彼此的链接:

For example the following test creates a portfolio with four documents A, B, C, and D which all contain links to each other:

public void testLinkToSibblingInPortfolio() throws IOException, DocumentException {
    Files.write(new File("portfolio-with-embedded-gotos.pdf").toPath(), createPdf(new String[] {"A", "B", "C", "D"}));
}

public byte[] createPdf(String[] allIds) throws DocumentException, IOException {
    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    document.open();
    document.add(new Paragraph("This document contains a collection of PDFs."));

    PdfCollection collection = new PdfCollection(PdfCollection.HIDDEN);
    PdfCollectionSchema schema = getCollectionSchema(); 
    collection.setSchema(schema);
    PdfCollectionSort sort = new PdfCollectionSort("TITLE");
    collection.setSort(sort);
    collection.setInitialDocument("A");
    writer.setCollection(collection);

    PdfFileSpecification fs;
    PdfCollectionItem item;
    for (String id : allIds) {
        fs = PdfFileSpecification.fileEmbedded(writer, null,
            String.format("%s.pdf", id),
            createPage(id, allIds));
        fs.addDescription(id, false);

        item = new PdfCollectionItem(schema);
        item.addItem("TITLE", id);
        fs.addCollectionItem(item);
        writer.addFileAttachment(fs);
    }

    document.close();
    return baos.toByteArray();
}

public byte[] createPage(String id, String[] allIds) throws DocumentException, IOException {
    Document document = new Document();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter.getInstance(document, baos);
    document.open();

    Paragraph p = new Paragraph(id,
        FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED, 48));
    document.add(p);

    for (String linkId : allIds) {
        if (!id.equals(linkId)) {
            PdfTargetDictionary sibbling = new PdfTargetDictionary(true);
            sibbling.setEmbeddedFileName(linkId);
            PdfTargetDictionary parent = new PdfTargetDictionary(sibbling);
            Chunk chunk = new Chunk("Go to " + linkId + ".");
            PdfDestination dest = new PdfDestination(PdfDestination.XYZ, -1, -1, 0);
            dest.addFirst(new PdfNumber(0));
            PdfAction action = PdfAction.gotoEmbedded(null, parent, dest, false);
            chunk.setAction(action);
            document.add(chunk);
        }
    }

    document.close();
    return baos.toByteArray();
}

private static PdfCollectionSchema getCollectionSchema() {
    PdfCollectionSchema schema = new PdfCollectionSchema();

    PdfCollectionField size = new PdfCollectionField("File size", PdfCollectionField.SIZE);
    size.setOrder(2);
    schema.addField("SIZE", size);

    PdfCollectionField filename = new PdfCollectionField("File name", PdfCollectionField.FILENAME);
    filename.setVisible(false);
    schema.addField("FILE", filename);

    PdfCollectionField title = new PdfCollectionField("Title", PdfCollectionField.TEXT);
    title.setOrder(0);
    schema.addField("TITLE", title);

    return schema;
}

(

(EmbeddedLinks test testLinkToSibblingInPortfolio and helpers createPdf, createPage, and getCollectionSchema)

这实际上是从iText示例中借来的很多 KubrickMovies .

This actually borrows a lot from the iText examples KubrickBox, KubrickCollection, and KubrickMovies.

使用当前的iText 5开发版本5.5.14-SNAPSHOT测试.

这篇关于使用Itext链接Portable Collection的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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