我有可移植的收藏夹,总共有两个pdf文档。我的要求是,如果我打开一个pdf文档并单击链接或任何文本,它应该重定向到该可移植Collection的另一个pdf。
例如:考虑一个可移植集合中有两个pdf,分别是Programming.pdf和Java.pdf。如果单击Programming.pdf中的任何特定文本或变量,则应将其重定向到Java.pdf。有可能...?。
答案 0 :(得分:0)
要在可移植集合中的文档之间导航,您必须采用嵌入式Go-To操作。它们特别包含一个 T arget字典,它实际上可以是目标字典链的开始,以在具有任意嵌入深度的集合成员之间导航。要在同级文档之间导航,您需要一个目标词典上移至父文档,而另一目标词典又从该父文档降到所需的同级。有关详细信息,请参见。 ISO 32000-2第12.6.4.4节“嵌入式转到动作”。
在iText 5.x中,您可以构建如下的嵌入式goto动作:
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);
(来自EmbeddedLinks助手createPage
)
例如,以下测试创建了一个包含四个文档A,B,C和D的投资组合,它们全部包含彼此的链接:
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测试testLinkToSibblingInPortfolio
和助手createPdf
,createPage
和getCollectionSchema
)
这实际上是从iText示例KubrickBox,KubrickCollection和KubrickMovies中借来的。
使用当前的iText 5开发版本5.5.14-SNAPSHOT测试。