A PDF Portfolio is a collection of files that are gathered and saved into a PDF container. PDF portfolios can consist of multiple document formats including Word, PDF, Excel, PowerPoint or images, and they behave similarly to zip archives by enabling you to share collections of different documents as one PDF file.
This article will introduce how to create a PDF Portfolio using Java codes, extract files from the PDF Portfolio and detect if a PDF file is a PDF Portfolio. It’s worth mentioning that it needs a third-party library called Spire.PDF for Java to finish the operations above.
Before running codes, we need to create a development environment and then add a Jar file which is located at the “lib” folder of the library to Intellij IDEA. There are two methods we can use to add the Jar to IDEA. One is first getting the package of library from the official website, finding Spire.Pdf.jar in the “lib” folder and finally manually adding it to IDEA. The other is creating a Maven project in IDEA and then typing the following codes in the pom.xml file, finally clicking the button “Import Changes”.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>4.4.5</version>
</dependency>
</dependencies>
RUNNING CODES
Create a PDF Portfolio and add multiple files to it
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
public class CreatePDFPortfolioWithFiles {
public static void main(String[] args) {
String[] files = new String[] { "C:\\Users\\Test1\\Desktop\\Sample.docx", "C:\\Users\\Test1\\Desktop\\Sample.xlsx",
"C:\\Users\\Test1\\Desktop\\Sample.pdf","C:\\Users\\Test1\\Desktop\\Sample.pptx","C:\\Users\\Test1\\Desktop\\logo.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("output/PortfolioWithFiles.pdf", FileFormat.PDF);
pdf.dispose();
}
}Output
Detect if a PDF file is a PDF Portfolio
In this part, the PDF Portfolio including multiple files will be seen as a sample document, and now the following codes will detect if
it is a PDF Portfolio.
import com.spire.pdf.PdfDocument;
public class DetectPortfolio {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument doc = new PdfDocument();
//Load the PDF file
doc.loadFromFile("PortfolioWithFiles.pdf");
//Detect if the PDF is a portfolio
boolean value = doc.isPortfolio();
if (value)
{
System.out.println("The document is a portfolio.");
}
else
{
System.out.println("The document is not a portfolio.");
}
}
}Output
Extract files from a PDF Portfolio
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachment;
import java.io.*;
public class ExtractFilesFromPDFPortfolio {
public static void main(String[] args) throws IOException {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF file
pdf.loadFromFile("PortfolioWithFiles.pdf");
//Loop through the attachments in the file
for(PdfAttachment attachment : (Iterable<PdfAttachment>)pdf.getAttachments()){
//提取附件
String fileName = attachment.getFileName();
OutputStream fos = new FileOutputStream("extract/" + fileName);
fos.write(attachment.getData());
}
pdf.dispose();
}
}Output
No comments:
Post a Comment