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.
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 Text
╰ TXTextControl Namespace
╰ TextControl Class
The TextControl class implements a Windows Forms control with high-level text editing features. 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:
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:
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:
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:
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!