Portable collections

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

7th November 2015
admin-marketing

Switch code for this example

PortableCollection.java
  1. package sandbox.collections;
  2.  
  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Paragraph;
  6. import com.itextpdf.text.pdf.PdfFileSpecification;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8. import com.itextpdf.text.pdf.collection.PdfCollection;
  9.  
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import sandbox.WrapToTest;
  15.  
  16. @WrapToTest
  17. public class PortableCollection {
  18.  
  19.     public static final String DEST = "results/collections/portable_collection.pdf";
  20.     public static final String DATA = "resources/data/united_states.csv";
  21.     public static final String HELLO = "resources/pdfs/hello.pdf";
  22.     public static final String IMG = "resources/images/berlin2013.jpg";
  23.    
  24.     public static void main(String[] args) throws IOException,
  25.             DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new PortableCollection().createPdf(DEST);
  29.     }
  30.    
  31.  
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         document.add(new Paragraph("Portable collection"));
  37.         PdfCollection collection = new PdfCollection(PdfCollection.TILE);
  38.         writer.setCollection(collection);
  39.         PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(
  40.                 writer, DATA, "united_states.csv", null);
  41.         writer.addFileAttachment("united_states.csv", fileSpec);
  42.         fileSpec = PdfFileSpecification.fileEmbedded(
  43.                 writer, HELLO, "hello.pdf", null);
  44.         writer.addFileAttachment("hello.pdf", fileSpec);
  45.         fileSpec = PdfFileSpecification.fileEmbedded(
  46.                 writer, IMG, "berlin2013.jpg", null);
  47.         writer.addFileAttachment("berlin2013.jpg", fileSpec);
  48.         document.close();
  49.     }
  50. }
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