diff --git a/Document-Processing/PDF/PDF-Library/NET/Working-with-Portfolio.md b/Document-Processing/PDF/PDF-Library/NET/Working-with-Portfolio.md index 63960b248..3e6d717a8 100644 --- a/Document-Processing/PDF/PDF-Library/NET/Working-with-Portfolio.md +++ b/Document-Processing/PDF/PDF-Library/NET/Working-with-Portfolio.md @@ -11,7 +11,9 @@ PDF Portfolios allows the user to bring together content from a variety of sourc ## Creating a PDF portfolio -You can create a portfolio using [PdfPortfolioInformation](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.PdfPortfolioInformation.html) class and attach a variety of documents using [PdfAttachment](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Interactive.PdfAttachment.html) class. The following code example illustrates this. +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](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Interactive.PdfAttachment.html) class to define each embedded file, while portfolio behavior such as specifying a startup document is configured through the documents [PdfPortfolioInformation](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.PdfPortfolioInformation.html). + +The following code example illustrates this. {% tabs %} @@ -28,11 +30,23 @@ 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); +// 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"; -//Set the startup document to view +// 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 @@ -57,10 +71,23 @@ 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"); +// 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"; -//Set the startup document to view +// 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 @@ -85,13 +112,27 @@ 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") +' 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" -'Set the startup document to view +' 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 +' Add the attachment to the document's attachment collection document.Attachments.Add(pdfFile) 'Save and close the document. @@ -106,7 +147,9 @@ You can download a complete working sample from [GitHub](https://github.com/Sync ## Extracting file from PDF Portfolio -The Essential® PDF provides support for extracting the files from the PDF Portfolio using [Attachments](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html#Syncfusion_Pdf_Parsing_PdfLoadedDocument_Attachments) property of [PdfLoadedDocument](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html) class and saving the files to the disk. The following code sample shows the steps to extract files from PDF Portfolio. +Files embedded in a PDF portfolio can be extracted using the [Attachments](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html#Syncfusion_Pdf_Parsing_PdfLoadedDocument_Attachments) property of the [PdfLoadedDocument](https://help.syncfusion.com/cr/document-processing/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html) 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. {% tabs %} @@ -118,15 +161,28 @@ using Syncfusion.Pdf.Parsing; //Load an existing PDF document PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf"); -//Iterate the attachments +// Iterate through all attachments in the PDF document 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(); + // 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); @@ -141,15 +197,28 @@ using Syncfusion.Pdf.Parsing; //Load an existing PDF document PdfLoadedDocument document = new PdfLoadedDocument("Sample.pdf"); -//Iterate the attachments +// Iterate through all attachments in the PDF document 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(); + // 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); @@ -164,12 +233,25 @@ Imports Syncfusion.Pdf.Parsing 'Load the PDF document Dim document As New PdfLoadedDocument("Sample.pdf") -'Iterate the attachments +' Iterate through all attachments in the PDF document 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() + ' 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