Skip to content

Commit a1def0f

Browse files
authored
Merge pull request #1698 from syncfusion-content/989629-ug
989629-ug: Updated the Portfolio related UG documentation to retrieve property from PDF attachement
2 parents 704b12c + 828824b commit a1def0f

File tree

1 file changed

+111
-29
lines changed

1 file changed

+111
-29
lines changed

Document-Processing/PDF/PDF-Library/NET/Working-with-Portfolio.md

Lines changed: 111 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ PDF Portfolios allows the user to bring together content from a variety of sourc
1111

1212
## Creating a PDF portfolio
1313

14-
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.
14+
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).
15+
16+
The following code example illustrates this.
1517

1618
{% tabs %}
1719

@@ -28,11 +30,23 @@ document.PortfolioInformation = new PdfPortfolioInformation();
2830
//Set the view mode of the portfolio
2931
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile;
3032

31-
//Create the attachment
32-
FileStream pdfStream = new FileStream("CorporateBrochure.pdf", FileMode.Open, FileAccess.Read);
33+
// Create a new PDF attachment using the file stream
3334
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
35+
// Set the name of the attachment file
3436
pdfFile.FileName = "CorporateBrochure.pdf";
35-
//Set the startup document to view
37+
// Provide a description for the attachment
38+
pdfFile.Description = "This is a PDF document";
39+
// Set the creation date of the attachment
40+
pdfFile.CreationDate = DateTime.Now;
41+
// Specify the MIME type of the attachment (important for identifying file type)
42+
pdfFile.MimeType = "application/pdf";
43+
// Optionally, set the modification date if needed
44+
pdfFile.ModificationDate = DateTime.Now;
45+
// Optionally, set the relationship (e.g., "Data", "Source", etc.)
46+
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified;
47+
// Add the attachment to the document's attachment collection
48+
document.Attachments.Add(pdfFile);
49+
// Set this attachment as the startup document in the PDF portfolio
3650
document.PortfolioInformation.StartupDocument = pdfFile;
3751

3852
//Add the attachment to the document
@@ -57,10 +71,23 @@ document.PortfolioInformation = new PdfPortfolioInformation();
5771
//Set the view mode of the portfolio
5872
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile;
5973

60-
//Create the attachment
61-
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf");
74+
// Create a new PDF attachment using the file stream
75+
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
76+
// Set the name of the attachment file
6277
pdfFile.FileName = "CorporateBrochure.pdf";
63-
//Set the startup document to view
78+
// Provide a description for the attachment
79+
pdfFile.Description = "This is a PDF document";
80+
// Set the creation date of the attachment
81+
pdfFile.CreationDate = DateTime.Now;
82+
// Specify the MIME type of the attachment (important for identifying file type)
83+
pdfFile.MimeType = "application/pdf";
84+
// Optionally, set the modification date if needed
85+
pdfFile.ModificationDate = DateTime.Now;
86+
// Optionally, set the relationship (e.g., "Data", "Source", etc.)
87+
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified;
88+
// Add the attachment to the document's attachment collection
89+
document.Attachments.Add(pdfFile);
90+
// Set this attachment as the startup document in the PDF portfolio
6491
document.PortfolioInformation.StartupDocument = pdfFile;
6592

6693
//Add the attachment to the document
@@ -85,13 +112,27 @@ document.PortfolioInformation = New PdfPortfolioInformation()
85112
'Set the view mode of the portfolio
86113
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile
87114

88-
'Create the attachment
89-
Dim pdfFile As New PdfAttachment("CorporateBrochure.pdf")
115+
' Load the PDF file stream to be attached
116+
Dim pdfStream As New FileStream("CorporateBrochure.pdf", FileMode.Open, FileAccess.Read)
117+
118+
' Create a new PDF attachment using the file stream
119+
Dim pdfFile As New PdfAttachment("CorporateBrochure.pdf", pdfStream)
120+
' Set the name of the attachment file
90121
pdfFile.FileName = "CorporateBrochure.pdf"
91-
'Set the startup document to view
122+
' Provide a description for the attachment
123+
pdfFile.Description = "This is a PDF document"
124+
' Set the creation date of the attachment
125+
pdfFile.CreationDate = DateTime.Now
126+
' Specify the MIME type of the attachment (important for identifying file type)
127+
pdfFile.MimeType = "application/pdf"
128+
' Optionally, set the modification date if needed
129+
pdfFile.ModificationDate = DateTime.Now
130+
' Optionally, set the relationship (e.g., "Data", "Source", etc.)
131+
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified
132+
' Set this attachment as the startup document in the PDF portfolio
92133
document.PortfolioInformation.StartupDocument = pdfFile
93134

94-
'Add the attachment to the document
135+
' Add the attachment to the document's attachment collection
95136
document.Attachments.Add(pdfFile)
96137

97138
'Save and close the document.
@@ -106,7 +147,9 @@ You can download a complete working sample from [GitHub](https://github.com/Sync
106147

107148
## Extracting file from PDF Portfolio
108149

109-
The Essential<sup>&reg;</sup> 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.
150+
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.
151+
152+
The following code demonstrates how to iterate through the attachments in a PDF portfolio.
110153

111154
{% tabs %}
112155

@@ -118,15 +161,28 @@ using Syncfusion.Pdf.Parsing;
118161
//Load an existing PDF document
119162
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
120163

121-
//Iterate the attachments
164+
// Iterate through all attachments in the PDF document
122165
foreach (PdfAttachment attachment in document.Attachments)
123166
{
124-
//Extract the attachment and save to the disk
125-
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
126-
s.Write(attachment.Data, 0, attachment.Data.Length);
127-
s.Dispose();
167+
// Create a file stream to save the attachment to disk using its original file name
168+
using (FileStream s = new FileStream(attachment.FileName, FileMode.Create))
169+
{
170+
// Write the attachment data to the file
171+
s.Write(attachment.Data, 0, attachment.Data.Length);
172+
}
173+
// Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
174+
string mimeType = attachment.MimeType;
175+
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}");
176+
// Optional: Access additional metadata if needed
177+
DateTime creationDate = attachment.CreationDate;
178+
DateTime modificationDate = attachment.ModificationDate;
179+
string description = attachment.Description;
180+
PdfAttachmentRelationship relationship = attachment.Relationship;
181+
// Log or use the metadata as needed
182+
Console.WriteLine($"Description: {description}");
183+
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}");
184+
Console.WriteLine($"Relationship: {relationship}");
128185
}
129-
130186
//Save and close the document.
131187
document.Save("Sample.pdf");
132188
document.Close(true);
@@ -141,15 +197,28 @@ using Syncfusion.Pdf.Parsing;
141197
//Load an existing PDF document
142198
PdfLoadedDocument document = new PdfLoadedDocument("Sample.pdf");
143199

144-
//Iterate the attachments
200+
// Iterate through all attachments in the PDF document
145201
foreach (PdfAttachment attachment in document.Attachments)
146202
{
147-
//Extract the attachment and save to the disk
148-
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
149-
s.Write(attachment.Data, 0, attachment.Data.Length);
150-
s.Dispose();
203+
// Create a file stream to save the attachment to disk using its original file name
204+
using (FileStream s = new FileStream(attachment.FileName, FileMode.Create))
205+
{
206+
// Write the attachment data to the file
207+
s.Write(attachment.Data, 0, attachment.Data.Length);
208+
}
209+
// Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
210+
string mimeType = attachment.MimeType;
211+
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}");
212+
// Optional: Access additional metadata if needed
213+
DateTime creationDate = attachment.CreationDate;
214+
DateTime modificationDate = attachment.ModificationDate;
215+
string description = attachment.Description;
216+
PdfAttachmentRelationship relationship = attachment.Relationship;
217+
// Log or use the metadata as needed
218+
Console.WriteLine($"Description: {description}");
219+
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}");
220+
Console.WriteLine($"Relationship: {relationship}");
151221
}
152-
153222
//Save and close the document
154223
document.Save("Output.pdf");
155224
document.Close(true);
@@ -164,12 +233,25 @@ Imports Syncfusion.Pdf.Parsing
164233
'Load the PDF document
165234
Dim document As New PdfLoadedDocument("Sample.pdf")
166235

167-
'Iterate the attachments
236+
' Iterate through all attachments in the PDF document
168237
For Each attachment As PdfAttachment In document.Attachments
169-
'Extracting the attachment and saving into the local disk
170-
Dim s As New FileStream(attachment.FileName, FileMode.Create)
171-
s.Write(attachment.Data, 0, attachment.Data.Length)
172-
s.Dispose()
238+
' Create a file stream to save the attachment to disk using its original file name
239+
Using s As New FileStream(attachment.FileName, FileMode.Create)
240+
' Write the attachment data to the file
241+
s.Write(attachment.Data, 0, attachment.Data.Length)
242+
End Using
243+
' Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
244+
Dim mimeType As String = attachment.MimeType
245+
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}")
246+
' Optional: Access additional metadata if needed
247+
Dim creationDate As DateTime = attachment.CreationDate
248+
Dim modificationDate As DateTime = attachment.ModificationDate
249+
Dim description As String = attachment.Description
250+
Dim relationship As PdfAttachmentRelationship = attachment.Relationship
251+
' Log or use the metadata as needed
252+
Console.WriteLine($"Description: {description}")
253+
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}")
254+
Console.WriteLine($"Relationship: {relationship}")
173255
Next
174256

175257
'Save and close the document

0 commit comments

Comments
 (0)