Portable collections

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

PortableCollection.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27063677/use-of-relative-path-for-anchor-method-using-itext-for-pdf-generation
  11.  */
  12. package com.itextpdf.samples.sandbox.collections;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.kernel.pdf.collection.PdfCollection;
  17. import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class PortableCollection extends GenericTest {
  29.     public static final String DATA
  30.             = "./src/test/resources/data/united_states.csv";
  31.     public static final String DEST
  32.             = "./target/test/resources/sandbox/collections/portable_collection.pdf";
  33.     public static final String HELLO
  34.             = "./src/test/resources/pdfs/hello.pdf";
  35.     public static final String IMG =
  36.             "./src/test/resources/img/berlin2013.jpg";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new PortableCollection().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  47.         Document doc = new Document(pdfDoc);
  48.         doc.add(new Paragraph("Portable collection"));
  49.         PdfCollection collection = new PdfCollection();
  50.         collection.setView(PdfCollection.TILE);
  51.         pdfDoc.getCatalog().setCollection(collection);
  52.         pdfDoc.addFileAttachment("united_states.csv",
  53.                 PdfFileSpec.createEmbeddedFileSpec(pdfDoc, DATA, "united_states.csv", "united_states.csv",
  54.                         null, null, false));
  55.         pdfDoc.addFileAttachment("hello.pdf",
  56.                 PdfFileSpec.createEmbeddedFileSpec(pdfDoc, HELLO, "hello.pdf", "hello.pdf",
  57.                         null, null, false));
  58.         pdfDoc.addFileAttachment("berlin2013.jpg",
  59.                 PdfFileSpec.createEmbeddedFileSpec(pdfDoc, IMG, "berlin2013.jpg", "berlin2013.jpg",
  60.                         null, null, false));
  61.         doc.close();
  62.     }
  63. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now