A PDF portfolio is a collection of files assembled into a single PDF document. It serves as a comprehensive and interactive showcase of various types of content, such as documents, images, presentations, videos, and more. Unlike a traditional PDF document, a PDF portfolio allows you to present multiple files in a cohesive and organized manner, providing a seamless browsing experience for the viewer. In this article, we will demonstrate how to create a PDF portfolio and how to identify if a PDF is a portfolio in Python using Spire.PDF for Python.
Install Spire.PDF for Python
This scenario requires Spire.PDF for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.PDF
If you are unsure how to install, please refer to this tutorial: How to Install Spire.PDF for Python on Windows
Create a PDF Portfolio with Python
Spire.PDF for Python allows you to generate a PDF portfolio by adding files to a PDF using the PdfDocument.Collection.AddFile() method. Furthermore, you can organize the files within the PDF portfolio by adding folders using the PdfDocument.Collection.Folders.CreateSubfolder() method. The detailed steps are as follows.
- Specify the output file path and the folders where the files to be included in the PDF portfolio are located.
- Create a PdfDocument object.
- Iterate through the files in the first folder and add them to the PDF portfolio using the PdfDocument.Collection.AddFile() method.
- Iterate through the files in the second folder. For each file, create a separate folder within the PDF portfolio using the PdfDocument.Collection.Folders.CreateSubfolder() method, and then add the file to the corresponding folder using the PdfFolder.AddFile() method.
- Save the resulting PDF portfolio using the PdfDocument.SaveToFile() method.
- Python
from spire.pdf.common import * from spire.pdf import * import glob # Specify the folders where the files to be included in the PDF portfolio are located input_folder1 = "Folder1/*" input_folder2 = "Folder2/*" # Specify the output file path output_file = "CreatePDFPortfolio.pdf" # Create a PdfDocument object doc = PdfDocument() # Get the list of file paths in the first folder files1 = glob.glob(input_folder1) # Loop through the files in the list for i, file in enumerate(files1): # Add each file to the PDF portfolio doc.Collection.AddFile(file) # Get the list of file paths in the second folder files2 = glob.glob(input_folder2) # Loop through the files in the list for j, file in enumerate(files2): # Create a separate folder for each file folder = doc.Collection.Folders.CreateSubfolder(f"SubFolder{j + 1}") # Add the file to the folder folder.AddFile(file) # Save the resulting PDF portfolio to the specified file path doc.SaveToFile(output_file) # Close the PdfDocument object doc.Close()
Identify if a PDF is a Portfolio with Python
You can use the PdfDocument.IsPortfolio property to easily identify whether a PDF document is a portfolio or not. The detailed steps are as follows.
- Specify the input and output file paths.
- Create a PdfDocument object.
- Load a PDF document using the PdfDocument.LoadFromFile() method.
- Identify whether the document is a portfolio or not using the PdfDocument.IsPortfolio property.
- Save the result to a text file.
- Python
from spire.pdf.common import * from spire.pdf import * # Specify the input and output file paths input_file = "CreatePDFPortfolio.pdf" output_file = "IsPDFPortfolio.txt" # Create a PdfDocument object doc = PdfDocument() # Load a PDF document doc.LoadFromFile(input_file) # Identify whether the document is a portfolio or not if doc.IsPortfolio: st = "The document is a portfolio" else: st = "The document is not a portfolio" # Save the result to a text file with open(output_file, "w") as text_file: text_file.write(st) # Close the PdfDocument object doc.Close()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.