With Text Control components and libraries, PDF documents can be created in all platforms including ASP.NET Core, Angular, React or pure JavaScript. This article focuses on the creation of PDF documents in .NET applications with C#.
In contrast to other PDF tools, TX Text Control can be used to create PDF documents from scratch or to prepare them from existing MS Word templates programmatically.
Creating PDFs From Scratch
TX Text Control libraries provide a powerful and complete API to create and manipulate documents programmatically. This API can be used in any .NET application including web and Windows applications.
The following code snippet shows how to create a new instance of Server
╰ TXTextControl Namespace
╰ ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications. , the non-visual Text Control class. The code inserts a paragraph, a table and exports the document as an Adobe PDF document:
using (ServerTextControl tx = new ServerTextControl()) | |
{ | |
tx.Create(); | |
// create a new selection range | |
Selection range = new Selection(); | |
range.Bold = true; | |
range.FontSize = 400; | |
range.Text = "This is a new paragraph\r\n"; | |
// insert the range to the document | |
tx.Selection = range; | |
// add a table | |
tx.Tables.Add(5, 5, 10); | |
foreach (TableCell cell in tx.Tables.GetItem(10).Cells) | |
{ | |
cell.Text = cell.Row.ToString() + ", " + cell.Column.ToString(); | |
} | |
// save the document as PDF | |
tx.Save("test.pdf", StreamType.AdobePDF); | |
} |
Use MS Word DOCX Templates
TX Text Control provides MS Word inspired document editor components for Angular, React and ASP.NET Core to modify and create templates. After a template has been designed, the reporting engine Mail
╰ DocumentServer Namespace
╰ MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services. is used to populate merge fields with actual data from JSON or business objects. For each data row of the master table in the data source, a document is created. Merge fields are populated with column values and repeating blocks are merged with data rows of related child tables in the data source.
A data source can be any IEnumerable object (business objects), JSON objects, DataSet and DataTable objects.
The following code creates an instance of the reporting engine MailMerge to merge the template that is loaded into a ServerTextControl with an IEnumerable object. Finally, the document is exported as a PDF document:
// create a business object that is used | |
// as the data source | |
Invoice invoice = new Invoice() { Name = "Report" }; | |
using (ServerTextControl tx = new ServerTextControl()) | |
{ | |
tx.Create(); | |
LoadSettings ls = new LoadSettings() { | |
ApplicationFieldFormat = ApplicationFieldFormat.MSWord | |
}; | |
// load the created template | |
tx.Load("template.docx", StreamType.WordprocessingML, ls); | |
// create a new MailMerge engine | |
using (MailMerge mailMerge = new MailMerge()) | |
{ | |
// connect to ServerTextControl | |
mailMerge.TextComponent = tx; | |
// merge the template that is loaded with | |
// the business object | |
mailMerge.MergeObject(invoice); | |
} | |
// export the document as PDF | |
tx.Save("test.pdf", StreamType.AdobePDF); | |
} |
Apply PDF Settings
Using the Save
╰ TXTextControl Namespace
╰ SaveSettings Class
The SaveSettings class provides properties for advanced settings and information during save operations. class, you can define two passwords: the UserPassword to open the document and the MasterPassword for the document's access permissions. These permissions can be set using the Document
╰ TXTextControl Namespace
╰ LoadSettings Class
╰ DocumentAccessPermissions Property
Specifies how a document can be accessed after it has been loaded. property.
Possible values are:
AllowAll
After the document has been opened no further document access is restricted.
AllowAuthoring
Allows comments to be added and interactive form fields (including signature fields) to be filled in.
AllowAuthoringFields
Allows existing interactive form fields (including signature fields) to be filled in.
AllowContentAccessibility
Allows content access for the visually impaired only.
AllowDocumentAssembly
Allows the document to be to assembled (insert, rotate or delete pages and create bookmarks or thumbnails).
AllowExtractContents
Allows text and/or graphics to be extraced.
AllowGeneralEditing
Allows the document contents to be modified.
AllowHighLevelPrinting
Allows the document to be printed.
AllowLowLevelPrinting
Allows the document to be printed (low-level).
The following code shows how to set some of the above access permissions on saving a document:
SaveSettings saveSettings = new SaveSettings() | |
{ | |
MasterPassword = "Master", | |
UserPassword = "User", | |
DocumentAccessPermissions = | |
DocumentAccessPermissions.AllowLowLevelPrinting | | |
DocumentAccessPermissions.AllowExtractContents | |
}; | |
textControl1.Save(StreamType.AdobePDF, saveSettings); |
Adding Digital Signatures
TX Text Control can be used to create Adobe PDF and PDF/A documents with digital signatures. These signatures can be created with PFX, DER Cer or Base64 CER certificate files. All you need is a valid certificate that is defined in the TXText
╰ TXTextControl Namespace
╰ SaveSettings Class
The SaveSettings class provides properties for advanced settings and information during save operations. .
TXTextControl.SaveSettings settings = new TXTextControl.SaveSettings(); | |
X509Certificate2 cert = new X509Certificate2("test.pfx", "123"); | |
settings.DigitalSignature = new TXTextControl.DigitalSignature(cert, null); | |
textControl1.Save("results.pdf", TXTextControl.StreamType.AdobePDF, settings); |
Creating PDF/A - Embedded Documents
TX Text Control supports the embedding of attachments in PDF/A-3b documents and also the extraction of an attachment. The following code shows how to embed an external XML document (ZUGFeRD) into the created PDF/A document:
string xmlZugferd = ""; // your XML | |
string metaData = ""; // required RDF meta data | |
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings(); | |
// create a new embedded file | |
var zugferdInvoice = new TXTextControl.EmbeddedFile( | |
"ZUGFeRD-invoice.xml", | |
Encoding.UTF8.GetBytes(xmlZugferd), | |
metaData); | |
zugferdInvoice.Description = "ZUGFeRD-invoice"; | |
zugferdInvoice.Relationship = "Alternative"; | |
zugferdInvoice.MIMEType = "application/xml"; | |
zugferdInvoice.LastModificationDate = DateTime.Now; | |
// set the embedded files | |
saveSettings.EmbeddedFiles = new TXTextControl.EmbeddedFile[] { | |
new TXTextControl.EmbeddedFile( | |
"ZUGFeRD-invoice.xml", | |
Encoding.UTF8.GetBytes(xmlZugferd), | |
metadata) }; | |
// export the PDF | |
textControl1.Save("test.pdf", TXTextControl.StreamType.AdobePDFA, saveSettings); |
Creating Fillable PDFs with Form Fields
TX Text Control provides a comprehensive way to create documents with fillable form elements such as form text boxes, check box fields and drop-down elements. The following code shows how to create a PDF document with form fields from scratch:
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { | |
tx.Create(); | |
tx.Selection.FontSize = 800; | |
tx.Selection.Text = "Sample Form\r\n\r\n"; | |
tx.Selection.FontSize = 400; | |
tx.Selection.Text = "Company name:\r\n"; | |
// add a text form field | |
TXTextControl.TextFormField textFormField = new TXTextControl.TextFormField(1000); | |
textFormField.Name = "company_name"; | |
textFormField.Text = "Text Control, LLC"; | |
tx.FormFields.Add(textFormField); | |
tx.Selection.Text = "\r\n\r\nCountry:\r\n"; | |
// add a text form field | |
TXTextControl.SelectionFormField selectionFormField = new TXTextControl.SelectionFormField(1000); | |
selectionFormField.Name = "company_country"; | |
selectionFormField.Items = new string[] { "United States", "Germany", "Italy" }; | |
selectionFormField.SelectedIndex = 0; | |
tx.FormFields.Add(selectionFormField); | |
// export the document as PDF | |
tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF); | |
} |
Visual Components
Not being dependent on an additional third-party tool such as MS Word to create templates is a very important key aspect when implementing document processing functionality into business applications. The MS Word compatible editor has the look and feel of MS Word, but can be customized and adapted to user requirements. MS Word templates can be reused and edited in TX Text Control. Templates can be stored in industry standard(ized) formats such as DOCX, DOC and RTF and are always compatible with other word processors such as MS Word.
Available for ASP.NET Core, ASP.NET MVC, Angular and React applications, the DocumentViewer is typically used to display documents and resulting reports in web applications. It provides a fully-featured interface to select text for clipboard actions, to search the text, a thumbnail side-bar view and a toolbar for typical tasks.
Conclusion
TX Text Control provides a feature-complete set of tools to create, modify and to edit Adobe PDF documents. Have a look at the live demo to see the Text Control components and libraries in action: