TPDF 0.4.13
See the version list below for details.
dotnet add package TPDF --version 0.4.13
NuGet\Install-Package TPDF -Version 0.4.13
<PackageReference Include="TPDF" Version="0.4.13" />
<PackageVersion Include="TPDF" Version="0.4.13" />
<PackageReference Include="TPDF" />
paket add TPDF --version 0.4.13
#r "nuget: TPDF, 0.4.13"
#:package TPDF@0.4.13
#addin nuget:?package=TPDF&version=0.4.13
#tool nuget:?package=TPDF&version=0.4.13
TPDF
TPDF is a .NET package for Word/DOCX workflows:
- Convert DOCX to PDF with the packaged in-process TPDF/LibreOffice runtime.
- Render Word
MERGEFIELDmail-merge templates with the managedTPDF.MailMergeassembly. - Merge a template and convert it directly to PDF without asking the caller to save an intermediate DOCX.
- Merge multiple PDF documents into one final PDF.
- Stamp final PDF page numbers after conversion or after combining multiple PDFs.
The package currently targets Linux x64 and Windows x64.
Install
dotnet add package TPDF
The package carries the native Linux and Windows TPDF runtimes. Its transitive build target copies the native library and tpdf-runtime folder beside your app output.
Convert DOCX To PDF
using TPDF;
Tpdf.ConvertDocxToPdf("input.docx", "output.pdf");
You can also pass DOCX bytes. TPDF still creates a temporary DOCX internally because the native converter is path-based, but your application does not have to persist or manage that file.
byte[] docxBytes = File.ReadAllBytes("input.docx");
Tpdf.ConvertDocxToPdf(docxBytes, "output.pdf");
byte[] pdfBytes = Tpdf.ConvertDocxToPdf(docxBytes);
Merge PDFs
Use MergePdfs when you have multiple final PDF documents and want one combined PDF. Do not concatenate DOCX or PDF byte arrays directly.
using TPDF;
byte[] firstPdf = Tpdf.ConvertDocxToPdf(firstDocxBytes);
byte[] secondPdf = Tpdf.ConvertDocxToPdf(secondDocxBytes);
byte[] combinedPdf = Tpdf.MergePdfs(firstPdf, secondPdf);
For file-based code:
Tpdf.MergePdfs(new[] { "first.pdf", "second.pdf" }, "combined.pdf");
Merge DOCX Documents To PDF
Use MergeDocxDocumentsToPdf when you have multiple DOCX documents and the final output should be one PDF. On Windows, TPDF uses a native LibreOffice merge-to-PDF path that loads the first DOCX, inserts the remaining DOCX documents, and exports one PDF. On Linux, TPDF keeps the separate DOCX-to-PDF conversions and merges the resulting PDFs.
Set TPDF_DISABLE_NATIVE_DOCX_TO_PDF_MERGE to any non-empty value to force the legacy separate-convert-and-merge behavior on Windows.
using TPDF;
byte[] combinedPdf = Tpdf.MergeDocxDocumentsToPdf(firstDocxBytes, secondDocxBytes);
For file-based code:
Tpdf.MergeDocxDocumentsToPdf(new[] { "first.docx", "second.docx" }, "combined.pdf");
Stamp PDF Page Numbers
Use StampPageNumbers after the final PDF has all pages. This matches the common Aspose-style flow where page numbers are added after the document is converted and combined.
using TPDF;
byte[] pdfBytes = Tpdf.ConvertDocxToPdf(docxBytes);
byte[] stampedPdfBytes = Tpdf.StampPageNumbers(pdfBytes);
For file-based code:
Tpdf.StampPageNumbers("input.pdf", "output.pdf");
The default format is {page}/{total}, for example 1/3, 2/3, 3/3. You can override the format, margins, font size, and style:
byte[] stampedPdfBytes = Tpdf.StampPageNumbers(pdfBytes, new PdfPageNumberStampOptions
{
Format = "{page}/{total}",
BottomMarginPoints = 0.2 * 72,
RightMarginPoints = 0.5 * 72,
FontSize = 10,
Bold = true,
Italic = true
});
TPDF uses bundled runtime fonts for the stamp so the default behavior does not depend on Calibri or Arial being installed on the host machine.
Mail Merge Only
Use the Tpdf facade when you want to render a Word mail-merge template to a DOCX.
using TPDF;
using TPDF.MailMerge;
var data = new MailMergeData(
Fields: new Dictionary<string, MailMergeValue>(StringComparer.OrdinalIgnoreCase)
{
["OrderId"] = new MailMergeValue.Text("TPDF-ORDER-001"),
["CustomerName"] = new MailMergeValue.Text("Morten Gryning"),
["SalesPrice"] = new MailMergeValue.Text("249.900 kr.")
},
Regions: new Dictionary<string, IReadOnlyList<IReadOnlyDictionary<string, MailMergeValue>>>(StringComparer.OrdinalIgnoreCase)
{
["CarEquipments"] = new[]
{
new Dictionary<string, MailMergeValue>(StringComparer.OrdinalIgnoreCase)
{
["Name"] = new MailMergeValue.Text("Vinterhjul"),
["Price"] = new MailMergeValue.Text("8.000 kr.")
},
new Dictionary<string, MailMergeValue>(StringComparer.OrdinalIgnoreCase)
{
["Name"] = new MailMergeValue.Text("Adaptiv fartpilot"),
["Price"] = new MailMergeValue.Text("12.000 kr.")
}
}
});
Tpdf.MergeMailMergeTemplate("template.docx", "merged.docx", data);
For an in-memory merge:
byte[] mergedDocx = Tpdf.MergeMailMergeTemplate("template.docx", data);
For templates that do not need merge data, use an empty data object:
Tpdf.MergeMailMergeTemplate("template.docx", "merged.docx", new MailMergeData());
// or
Tpdf.MergeMailMergeTemplate("template.docx", "merged.docx", MailMergeData.Empty);
For stream-oriented code:
using var template = File.OpenRead("template.docx");
using var merged = File.Create("merged.docx");
Tpdf.MergeMailMergeTemplate(template, merged, data);
DTO Adapter
If your existing code prepares report DTOs for Aspose-style mail merge, you can build MailMergeData from those DTOs instead of hand-writing dictionaries.
using TPDF;
using TPDF.MailMerge;
var data = new MailMergeDataBuilder()
.AddFieldsFromObject(reportDto)
.AddRegion("CarEquipments", reportDto.CarEquipments)
.Build();
Tpdf.MergeMailMergeTemplate("template.docx", "merged.docx", data);
Single fields are flattened using dot notation, so reportDto.Buyer.Name becomes Buyer.Name. Byte arrays become image values for fields such as Image:LogoStream. Collection properties are skipped during field flattening so lists can be added deliberately as named regions.
For compact code, a DTO can also be converted directly:
var data = reportDto.ToMailMergeData();
Region rows can come from regular objects, dictionaries, or existing MailMergeValue rows:
var data = new MailMergeDataBuilder()
.AddFieldsFromObject(reportDto)
.AddRegion("Items", items.Select(i => new { i.Name, i.Price }))
.Build();
Use MailMergeObjectAdapterOptions when you want predictable formatting:
using System.Globalization;
var options = new MailMergeObjectAdapterOptions
{
Culture = CultureInfo.GetCultureInfo("da-DK"),
DateTimeFormat = "dd-MM-yyyy",
FloatingPointFormat = "N"
};
var data = new MailMergeDataBuilder()
.AddFieldsFromObject(reportDto, options: options)
.AddRegion("Items", reportDto.Items, options)
.Build();
Merge And Convert To PDF
For the common server flow, merge first and pass the merged DOCX bytes straight into the converter:
byte[] mergedDocx = Tpdf.MergeMailMergeTemplate("template.docx", data);
byte[] pdfBytes = Tpdf.ConvertDocxToPdf(mergedDocx);
byte[] stampedPdfBytes = Tpdf.StampPageNumbers(pdfBytes);
Or use the combined helper:
Tpdf.ConvertMailMergeTemplateToPdf("template.docx", "output.pdf", data);
byte[] pdfBytes = Tpdf.ConvertMailMergeTemplateToPdf("template.docx", data);
byte[] stampedPdfBytes = Tpdf.StampPageNumbers(pdfBytes);
The combined helper does not require the caller to save merged.docx. Internally it renders the merged DOCX in memory, writes a temporary DOCX for the native TPDF converter, reads or writes the PDF result, and removes the temporary folder.
For multi-template reports, merge each template to DOCX, combine those DOCX documents directly to one PDF, then stamp the final document:
var mergedDocxs = new List<byte[]>();
foreach (var templatePath in templatePaths)
{
byte[] mergedDocx = Tpdf.MergeMailMergeTemplate(templatePath, data);
mergedDocxs.Add(mergedDocx);
}
byte[] combinedPdf = Tpdf.MergeDocxDocumentsToPdf(mergedDocxs);
byte[] finalPdf = Tpdf.StampPageNumbers(combinedPdf);
Template Support
TPDF.MailMerge is designed for Word mail-merge templates and aims to match common Aspose.Words mail-merge output. Aspose is not used at runtime. It supports:
- Regular Word
MERGEFIELDfields. - Repeating regions using
TableStart:RegionNameandTableEnd:RegionName. - Image fields such as
Image:LogoStreamwith PNG byte values. - Word
IFfield structures used in mail-merge templates. - Headers, footers, text boxes, VML/DrawingML image placeholders, and common OpenXML field shapes.
It does not use the custom <<[Field]>> syntax. That syntax was only used by internal comparison tooling.
Data Model
MailMergeData.Fields supplies single-record values:
["OrderId"] = new MailMergeValue.Text("TPDF-ORDER-001")
MailMergeData.Regions supplies repeated rows for fields wrapped in TableStart: / TableEnd::
["Items"] = new[]
{
new Dictionary<string, MailMergeValue>
{
["Name"] = new MailMergeValue.Text("Line item"),
["Price"] = new MailMergeValue.Text("100 kr.")
}
}
For image merge fields, the template field is usually named Image:LogoStream, while the data key is LogoStream:
["LogoStream"] = new MailMergeValue.ImagePng(File.ReadAllBytes("logo.png"))
Runtime Lifecycle
Tpdf.Initialize() is optional. Call it during application startup if you want to warm up the runtime and catch startup/configuration errors early. If you skip it, the first conversion initializes the runtime automatically.
Tpdf.Initialize();
TPDF uses a process-lifetime LibreOffice runtime singleton and does not spawn soffice. Conversions are serialized internally to respect the embedded LibreOffice runtime contract.
Printer access is disabled by default before LibreOffice starts, so hidden server-side conversions do not block on disconnected default or network printers. Test harnesses that deliberately need printer-dependent layout can opt in before Tpdf.Initialize() or the first conversion:
Tpdf.SetPrinterAccessEnabled(true);
Tpdf.Initialize();
The same opt-in is available from native code as tpdf_set_printer_access_enabled(1) or from tpdf-test with --enable-printer-access.
Tpdf.Shutdown() exists for API completeness, but native runtime unload is not currently supported.
Deployment Notes
The NuGet package copies:
TPDF.Managed.dllTPDF.MailMerge.dlllibtpdf.soon Linux, ortpdf.dllon Windows- the sibling
tpdf-runtimefolder required by the converter
For app deployment, publish/copy the application output folder with those runtime files intact.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.4.22 | 326 | 6/26/2026 |
| 0.4.21 | 113 | 6/26/2026 |
| 0.4.20 | 129 | 6/22/2026 |
| 0.4.19 | 138 | 6/19/2026 |
| 0.4.18 | 118 | 6/17/2026 |
| 0.4.17 | 113 | 6/15/2026 |
| 0.4.16 | 106 | 6/11/2026 |
| 0.4.15 | 101 | 6/11/2026 |
| 0.4.14 | 110 | 6/11/2026 |
| 0.4.13 | 115 | 6/10/2026 |
| 0.4.12 | 107 | 6/10/2026 |
| 0.4.11 | 108 | 6/10/2026 |
| 0.4.10 | 115 | 6/8/2026 |
| 0.4.9 | 102 | 6/8/2026 |
| 0.4.8 | 107 | 6/8/2026 |
| 0.4.7 | 107 | 6/8/2026 |
| 0.4.6 | 120 | 5/25/2026 |
| 0.4.5 | 101 | 5/23/2026 |
| 0.4.4 | 104 | 5/23/2026 |
| 0.4.3 | 106 | 5/23/2026 |