Products Technologies Demo Docs Blog Support Company

X19 Sneak Peek: Storing Document Revisions in PDF/A-3b

TX Text Control X19 will support the embedding of attachments to PDF/A-3b documents. This article explains a useful application for this feature that stores document revisions as attachments.

X19 Sneak Peek: Storing Document Revisions in PDF/A-3b

PDF/A-3b allows the storage of attachments in PDF documents. With TX Text Control X19, you will be able to import and export those attachments from and to PDF documents. Typically, this is used to store machine-readable data as an attachment to the human-readable PDF version.

Document Revisions

But there many other applications for this powerful feature. Consider a PDF document that contains the most current version of a document as a PDF representation that can be read by any standard PDF viewer such as Adobe Acrobat Reader or browser readers such as PDF.js.

In addition to this "printable" version of the document, document revisions are stored in an editable format as attachments.

Creating documents with TX Text Control

The following code shows a sample implementation of such a VersionedDocument class:

public class VersionedDocument {

  // member fields
  private TextControl _textControl;
  private string _filename;

  // properties
  public List<EmbeddedFile> Attachments { get; set; } = new List<EmbeddedFile>();

  // constructor
  // attach a TextControl and set filename
  public VersionedDocument(TextControl textControl, string filename) {
    _textControl = textControl;
    _filename = filename;
  }

  // saves the current version as the visible representation
  // and attaches the current version to the attachments
  public void SaveDocument() {
    // save current document from connected TextControl
    byte[] baCurrentDocument;
    _textControl.Save(out baCurrentDocument, BinaryStreamType.InternalUnicodeFormat);

    // create a unique filename
    var uniqueFilename = Guid.NewGuid().ToString() + ".tx";

    // add the latest attachment
    Attachments.Add(new EmbeddedFile(uniqueFilename, baCurrentDocument, null));

    // use a temporary ServerTextControl to save the document
    using (ServerTextControl tx = new TXTextControl.ServerTextControl()) {
      tx.Create();
      tx.Load(baCurrentDocument, BinaryStreamType.InternalUnicodeFormat);

      // set the embedded files using the SaveSettings
      SaveSettings saveSettings = new SaveSettings() {
        EmbeddedFiles = Attachments.ToArray()
      };

      // save the file
      tx.Save(_filename, StreamType.AdobePDF, saveSettings);
    }
  }

  public void LoadDocument(string filename) {
    // temporary ServerTextControl to handle attachments
    using (ServerTextControl tx = new ServerTextControl()) { 
      tx.Create();

      LoadSettings ls = new LoadSettings() {
        PDFImportSettings = PDFImportSettings.LoadEmbeddedFiles
      };

      // load the PDF document
      try {
        tx.Load(filename, StreamType.AdobePDF, ls);
      }
      catch { throw; }

      // store attachments
      Attachments = ls.EmbeddedFiles?.ToList();

      // get latest attachment
      var latestAttachment = Attachments.OrderByDescending(c => c.CreationDate).First();

      // load attachment into connected TextControl
      try {
        _textControl.Load(
          (byte[])latestAttachment.Data,
          BinaryStreamType.InternalUnicodeFormat);
      }
      catch { throw; }

      // set the current filename
      _filename = filename;
    }
  }
}

In a sample Windows Forms application, in the constructor, VersionedDocument is connected to a TextControl class.

VersionedDocument _document;

public Form1() {
  InitializeComponent();
  _document = new VersionedDocument(textControl1, "test.pdf");
}

The sample form consists of two Text Control instances, a button to save a revision, a button to load an existing document and a drop-down box that lists all embedded revisions:

Sample implementation

Saving the Document

In a first step, a first version of the document is created and saved as a revision in the document test.pdf by clicking the button Save Revision:

Sample implementation

The code behind the first button click is shown in the following code gist:

private void button1_Click(object sender, EventArgs e) {
  _document.SaveDocument();
  UpdateBinding();
}

In a second step, the document in the left side Text Control is modified and saved as a new revision by clicking Save Revision:

Sample implementation

The drop-down box shows now two embedded revisions and by changing the selected index, the selected revision can be loaded separately into the second Text Control on the right side:

private void cbAttachments_SelectedIndexChanged(object sender, EventArgs e) {
  if (cbAttachments.SelectedIndex != -1)
    _document.Attachments[cbAttachments.SelectedIndex].Load(textControl2);
}

Loading the Visual Representation

When loading an existing document, the newest version is loaded into the connected Text Control:

private void button2_Click(object sender, EventArgs e) {
  _document.LoadDocument("test.pdf");
  UpdateBinding();
}

If this created document repository is opened in Adobe Acrobat Reader, the most current version is displayed as the PDF representation and the embedded revisions are shown in the list of attachments:

Adobe PDF

This is just another innovative idea from Text Control to integrate electronic document processing into business workflows. Stay tuned for more features of TX Text Control X19!

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETWindows FormsElectronic Invoice

X19 Sneak Peek: Validating ZUGFeRD / Factur-X Invoices with TX Text Control

ZUGFeRD / Factur-X documents can be created and extracted using TX Text Control X19. This article shows how to validate these eletronic invoices by matching invoice elements with the visual PDF…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…


ASP.NETWindows FormsWPF

The Wait is Over: TX Text Control for Linux is Officially Here

We are very excited to announce the release of TX Text Control 33.0 which includes the long awaited Linux version of TX Text Control. This version allows you to integrate TX Text Control into your…