DEV Community

Jane
Jane

Posted on

Create PDF Portfolio in Java

A PDF Portfolio can contain a wide range of file types created in different applications. For example, it can include Word documents, e-mail messages, spreadsheets and PowerPoint presentations. In this article, you will learn how to create a portfolio and add files/folders to it programmatically by using Spire.PDF for Java.

Import jar dependency (2 Methods)
● Download the Spire.PDF for Java API and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
● Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.pdf</artifactId>
      <version>4.8.7</version>
   </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Create portfolio and add files

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;

public class CreatePortfolioWithFiles {
    public static void main(String []args){

        String[] files = new String[] { "test.pdf", "WordDocument.docx", "ExcelDocument.xlsx","Presentation.pptx","cow.png" };

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        for (int i = 0; i < files.length; i++)
        {
            //Create a PDF Portfolio and add files to it
            pdf.getCollection().addFile(files[i]);
        }

        //Save the result file
        pdf.saveToFile("PortfolioWithFiles.pdf", FileFormat.PDF);
        pdf.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Portfolio1

Create portfolio and add folders

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.collections.PdfFolder;

public class CreatePortfolioWithFolders {
    public static void main(String []args){

        String[] files = new String[] { "test.pdf", "WordDocument.docx", "ExcelDocument.xlsx","Presentation.pptx","cow.png" };

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        for (int i = 0; i < files.length; i++)
        {
            //Create a portfolio and add folders to it
            PdfFolder folder = pdf.getCollection().getFolders().createSubfolder("folder" + i);
            folder.addFile(files[i]);
        }

        //Save the result file
        pdf.saveToFile("PortfolioWithFolders.pdf", FileFormat.PDF);
        pdf.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Portfolio2

Discussion (1)

Collapse
firent profile image
Ivan

You should have mentioned that Spire.PDF is a paid library:
"The trial version will add a red watermark to the generated documents, and only supports converting the first 10 pages to other formats (excluding image)."
"To help you evaluate our product without any watermarks or restrictions, we provide you with a one-month temporary license."