Save as DOCX and PDF
Once a document is ready, you save it as DOCX or PDF to a file or a byte array, return it from an ASP.NET Core controller or save it as HTML or plain text. PDF metadata and PDF/A are covered in Create a PDF.
Prerequisites
- A console application that references
TXTextControl.TextControl.Core.SDK, as created in Create Your First Application. - For the examples that convert a document, a
sample.docxfile in aDocumentsfolder that is copied to the output directory, as described in Load a Document. - For the controller example, Visual Studio 2026 with the .NET 10 SDK. The section creates the ASP.NET Core application.
How Saving Works
You save a document with the ServerTextControl.Save method. It always saves the complete document. As with loading, the enumeration you pass depends on where the data goes:
| Target | First parameter | Format parameter |
|---|---|---|
| File path | string | StreamType |
| File stream | System.IO.FileStream | StreamType |
| Byte array | out byte[] | BinaryStreamType |
| String | out string | StringStreamType |
Every overload also accepts a SaveSettings object as an optional third parameter.
The values for DOCX and PDF:
| Format | StreamType | BinaryStreamType |
|---|---|---|
| DOCX | WordprocessingML | WordprocessingML |
AdobePDF | AdobePDF | |
| PDF/A | AdobePDFA | AdobePDFA |
Save to a File
Pass the path of the file and a StreamType value. If the file does not exist, it is created. If it exists, its contents are overwritten. The following example creates a document and saves it in both formats:
using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
tx.Text = "This document is saved as DOCX and PDF.";
// Save the document as DOCX
tx.Save("result.docx", StreamType.WordprocessingML);
// Save the document as PDF
tx.Save("result.pdf", StreamType.AdobePDF);
}
Converting a document works the same way: load it in one format and save it in another, as Convert an Existing Document to PDF shows for DOCX to PDF.
Save to a Byte Array
When the document is sent over the network, stored in a database or attached to an email, save it to a byte array instead of a file. Declare the variable and pass it with out. Save creates the array, so you do not allocate it yourself:
using TXTextControl;
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
tx.Load("Documents/sample.docx", StreamType.WordprocessingML);
// Save the document as PDF into a byte array
byte[] pdf;
tx.Save(out pdf, BinaryStreamType.AdobePDF);
// Save the document as DOCX into a byte array
byte[] docx;
tx.Save(out docx, BinaryStreamType.WordprocessingML);
Console.WriteLine($"PDF: {pdf.Length} bytes, DOCX: {docx.Length} bytes");
}
Return a Document From an ASP.NET Core Controller
ServerTextControl works the same way in an ASP.NET Core application. In a web application, save the document to a byte array and return it with the File method of the controller. The content type tells the browser what kind of file it receives, and the file name makes the browser download it.
In Visual Studio 2026, create a new project and select ASP.NET Core Web App (Model-View-Controller) as the project template. Choose .NET 10.0 (Long Term Support) as the Framework.
Install the
TXTextControl.TextControl.Core.SDKpackage as described in Install the NuGet Package.Add the following class as
DocumentController.csto theControllersfolder:using Microsoft.AspNetCore.Mvc; using TXTextControl; public class DocumentController : Controller { public IActionResult Pdf() { using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); tx.Text = "This PDF was created in ASP.NET Core."; byte[] pdf; tx.Save(out pdf, BinaryStreamType.AdobePDF); return File(pdf, "application/pdf", "document.pdf"); } } public IActionResult Docx() { using (ServerTextControl tx = new ServerTextControl()) { tx.Create(); tx.Text = "This DOCX was created in ASP.NET Core."; byte[] docx; tx.Save(out docx, BinaryStreamType.WordprocessingML); return File(docx, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "document.docx"); } } }Start the application and open
/Document/Pdfin the browser. With the default route of an ASP.NET Core MVC application, the actions are available at/Document/Pdfand/Document/Docx, and the browser downloadsdocument.pdf.
Save PDF With Metadata or as PDF/A
A SaveSettings object, passed as the third parameter of Save, adds document metadata such as the title, author and keywords, a password or access permissions, and digital signatures. To create a PDF/A document for long-term archiving, save with StreamType.AdobePDFA or BinaryStreamType.AdobePDFA instead of AdobePDF. Set Document Properties and Create a PDF/A Document in Create a PDF show both with code.
Save Other Formats
The same method saves the other formats listed in Formats and StreamType Values, except XLSX, which TX Text Control only loads. Text-based formats can be saved to a string. For example, the following line saves the document as HTML:
string html;
tx.Save(out html, StringStreamType.HTMLFormat);
The next line extracts the text of the document as plain text:
string plainText;
tx.Save(out plainText, StringStreamType.PlainText);
Next Steps
- PDF Processing — create, modify and sign PDF documents.
- Digitally Sign a PDF — add a digital signature when you save a PDF.
- Simple Mail Merge — merge data into a template and save the result as PDF.
- Document Metadata — read and write PDF metadata.