Products Technologies Demo Docs Blog Support Company

Adding Attachments to Adobe PDF Documents using C#

TX Text Control is used to create electronic document containers by embedding files in Adobe PDF documents. This tutorial shows how to add and extract attachments to and from PDF documents.

Adding Attachments to Adobe PDF Documents using C#

PDF/A-3 allows the embedding of any file type into PDF documents. That allows the progression from electronic paper to an electronic container that holds the human and machine-readable versions of a document. Applications can extract the machine-readable portion of the PDF document in order to process it. A PDF/A-3 document can contain an unlimited number of embedded documents for different processes.

Learn more

PDF/A-3 permits the embedding of files of any format. This article gives an overview of the advantages of PDF/A-3 as an electronic container.

PDF/A-3: The Better Container for Electronic Documents

In this article you will learn how to embed a plain text file into a PDF document and how to extract the attachment from the PDF document.

Adding Attachments

This following sample code shows how TX Text Control can be used to attach the text file to a PDF document:

// create a non-UI ServerTextControl instance
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {

  tx.Create();
  // set dummy content
  tx.Text = "PDF Document Content";

  // read the content of the attachment
  string sAttachment = System.IO.File.ReadAllText("attachment.txt");

  // create the attachement
  TXTextControl.EmbeddedFile attachment =
     new TXTextControl.EmbeddedFile(
        "attachment.txt",
        sAttachment,
        null) {
       Description = "My Text File",
       Relationship = "Unspecified",
       MIMEType = "application/txt",
       CreationDate = DateTime.Now,
     };

  // attached the embedded file
  tx.DocumentSettings.EmbeddedFiles =
     new TXTextControl.EmbeddedFile[] { attachment };

  // save as PDF/A
  tx.Save("document.pdf", TXTextControl.StreamType.AdobePDFA);
}

The EmbeddedFile object represents the attachment. In the constructor, the file name, the data and additional meta data can be added. Additionally, the MIME type of the attachment (application/text in our case), a textual description, a relationship and the creation date is provided.

The relationship is an optional string describing the relationship of the embedded file and the containing document. It can be a predefined value or should follow the rules for second-class names (ISO 32000-1, Annex E). Predefined values are "Source", "Data", "Alternative", "Supplement" or "Unspecified".

When opening the document in Adobe Acrobat Reader, you will find the attachment in the Attachments side-panel.

PDF Attachments

Extracting Attachments

The following code is loading the created PDF file in order to find the attachment by looping through all embedded files. The found attachment is then extracted and exported as a text file.

// create a non-UI ServerTextControl instance
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {

  tx.Create();

  // load the PDF document
  TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
  tx.Load("document.pdf", TXTextControl.StreamType.AdobePDF, ls);

  // read the attachments
  TXTextControl.EmbeddedFile[] files = ls.EmbeddedFiles;

  // find the specific attachment and save it
  foreach(TXTextControl.EmbeddedFile file in files) {
    if (file.Description == "My Text File") {
      string sAttachment = Encoding.UTF8.GetString((byte[])file.Data);
      System.IO.File.WriteAllText("attachment_read.txt", sAttachment);

      break;
    }
  }
}

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.EmbeddedFile Class
  • TXTextControl.EmbeddedFile.Relationship Property

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularASP.NETASP.NET Core

Creating Advanced Tables in PDF and DOCX Documents with C#

This article shows how to create advanced tables in PDF and DOCX documents using the TX Text Control .NET for ASP.NET Server component. This article shows how to create tables from scratch,…


AngularASP.NETASP.NET Core

How to Choose the Best C# Library for your Document Processing Needs

There are many document processing libraries and components on the market. This article is an overview of all the important aspects in the selection of the best library for your needs.


ASP.NETASP.NET CoreExtraction

Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas

Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…


ASP.NETASP.NET CoreForms

Streamline Data Collection with Embedded Forms in C# .NET

Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…


ASP.NETASP.NET CorePDF

Adding QR Codes to PDF Documents in C# .NET

This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…