Чтобы перемещаться между документами в переносимой коллекции, вы должны использовать Встроенные действия Go-To .В частности, они содержат словарь arget T , который фактически может быть началом цепочки целевых словарей для навигации между членами коллекции с произвольной глубиной встраивания.Чтобы перемещаться между одноуровневыми документами, вам нужен один целевой словарь, идущий к родительскому документу, содержащий другой целевой словарь, снова переходящий от этого родителя к нужному брату.Подробности см.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 test testLinkToSibblingInPortfolio
и помощники createPdf
, createPage
и getCollectionSchema
)
Это на самом деле занимаетмножество из примеров iText KubrickBox , KubrickCollection и KubrickMovies .
Протестировано с текущей версией разработки iText 55.5.14-ПАНОРАМА.