Products Technologies Demo Docs Blog Support Company

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.

Add and Extract Attachments from PDF Documents in C#

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

  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

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

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 section.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETASP.NET CoreAttachments

Streamline Document Workflows: Harness the Power of Embedded Attachments in…

Embedding files as attachments to PDF documents offers significant benefits across multiple industries. This article shows how to create PDF documents with embedded attachments using TX Text…


ASP.NETASP.NET CoreAttachments

How to Extract Attachments from PDF Documents in C#

This article shows how to extract attachments from PDF documents using TX Text Control .NET Server. The sample code extracts all attachments from a PDF document and saves them to disk.


ASP.NETASP.NET CorePDF

Using QR Codes in PDF Documents in C# .NET

QR codes are a powerful tool for embedding machine-readable information in documents. In this article, we will explore how to generate and insert them into PDF documents using C# .NET with TX Text…


ASP.NETASP.NET CoreE-Invoicing

Why Structured E-Invoices Still Need Tamper Protection using C# and .NET

ZUGFeRD, Factur-X, German e-invoicing rules, and how to seal PDF invoices with TX Text Control to prevent tampering. Learn how to create compliant e-invoices with C# and .NET.


ASP.NETASP.NET CoreForms

Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template

Learn how to generate PDFs from HTML forms in ASP.NET Core using a pixel-perfect WYSIWYG template. Extract form fields from a document, render a dynamic HTML form, and merge the data server-side…

Share on this blog post on: