Working with Portfolio
5 Dec 20247 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
You can create a portfolio using PdfPortfolioInformation class and attach a variety of documents using PdfAttachment class. The following code example illustrates this.
//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 the attachment
FileStream pdfStream = new FileStream("CorporateBrochure.pdf", FileMode.Open, FileAccess.Read);
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
pdfFile.FileName = "CorporateBrochure.pdf";
//Set the startup document to view
document.PortfolioInformation.StartupDocument = pdfFile;
//Add the attachment to the document
document.Attachments.Add(pdfFile);
//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the content type for PDF file
string contentType = "application/pdf";
//Define the file name.
string fileName = "Sample.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//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 the attachment
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf");
pdfFile.FileName = "CorporateBrochure.pdf";
//Set the startup document to view
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);
'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
'Create the attachment
Dim pdfFile As New PdfAttachment("CorporateBrochure.pdf")
pdfFile.FileName = "CorporateBrochure.pdf"
'Set the startup document to view
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)
You can download a complete working sample from GitHub.
Extracting file from PDF Portfolio
The Essential® PDF provides support for extracting the files from the PDF Portfolio using Attachments property of PdfLoadedDocument class and saving the files to the disk. The following code sample shows the steps to extract files from PDF Portfolio.
//Load an existing PDF document
FileStream docStream = new FileStream("Sample.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Iterate the attachments
foreach (PdfAttachment attachment in document.Attachments)
{
//Extract the attachment and save to the disk
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
s.Write(attachment.Data, 0, attachment.Data.Length);
s.Dispose();
}
//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the content type for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Load an existing PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Sample.pdf");
//Iterate the attachments
foreach (PdfAttachment attachment in document.Attachments)
{
//Extract the attachment and save to the disk
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
s.Write(attachment.Data, 0, attachment.Data.Length);
s.Dispose();
}
//Save and close the document
document.Save("Output.pdf");
document.Close(true);
'Load the PDF document
Dim document As New PdfLoadedDocument("Sample.pdf")
'Iterate the attachments
For Each attachment As PdfAttachment In document.Attachments
'Extracting the attachment and saving into the local disk
Dim s As New FileStream(attachment.FileName, FileMode.Create)
s.Write(attachment.Data, 0, attachment.Data.Length)
s.Dispose()
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.
//Load the PDF document
FileStream docStream = new FileStream("Sample.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Remove the file from the Portfolio
document.Attachments.RemoveAt(0);
//Save and close the document
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the content type for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//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();
'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.