Skip to content

Commit df020ad

Browse files
committed
261980: Updated the code example.
1 parent faff61a commit df020ad

File tree

2 files changed

+31
-20
lines changed
  • Portfolio
    • Create-a-portfolio-and-attach-variety-of-documents/.NET/Create-a-portfolio-and-attach-variety-of-documents
    • Extracting-the-files-from-PDF-portfolio/.NET/Extracting-the-files-from-PDF-portfolio

2 files changed

+31
-20
lines changed

Portfolio/Create-a-portfolio-and-attach-variety-of-documents/.NET/Create-a-portfolio-and-attach-variety-of-documents/Program.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
{
77
//Create a new portfolio.
88
document.PortfolioInformation = new PdfPortfolioInformation();
9-
109
//Set the view mode of the portfolio.
1110
document.PortfolioInformation.ViewMode = PdfPortfolioViewMode.Tile;
12-
1311
//Get stream from the attachment PDF file.
1412
FileStream pdfStream = new FileStream(Path.GetFullPath(@"Data/CorporateBrochure.pdf"), FileMode.Open, FileAccess.Read);
15-
16-
//Create the attachment.
17-
PdfAttachment pdfFile = new PdfAttachment("CorporateBrochure.pdf", pdfStream);
18-
19-
//Set the file name.
13+
// Set the name of the attachment file
2014
pdfFile.FileName = "CorporateBrochure.pdf";
21-
22-
//Set the startup document to view.
15+
// Provide a description for the attachment
16+
pdfFile.Description = "This is a PDF document";
17+
// Set the creation date of the attachment
18+
pdfFile.CreationDate = DateTime.Now;
19+
// Specify the MIME type of the attachment (important for identifying file type)
20+
pdfFile.MimeType = "application/pdf";
21+
// Optionally, set the modification date if needed
22+
pdfFile.ModificationDate = DateTime.Now;
23+
// Optionally, set the relationship (e.g., "Data", "Source", etc.)
24+
pdfFile.Relationship = PdfAttachmentRelationship.Unspecified;
25+
// Set this attachment as the startup document in the PDF portfolio
2326
document.PortfolioInformation.StartupDocument = pdfFile;
24-
2527
//Add the attachment to the document.
2628
document.Attachments.Add(pdfFile);
2729
//Save the PDF document

Portfolio/Extracting-the-files-from-PDF-portfolio/.NET/Extracting-the-files-from-PDF-portfolio/Program.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,27 @@
33

44
//Load the PDF document.
55
PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf"));
6-
7-
string outputFileName = "";
8-
9-
//Iterate the attachments.
6+
// Iterate through all attachments in the PDF document
107
foreach (PdfAttachment attachment in document.Attachments)
118
{
12-
//Extract the attachment and save to the disk.
13-
FileStream s = new FileStream(attachment.FileName, FileMode.Create);
14-
s.Write(attachment.Data, 0, attachment.Data.Length);
15-
s.Dispose();
16-
17-
outputFileName = attachment.FileName;
9+
// Create a file stream to save the attachment to disk using its original file name
10+
using (FileStream s = new FileStream(attachment.FileName, FileMode.Create))
11+
{
12+
// Write the attachment data to the file
13+
s.Write(attachment.Data, 0, attachment.Data.Length);
14+
}
15+
// Retrieve the MIME type of the attachment (e.g., application/pdf, image/png)
16+
string mimeType = attachment.MimeType;
17+
Console.WriteLine($"Saved: {attachment.FileName}, MIME Type: {mimeType}");
18+
// Optional: Access additional metadata if needed
19+
DateTime creationDate = attachment.CreationDate;
20+
DateTime modificationDate = attachment.ModificationDate;
21+
string description = attachment.Description;
22+
PdfAttachmentRelationship relationship = attachment.Relationship;
23+
// Log or use the metadata as needed
24+
Console.WriteLine($"Description: {description}");
25+
Console.WriteLine($"Created on: {creationDate}, Modified on: {modificationDate}");
26+
Console.WriteLine($"Relationship: {relationship}");
1827
}
1928
//Save the PDF document
2029
document.Save(Path.GetFullPath(@"Output/Output.pdf"));

0 commit comments

Comments
 (0)