# Add and Extract Attachments from PDF Documents in C#

> PDF/A-3 allows files of any format to be embedded and can contain an unlimited number of embedded documents for different processes. This example shows adding and extracting attachments from PDF documents.

- **Author:** Bjoern Meyer
- **Published:** 2023-07-07
- **Modified:** 2025-11-16
- **Description:** PDF/A-3 allows files of any format to be embedded and can contain an unlimited number of embedded documents for different processes. This example shows adding and extracting attachments from PDF documents.
- **3 min read** (444 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - PDF
  - Attachments
- **Web URL:** https://www.textcontrol.com/blog/2023/07/07/add-and-extract-attachments-from-pdf-documents-in-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/07/07/add-and-extract-attachments-from-pdf-documents-in-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/07/07/add-and-extract-attachments-from-pdf-documents-in-csharp/llms-full.txt

---

PDF/A-3 documents enable the transition from electronic paper to an electronic container that holds both human- and machine-readable versions of a document. An unlimited number of embedded documents for different processes can be included in a PDF/A-3 document. The e-invoice, which attaches a machine-readable XML document to the generated PDF invoice, is a very popular example.

The document processing libraries of TX Text Control make it easy to add and extract attachments from PDF/A-3 documents.

### Preparing the Application

For the purposes of this demo, a .NET 6 console application will be created.

1. In Visual Studio, create a new Console App using .NET 6.
    
    ![Extracting Attachments from PDF documents](https://s1-www.textcontrol.com/assets/dist/blog/2023/07/07/a/assets/step1.webp "Extracting Attachments from PDF documents")
2. In the *Solution Explorer*, select your created project and choose *Manage NuGet Packages...* from the *Project* main menu.
    
    Select *Text Control Offline Packages* from the *Package source* drop-down.
    
    Install the latest versions of the following package:
    
    
    - TXTextControl.TextControl.ASP.SDK
    
    ![Extracting Attachments from PDF documents](https://s1-www.textcontrol.com/assets/dist/blog/2023/07/07/a/assets/step2.webp "Extracting Attachments from PDF documents")

### Adding Attachments

The following code creates a new PDF document with a text document attached. The EmbeddedFile class represents the embedded file and can be added to the document using the EmbeddedFiles collection.

```
using System.Text;

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
   tx.Create();
   tx.Text = "This is a sample PDF document with an attachment";

   byte[] baAttachment = Encoding.ASCII.GetBytes("This is a textual attachment.");

   // create a new embedded file
   TXTextControl.EmbeddedFile efAttachment =
       new TXTextControl.EmbeddedFile("attachment.tx", baAttachment, null) {
          Description = "My embedded text file."
       };

   // add the embedded file to TextControl
   tx.DocumentSettings.EmbeddedFiles =
       new TXTextControl.EmbeddedFile[] { efAttachment };

   // save the document
   tx.Save("mypdf.pdf", TXTextControl.StreamType.AdobePDF);
}
```

When you open the document in Acrobat Reader, the attachment is listed in the *Attachments* sidebar.

![Extracting Attachments from PDF documents](https://s1-www.textcontrol.com/assets/dist/blog/2023/07/07/a/assets/attachment1.webp "Extracting Attachments from PDF documents")

### Extracting Attachments

To extract attachments, the PDF must be loaded into TX Text Control using LoadSettings. The attachments are stored in the EmbeddedFiles array of attachments.

```
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
   tx.Create();

   TXTextControl.LoadSettings loadSettings = new TXTextControl.LoadSettings();

   tx.Load("mypdf.pdf", TXTextControl.StreamType.AdobePDF, loadSettings);

   foreach (TXTextControl.EmbeddedFile embeddedFile in loadSettings.EmbeddedFiles) {
      System.IO.File.WriteAllText(
         embeddedFile.FileName,
         Encoding.ASCII.GetString((byte[])embeddedFile.Data));
   }
}
```

TX Text Control provides comprehensive functionality for creating, manipulating, and analyzing PDF documents. To get started with TX Text Control, take a look at the [Getting Started](https://www.textcontrol.com/product/tx-text-control-dotnet-server/getting-started/?type=Getting+Started&technology=&component=) section.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Streamline Document Workflows: Harness the Power of Embedded Attachments in PDFs in .NET C#](https://www.textcontrol.com/blog/2024/11/15/streamline-document-workflows-harness-the-power-of-embedded-attachments-in-pdfs-in-net-c-sharp/llms.txt)
- [How to Extract Attachments from PDF Documents in C#](https://www.textcontrol.com/blog/2024/10/07/how-to-extract-attachments-from-pdf-documents-in-csharp/llms.txt)
- [Using QR Codes in PDF Documents in C# .NET](https://www.textcontrol.com/blog/2026/04/21/using-qr-codes-in-pdf-documents-in-csharp-dotnet/llms.txt)
- [Why Structured E-Invoices Still Need Tamper Protection using C# and .NET](https://www.textcontrol.com/blog/2026/03/24/why-structured-e-invoices-still-need-tamper-protection-using-csharp-and-dotnet/llms.txt)
- [Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template](https://www.textcontrol.com/blog/2026/03/17/create-fillable-pdfs-from-html-forms-in-csharp-aspnet-core-using-a-wysiwyg-template/llms.txt)
- [Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents in C# .NET](https://www.textcontrol.com/blog/2026/03/13/why-html-to-pdf-conversion-is-often-the-wrong-choice-for-business-documents-in-csharp-dot-net/llms.txt)
- [A Complete Guide to Converting Markdown to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/07/a-complete-guide-to-converting-markdown-to-pdf-in-dotnet-csharp/llms.txt)
- [Why PDF Creation Belongs at the End of the Business Process](https://www.textcontrol.com/blog/2026/01/02/why-pdf-creation-belongs-at-the-end-of-the-business-process/llms.txt)
- [Designing the Perfect PDF Form with TX Text Control in .NET C#](https://www.textcontrol.com/blog/2025/12/16/designing-the-perfect-pdf-form-with-tx-text-control-in-dotnet-csharp/llms.txt)
- [Why Defining MIME Types for PDF/A Attachments Is Essential](https://www.textcontrol.com/blog/2025/12/10/why-defining-mime-types-for-pdfa-attachments-is-essential/llms.txt)
- [Validate Digital Signatures and the Integrity of PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/11/14/validate-digital-signatures-and-the-integrity-of-pdf-documents-in-csharp-dotnet/llms.txt)
- [Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET](https://www.textcontrol.com/blog/2025/11/13/validate-pdf-ua-documents-and-verify-electronic-signatures-in-csharp-dotnet/llms.txt)
- [How To Choose the Right C# PDF Generation Library: Developer Checklist](https://www.textcontrol.com/blog/2025/11/12/how-to-choose-the-right-csharp-pdf-generation-library-developer-checklist/llms.txt)
- [Why Digitally Signing your PDFs is the Only Reliable Way to Prevent Tampering](https://www.textcontrol.com/blog/2025/10/30/why-digitally-signing-your-pdfs-is-the-only-reliable-way-to-prevent-tampering/llms.txt)
- [Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX Text Control and LLMs](https://www.textcontrol.com/blog/2025/10/16/automating-pdf-ua-accessibility-with-ai-describing-docx-documents-using-tx-text-control-and-llms/llms.txt)
- [Converting Office Open XML (DOCX) to PDF in Java](https://www.textcontrol.com/blog/2025/10/14/converting-office-open-xml-docx-to-pdf-in-java/llms.txt)
- [Extending DS Server with Custom Digital Signature APIs](https://www.textcontrol.com/blog/2025/10/09/extending-ds-server-with-custom-digital-signature-apis/llms.txt)
- [Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance](https://www.textcontrol.com/blog/2025/10/07/why-pdf-ua-and-pdf-a-3a-matter-accessibility-archiving-and-legal-compliance/llms.txt)
- [Convert Markdown to PDF in a Console Application on Linux and Windows](https://www.textcontrol.com/blog/2025/09/23/convert-markdown-to-pdf-in-a-console-application-on-linux-and-windows/llms.txt)
- [Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas](https://www.textcontrol.com/blog/2025/08/12/mining-pdfs-with-regex-in-csharp-practical-patterns-tips-and-ideas/llms.txt)
- [Streamline Data Collection with Embedded Forms in C# .NET](https://www.textcontrol.com/blog/2025/08/02/streamline-data-collection-with-embedded-forms-in-csharp-dotnet/llms.txt)
- [Adding QR Codes to PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/07/15/adding-qr-codes-to-pdf-documents-in-csharp-dotnet/llms.txt)
- [Adding SVG Graphics to PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/07/08/adding-svg-graphics-to-pdf-documents-in-csharp-dotnet/llms.txt)
- [Enhancing PDF Searchability in Large Repositories by Adding and Reading Keywords Using C# .NET](https://www.textcontrol.com/blog/2025/06/24/enhancing-pdf-searchability-in-large-repositories-by-adding-and-reading-keywords-using-csharp-dotnet/llms.txt)
- [How to Verify PDF Encryption Programmatically in C# .NET](https://www.textcontrol.com/blog/2025/06/20/how-to-verify-pdf-encryption-programmatically-in-csharp-dotnet/llms.txt)
