Smart Documents: Embedding Documents in PDF Containers
TX Text Control is able to embed and extract embedded files to and from PDF documents. This can be used to create smart document containers that consists of the original document for editing and additional information such as annotations.

PDF/A-3 permits the embedding of files in any format. PDF/A-3 documents allow 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.
Smart Document Container
In this sample application, we use a PDF document as a container to store an editable version and the DocumentViewer annotations as embedded files. The following illustration shows this container setup:
The advantage in this scenario is that the PDF can be send to anyone outside of your infrastructure and the current version of the document is always visible for everyone using a simple Acrobat Reader. The viewable version always reflects the most current version.
Sample Concept
In this sample, you can create a new Smart Document by clicking New Smart Document:
The static method CreateNewDocument of the SmartDocument class creates a blank document using a Server
public static string CreateNewDocument() {
var DocumentName = Guid.NewGuid().ToString() + ".pdf";
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
byte[] dataTx;
// save the blank document in the internal TX format
tx.Save(out dataTx, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
// create an attachment
EmbeddedFile embeddedFile = new EmbeddedFile("original.tx", dataTx, null);
embeddedFile.Relationship = "Source";
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
EmbeddedFiles = new EmbeddedFile[] { embeddedFile }
};
// save a PDF with the attached Text Control document embedded
tx.Save("App_Data/" + DocumentName,
TXTextControl.StreamType.AdobePDF,
saveSettings);
}
return DocumentName;
}
It opens the TX Text Control document editor that can be used to create a document:
When loading the original, embedded document, the ExtractSmartDocument method opens the PDF in order to check the embedded files for the original document or the annotations JSON. In case the document should be edited, the embedded original document is loaded into the editor.
public static SmartDocument ExtractSmartDocument(string DocumentName) {
SmartDocument smartDocument = new SmartDocument();
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
// the load PDF document
TXTextControl.LoadSettings loadSettings = new LoadSettings();
tx.Load("App_Data/" + DocumentName,
TXTextControl.StreamType.AdobePDF,
loadSettings);
// loop through all attachments to find the original document
// and the annotations
foreach (EmbeddedFile file in loadSettings.EmbeddedFiles) {
if (file.FileName == "original.tx")
smartDocument.Document = Convert.ToBase64String((byte[])file.Data);
if (file.FileName == "annotations.json")
smartDocument.Annotations = System.Text.Encoding.UTF8.GetString((byte[])file.Data);
}
smartDocument.Name = DocumentName;
}
return smartDocument;
}
When saving the original document, the method SaveDocument is loading the modified version into a ServerTextControl to create the updated PDF representation container by including itself as an embedded file.
public static void SaveDocument(SmartDocument smartDocument) {
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
// load the edited document
tx.Load(Convert.FromBase64String(smartDocument.Document),
TXTextControl.BinaryStreamType.InternalUnicodeFormat);
// create an attachment from the edited document
EmbeddedFile embeddedFile = new EmbeddedFile("original.tx",
Convert.FromBase64String(smartDocument.Document),
null);
embeddedFile.Relationship = "Source";
// attache the files
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
EmbeddedFiles = new EmbeddedFile[] { embeddedFile }
};
// save the document as PDF with the edited original attachment
tx.Save("App_Data/" + smartDocument.Name,
TXTextControl.StreamType.AdobePDF,
saveSettings);
}
}
Back in the overview, this new document can be edited or viewed:
When clicking View (Annotations), the original, editable version of the document is opened in the TX Text Control DocumentViewer that can be used to add annotations:
When clicking Save Annotations, the annotations JSON is stored as an embedded file in the container PDF. Therefore, the associated container PDF is loaded and the original document is extracted. This original document is then loaded and used to create the new PDF document to embed the original version and the annotation JSON.
public static void SaveAnnotations(SmartDocument smartDocument) {
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
tx.Create();
// load the SmartDocument PDF
TXTextControl.LoadSettings loadSettings = new LoadSettings();
tx.Load("App_Data/" + smartDocument.Name,
TXTextControl.StreamType.AdobePDF,
loadSettings);
// find the original embedded document
foreach (EmbeddedFile file in loadSettings.EmbeddedFiles) {
if (file.FileName == "original.tx")
smartDocument.Document = Convert.ToBase64String((byte[])file.Data);
}
// load the original document
tx.Load(Convert.FromBase64String(smartDocument.Document),
BinaryStreamType.InternalUnicodeFormat);
// create an attachment for the original document
EmbeddedFile efOriginal = new EmbeddedFile("original.tx",
Convert.FromBase64String(smartDocument.Document),
null);
efOriginal.Relationship = "Source";
// create an attachment for the annotations
EmbeddedFile efAnnotations = new EmbeddedFile("annotations.json",
Encoding.UTF8.GetBytes(smartDocument.Annotations),
null);
efAnnotations.Relationship = "Source";
// attach the files
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() {
EmbeddedFiles = new EmbeddedFile[] { efOriginal, efAnnotations }
};
// save the SmartDocument as PDF with attachments
tx.Save("App_Data/" + smartDocument.Name,
TXTextControl.StreamType.AdobePDF,
saveSettings);
}
}
Download the Sample
This concept is very flexible and can be used for your own formats and workflows. To test this on your own, download the fully-functional sample project from GitHub and let us know, if you have any questions or feedback.
Happy coding!
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- TX Text Control .NET Server X19
- Visual Studio 2019
Angular
Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.
Related Posts
DocumentViewer Annotations: Highlight Text
We just published a pre-release version of the DocumentViewer that includes a new annotation feature: Text highlights. It is now possible to select text to add a comment at a specific text…
DocumentViewer 29.2 (29.0.302.500) Final Released
We are happy to announce the final release of version 29.2 of the DocumentViewer for ASP.NET and ASP.NET Core. It comes with many new annotation and form field features to deploy documents for…
AngularASP.NETDocument Processing
Creation of Custom Electronic Signature Boxes
This article describes the customization of electronic signature boxes. It contains further explanations including the creation of templates and shows all code snippets required. The step-by-step…
ASP.NETAnnotationsCollaboration
DocumentViewer Collaboration: Live Share Document Annotations
Digital collaboration became the norm in today's modern business world. Adding real-time document collaboration adds a significant value to applications to increase productivity for your users.…
ASP.NETAnnotationsCollaboration
DocumentViewer Pre-Release: Stamps, Sticky Notes and Comments
We just published a pre-release version of the DocumentViewer that supports an extension of the annotation feature. It is now possible to insert custom stamps, sticky notes and comments.