Create PDF Portfolios in Java

A PDF portfolio can bundle multiple files inside a single PDF container while preserving each file in its original format.

Create a PDF portfolio

Use this example when you need to package several files into a PDF portfolio collection.

  1. Create a new PDF Document and enable its Collection.
  2. Create FileSpecification objects for each input file and set their descriptions.
  3. Add the files to the portfolio collection and save the output document.
public static void createPdfPortfolio(Path[] inputFiles, Path outputFile) {
    try (Document document = new Document()) {
        document.setCollection(new Collection());

        FileSpecification excel = new FileSpecification(inputFiles[0].toString());
        FileSpecification word = new FileSpecification(inputFiles[1].toString());
        FileSpecification image = new FileSpecification(inputFiles[2].toString());

        excel.setDescription("Excel File");
        word.setDescription("Word File");
        image.setDescription("Image File");

        document.getCollection().add(excel);
        document.getCollection().add(word);
        document.getCollection().add(image);

        document.save(outputFile.toString());
    }
}

Remove files from a PDF portfolio

Use this example when an existing PDF portfolio collection should be cleared.

  1. Open the source PDF Document.
  2. Delete the document collection entries.
  3. Save the cleaned output document.
public static void removeFilesFromPdfPortfolio(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.getCollection().delete();
        document.save(outputFile.toString());
    }
}