Create a PDF
Creating PDF documents with ServerTextControl takes a .NET console application and a few steps: you save a new document as PDF, convert an existing DOCX file, write the PDF to a byte array, create PDF/A and set document properties. Finally, you return a PDF from an ASP.NET Core controller.
Prerequisites
- A TX Text Control .NET Server license and access to the Text Control Private NuGet Feed.
- .NET 8 or higher. The steps were written with Visual Studio 2026 and .NET 10.
- Basic knowledge of how
ServerTextControlis used. Create Your First Application walks you through it.
Create the Project
- In Visual Studio, create a new project with the Console App template and choose .NET 10 as the framework.
- Install the Core package from the Text Control Private NuGet Feed, either with the NuGet Package Manager or from the project folder:
dotnet add package TXTextControl.TextControl.Core.SDK
The Core package runs on Windows and Linux and is recommended for new projects. If your application runs on Windows only, you can use TXTextControl.TextControl.ASP.SDK (Classic) instead. The code on this page is the same for both packages.
Create a Document and Save It as PDF
Replace the content of Program.cs with the following code. It creates an empty document, inserts a formatted line of text and saves the document as PDF:
using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
// Create must be called before the instance is used
tx.Create();
// Insert formatted text at the beginning of the document
Selection selection = new Selection(0, 0);
selection.Text = "Hello, PDF!";
selection.FontName = "Arial";
selection.FontSize = 480; // in twips: 480 twips = 24 pt
selection.Bold = true;
tx.Selection = selection;
// Save the document as PDF
tx.Save("result.pdf", StreamType.AdobePDF);
}
Run the application. The file result.pdf is created in the working directory of the application: the project folder with dotnet run, or the build output folder (for example bin/Debug/net10.0) when you start it from Visual Studio. If the file already exists, it is overwritten.
ServerTextControl implements IDisposable, so the using statement releases its resources when the work is done.
Convert an Existing Document to PDF
To convert a document, load it with the matching StreamType and save it as PDF. Add a DOCX file named sample.docx to the project and set its Copy to Output Directory property to Copy always, then use this code:
using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
// Load the Word document
tx.Load("sample.docx", StreamType.WordprocessingML);
// Save the document as PDF
tx.Save("sample.pdf", StreamType.AdobePDF);
}
Other formats work the same way, for example StreamType.RichTextFormat for RTF and StreamType.HTMLFormat for HTML. The Core package does not support the legacy Word binary format (DOC, StreamType.MSWord). See Load a Document for more about loading documents, and the StreamType enumeration for all formats.
Save the PDF to a Byte Array
In web applications and services, you usually keep the PDF in memory instead of writing a file, for example to send it to a browser or store it in a database. Pass an out byte[] variable and BinaryStreamType.AdobePDF to Save, as Save to a Byte Array shows:
tx.Save(out byte[] pdf, BinaryStreamType.AdobePDF);
Create a PDF/A Document
PDF/A is the ISO standard for long-term archiving. To create PDF/A, save the document with StreamType.AdobePDFA (or BinaryStreamType.AdobePDFA for a byte array). There is no separate PDF/A option in SaveSettings: the stream type selects the format.
using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
// Allow only fonts that can be embedded in the PDF/A file
tx.FontSettings.EmbeddableFontsOnly = true;
tx.Load("sample.docx", StreamType.WordprocessingML);
// Save the document as PDF/A
tx.Save("sample-archive.pdf", StreamType.AdobePDFA);
}
Since version 34.0, TX Text Control writes PDF/A-3a documents, which include the tagged document structure used by assistive technologies. PDF/A embeds all fonts and allows no passwords and no JavaScript; The Rules of PDF/A lists what the standard changes in your document. If your PDF must also conform to PDF/UA, PDF/UA explains the requirements.
Set Document Properties
PDF readers show the author, title, subject and keywords of a document, and search tools use them to index files. Set them with a SaveSettings object and pass it to Save:
using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
tx.Text = "Quarterly report";
SaveSettings saveSettings = new SaveSettings()
{
Author = "Tim Typer",
DocumentTitle = "Quarterly Report",
DocumentSubject = "Finance",
DocumentKeywords = new string[] { "report", "Q2", "finance" },
CreatorApplication = "My Application",
CreationDate = DateTime.Now,
LastModificationDate = DateTime.Now
};
tx.Save("report.pdf", StreamType.AdobePDF, saveSettings);
}
Open the PDF in Adobe Acrobat Reader and look at the document properties to see the values. Document Metadata lists all metadata properties and how they relate to the document settings.
SaveSettings also protects a PDF with a password and access permissions, as Protect a PDF With Passwords and Permissions describes.
Return the PDF From an ASP.NET Core Controller
In an ASP.NET Core application, create the PDF in memory and return the byte array with the File method of a controller and the content type application/pdf. Return a Document from an ASP.NET Core Controller creates the project and the controller.
Next Steps
- Load and Modify a PDF: import an existing PDF, read its content and save it again.
- Digitally Sign a PDF: add a certificate-based signature when you save the PDF.
- PDF/A: more about archiving with PDF/A.
- Document Metadata: write and read PDF metadata.