CodeSharing
Posted on September 9, 2021
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>
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();
}
}
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();
}
}
Posted on September 9, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.