Working with Portfolio
17 Nov 202511 minutes to read
PDF Portfolios allows the user to bring together content from a variety of sources, including documents, drawings, images, e-mail, spreadsheets and web pages. Essential® PDF allows the user to create portfolios and also to extract the files from them.
Creating a PDF portfolio
A PDF portfolio enables the embedding of multiple files within a single PDF container, with each file represented as a PdfAttachment. Attachments can include metadata such as file name, description, creation and modification dates, MIME type, and a relationship type. Use the PdfAttachment class to define each embedded file, while portfolio behavior such as specifying a startup document is configured through the documents PdfPortfolioInformation.
The following code example illustrates this.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
//Create a new instance of PdfDocument class
PdfDocument document = new PdfDocument();
//Create a new portfolio
document.PortfolioInformation = new PdfPortfolioInformation();
//Set the view mode of the portfolio
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile;
// Create a new PDF attachment using the file stream
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
// Set the name of the attachment file
pdfFile.FileName = "CorporateBrochure.pdf";
// Provide a description for the attachment
pdfFile.Description = "This is a PDF document";
// Set the creation date of the attachment
pdfFile.CreationDate = DateTime.Now;
// Specify the MIME type of the attachment (important for identifying file type)
pdfFile.MimeType = "application/pdf";
// Optionally, set the modification date if needed
pdfFile.ModificationDate = DateTime.Now;
// Optionally, set the relationship (e.g., "Data", "Source", etc.)
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified;
// Add the attachment to the document's attachment collection
document.Attachments.Add(pdfFile);
// Set this attachment as the startup document in the PDF portfolio
document.PortfolioInformation.StartupDocument = pdfFile;
//Add the attachment to the document
document.Attachments.Add(pdfFile);
//Save and close the document.
document.Save("Sample.pdf");
document.Close(true);using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
//Create a new instance of PdfDocument class
PdfDocument document = new PdfDocument();
//Create a new portfolio
document.PortfolioInformation = new PdfPortfolioInformation();
//Set the view mode of the portfolio
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile;
// Create a new PDF attachment using the file stream
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
// Set the name of the attachment file
pdfFile.FileName = "CorporateBrochure.pdf";
// Provide a description for the attachment
pdfFile.Description = "This is a PDF document";
// Set the creation date of the attachment
pdfFile.CreationDate = DateTime.Now;
// Specify the MIME type of the attachment (important for identifying file type)
pdfFile.MimeType = "application/pdf";
// Optionally, set the modification date if needed
pdfFile.ModificationDate = DateTime.Now;
// Optionally, set the relationship (e.g., "Data", "Source", etc.)
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified;
// Add the attachment to the document's attachment collection
document.Attachments.Add(pdfFile);
// Set this attachment as the startup document in the PDF portfolio
document.PortfolioInformation.StartupDocument = pdfFile;
//Add the attachment to the document
document.Attachments.Add(pdfFile);
//Save and close the document.
document.Save("Sample.pdf");
document.Close(true);Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing
'Create a new instance of PdfDocument class
Dim document As New PdfDocument()
'Create a new portfolio
document.PortfolioInformation = New PdfPortfolioInformation()
'Set the view mode of the portfolio
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile
' Load the PDF file stream to be attached
Dim pdfStream As New FileStream("CorporateBrochure.pdf", FileMode.Open, FileAccess.Read)
' Create a new PDF attachment using the file stream
Dim pdfFile As New PdfAttachment("CorporateBrochure.pdf", pdfStream)
' Set the name of the attachment file
pdfFile.FileName = "CorporateBrochure.pdf"
' Provide a description for the attachment
pdfFile.Description = "This is a PDF document"
' Set the creation date of the attachment
pdfFile.CreationDate = DateTime.Now
' Specify the MIME type of the attachment (important for identifying file type)
pdfFile.MimeType = "application/pdf"
' Optionally, set the modification date if needed
pdfFile.ModificationDate = DateTime.Now
' Optionally, set the relationship (e.g., "Data", "Source", etc.)
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified
' Set this attachment as the startup document in the PDF portfolio
document.PortfolioInformation.StartupDocument = pdfFile
' Add the attachment to the document's attachment collection
document.Attachments.Add(pdfFile)
'Save and close the document.
document.Save("Sample.pdf")
document.Close(True)You can download a complete working sample from GitHub.
Extracting file from PDF Portfolio
Files embedded in a PDF portfolio can be extracted using the Attachments property of the PdfLoadedDocument class. Each attachment can be accessed, read, and saved to disk. Metadata such as file name and MIME type can also be retrieved during the extraction process.
The following code demonstrates how to iterate through the attachments in a PDF portfolio.
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
//Load an existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Iterate through all attachments in the PDF document
foreach (PdfAttachment attachment in document.Attachments)
{
// Create a file stream to save the attachment to disk using its original file name
using (FileStream s = new FileStream(attachment.FileName, FileMode.Create))
{
// Write the attachment data to the file
s.Write(attachment.Data, 0, attachment.Data.Length);
}
// Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
string mimeType = attachment.MimeType;
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}");
// Optional: Access additional metadata if needed
DateTime creationDate = attachment.CreationDate;
DateTime modificationDate = attachment.ModificationDate;
string description = attachment.Description;
PdfAttachmentRelationship relationship = attachment.Relationship;
// Log or use the metadata as needed
Console.WriteLine($"Description: {description}");
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}");
Console.WriteLine($"Relationship: {relationship}");
}
//Save and close the document.
document.Save("Sample.pdf");
document.Close(true);using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
//Load an existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Sample.pdf");
// Iterate through all attachments in the PDF document
foreach (PdfAttachment attachment in document.Attachments)
{
// Create a file stream to save the attachment to disk using its original file name
using (FileStream s = new FileStream(attachment.FileName, FileMode.Create))
{
// Write the attachment data to the file
s.Write(attachment.Data, 0, attachment.Data.Length);
}
// Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
string mimeType = attachment.MimeType;
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}");
// Optional: Access additional metadata if needed
DateTime creationDate = attachment.CreationDate;
DateTime modificationDate = attachment.ModificationDate;
string description = attachment.Description;
PdfAttachmentRelationship relationship = attachment.Relationship;
// Log or use the metadata as needed
Console.WriteLine($"Description: {description}");
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}");
Console.WriteLine($"Relationship: {relationship}");
}
//Save and close the document
document.Save("Output.pdf");
document.Close(true);Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing
'Load the PDF document
Dim document As New PdfLoadedDocument("Sample.pdf")
' Iterate through all attachments in the PDF document
For Each attachment As PdfAttachment In document.Attachments
' Create a file stream to save the attachment to disk using its original file name
Using s As New FileStream(attachment.FileName, FileMode.Create)
' Write the attachment data to the file
s.Write(attachment.Data, 0, attachment.Data.Length)
End Using
' Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
Dim mimeType As String = attachment.MimeType
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}")
' Optional: Access additional metadata if needed
Dim creationDate As DateTime = attachment.CreationDate
Dim modificationDate As DateTime = attachment.ModificationDate
Dim description As String = attachment.Description
Dim relationship As PdfAttachmentRelationship = attachment.Relationship
' Log or use the metadata as needed
Console.WriteLine($"Description: {description}")
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}")
Console.WriteLine($"Relationship: {relationship}")
Next
'Save and close the document
document.Save("Output.pdf")
document.Close(True)You can download a complete working sample from GitHub.
Removing files from PDF Portfolio
The following code example illustrates how to remove files from an existing PDF portfolio using RemoveAt method or Remove method of PdfAttachmentCollection class.
using Syncfusion.Pdf.Parsing;
//Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Remove the file from the Portfolio
document.Attachments.RemoveAt(0);
//Save and close the document.
document.Save("Sample.pdf");
document.Close(true);using Syncfusion.Pdf.Parsing;
//Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Sample.pdf");
//Remove the file from the Portfolio
document.Attachments.RemoveAt(0);
//Save and close the document
document.Save("Output.pdf");
document.Close();Imports Syncfusion.Pdf.Parsing
'Load the PDF document
Dim document As New PdfLoadedDocument("Sample.pdf")
'Remove the file from the Portfolio
document.Attachments.RemoveAt(1)
'Save and close the document
document.Save("Output.pdf")
document.Close()You can download a complete working sample from GitHub.