Creating PDF Documents from MS Word DOCX in C#
TX Text Control provides an elegant way to create Adobe PDF documents by merging data into MS Word DOCX templates. The developer libraries can be used in any platform including ASP.NET Core, Angular, React or Windows applications.

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
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
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
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.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:
Angular
Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.
Related Posts
PDF Conversion in .NET: Convert DOCX, HTML and more with C#
PDF conversion in .NET is a standard requirement for generating invoices, templates, and accessible reports. This article provides an overview of PDF conversion capabilities using TX Text Control,…
Convert MS Word DOCX to PDF including Text Reflow using .NET C# on Linux
This article explains how to use TX Text Control .NET Server to convert a Microsoft Word DOCX document to a PDF file on a Linux system using .NET C#. This conversion process includes text reflow,…
Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX…
This article explores how to use the TX Text Control MailMerge feature in .NET applications on Linux to generate pixel-perfect PDFs from DOCX templates. This powerful combination enables…
Mail Merge MS Word DOCX Documents and Convert to PDF in .NET C#
This article shows how to merge data into MS Word DOCX documents and convert them to PDF using TX Text Control .NET Server.
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,…