Skip to content

Commit 2938666

Browse files
Add 50 example(s) for working-with-attachments
1 parent 3765296 commit 2938666

7 files changed

Lines changed: 222 additions & 306 deletions

working-with-attachments/agents.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ See the root [agents.md](../agents.md) for repository-wide conventions and bound
2323

2424
- `using Aspose.Pdf;` (49/50 files) ← category-specific
2525
- `using Aspose.Pdf.Annotations;` (12/50 files)
26-
- `using Aspose.Pdf.Facades;` (3/50 files)
2726
- `using Aspose.Pdf.Drawing;` (2/50 files)
28-
- `using Aspose.Pdf.Text;` (2/50 files)
2927
- `using Aspose.Pdf.AI;` (1/50 files)
3028
- `using Aspose.Pdf.Devices;` (1/50 files)
3129
- `using Aspose.Pdf.Optimization;` (1/50 files)
30+
- `using Aspose.Pdf.Text;` (1/50 files)
3231
- `using System;` (50/50 files)
3332
- `using System.IO;` (49/50 files)
34-
- `using System.Collections.Generic;` (5/50 files)
33+
- `using System.Collections.Generic;` (6/50 files)
3534
- `using NUnit.Framework;` (2/50 files)
3635
- `using System.Threading.Tasks;` (2/50 files)
3736
- `using System.Diagnostics;` (1/50 files)
@@ -79,7 +78,7 @@ using (Document doc = new Document("input.pdf"))
7978
| [create-pdf-portfolio](./create-pdf-portfolio.cs) | Create PDF Portfolio and Add Files | `Document`, `Collection`, `FileSpecification` | Demonstrates how to create a PDF Portfolio with Aspose.Pdf, add multiple files as attachments, se... |
8079
| [delete-pdf-attachment-by-filename](./delete-pdf-attachment-by-filename.cs) | Delete PDF Attachment by Filename | `Document`, `Delete`, `Save` | Demonstrates how to remove an embedded file from a PDF by specifying its filename using Aspose.Pdf. |
8180
| [delete-pdf-outline-items-by-description](./delete-pdf-outline-items-by-description.cs) | Delete PDF Outline Items by Description | `Document`, `OutlineItemCollection`, `Title` | Demonstrates loading a PDF with Aspose.Pdf, finding outline (bookmark) entries whose titles match... |
82-
| [embed-attachment-metadata-into-pdf](./embed-attachment-metadata-into-pdf.cs) | Embed Attachment Metadata into PDF Document Information | `Document`, `PdfContentEditor`, `BindPdf` | Shows how to add a file attachment to a PDF with PdfContentEditor and store custom attachment met... |
81+
| [embed-attachment-metadata-into-pdf](./embed-attachment-metadata-into-pdf.cs) | Embed Attachment and Metadata into PDF | `Document`, `FileSpecification`, `PdfSaveOptions` | Shows how to add a file attachment to a PDF and store attachment metadata in the document informa... |
8382
| [embed-file-attachment-in-pdf](./embed-file-attachment-in-pdf.cs) | Embed a File as an Attachment in a PDF | `Document`, `Save`, `Page` | Shows how to create a FileSpecification from a byte array and add it as a file attachment annotat... |
8483
| [embed-image-into-pdf-portfolio](./embed-image-into-pdf-portfolio.cs) | Embed Image into PDF Portfolio with Display Name | `Document`, `FileSpecification` | Shows how to embed an image file into a PDF portfolio and assign a custom display name for the at... |
8584
| [embed-xml-metadata-hidden-attachment](./embed-xml-metadata-hidden-attachment.cs) | Embed XML Metadata as Hidden Attachment in PDF | `Document`, `Page`, `FileAttachmentAnnotation` | Demonstrates how to serialize XML metadata, embed it as a file attachment, and hide the annotatio... |
@@ -131,5 +130,5 @@ using (Document doc = new Document("input.pdf"))
131130
- Review code examples in this folder for working-with-attachments patterns
132131

133132
<!-- AUTOGENERATED:START -->
134-
Updated: 2026-04-07 | Run: `20260407_212044_4ffbd1`
133+
Updated: 2026-04-10 | Run: `20260410_121416_bd35e2`
135134
<!-- AUTOGENERATED:END -->
Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
22
using System.IO;
33
using Aspose.Pdf;
4-
using Aspose.Pdf.Facades;
54

65
class Program
76
{
87
static void Main()
98
{
109
const string inputPdf = "input.pdf";
11-
const string outputPdf = "output.pdf";
10+
const string outputPdf = "output_with_attachment.pdf";
1211
const string attachmentPath = "attachment.txt";
1312

1413
if (!File.Exists(inputPdf))
@@ -23,25 +22,36 @@ static void Main()
2322
return;
2423
}
2524

26-
// Load the existing PDF document
2725
using (Document doc = new Document(inputPdf))
2826
{
29-
// Add the attachment without creating an annotation
30-
using (PdfContentEditor editor = new PdfContentEditor())
27+
// -------------------------------------------------------------
28+
// 1. Create a FileSpecification for the attachment and set its
29+
// contents via a stream. Then add it to the EmbeddedFiles
30+
// collection of the document.
31+
// -------------------------------------------------------------
32+
var fileSpec = new FileSpecification(attachmentPath, "Attachment");
33+
using (FileStream attStream = File.OpenRead(attachmentPath))
3134
{
32-
editor.BindPdf(doc);
33-
editor.AddDocumentAttachment(attachmentPath, "Sample attachment");
35+
var mem = new MemoryStream();
36+
attStream.CopyTo(mem);
37+
mem.Position = 0;
38+
fileSpec.Contents = mem;
3439
}
40+
doc.EmbeddedFiles.Add(fileSpec);
3541

36-
// Embed attachment metadata into the document information dictionary
37-
// Custom keys can be any string; here we store file name and description
38-
doc.Info.Add("AttachmentFileName", Path.GetFileName(attachmentPath));
39-
doc.Info.Add("AttachmentDescription", "Sample attachment");
42+
// -------------------------------------------------------------
43+
// 2. Store attachment metadata (e.g., file name) into the
44+
// document information dictionary.
45+
// -------------------------------------------------------------
46+
doc.Info.Add("Attachment", Path.GetFileName(attachmentPath));
4047

41-
// Save the modified PDF
42-
doc.Save(outputPdf);
48+
// -------------------------------------------------------------
49+
// 3. Save the document using explicit PdfSaveOptions.
50+
// -------------------------------------------------------------
51+
var saveOpts = new PdfSaveOptions();
52+
doc.Save(outputPdf, saveOpts);
4353
}
4454

45-
Console.WriteLine($"PDF saved with attachment and metadata to '{outputPdf}'.");
55+
Console.WriteLine($"PDF saved with attachment and metadata: {outputPdf}");
4656
}
47-
}
57+
}

0 commit comments

Comments
 (0)