Pacem.OpenXml.OfficeWord.Pdf
1.1.1
dotnet add package Pacem.OpenXml.OfficeWord.Pdf --version 1.1.1
NuGet\Install-Package Pacem.OpenXml.OfficeWord.Pdf -Version 1.1.1
<PackageReference Include="Pacem.OpenXml.OfficeWord.Pdf" Version="1.1.1" />
<PackageVersion Include="Pacem.OpenXml.OfficeWord.Pdf" Version="1.1.1" />
<PackageReference Include="Pacem.OpenXml.OfficeWord.Pdf" />
paket add Pacem.OpenXml.OfficeWord.Pdf --version 1.1.1
#r "nuget: Pacem.OpenXml.OfficeWord.Pdf, 1.1.1"
#:package Pacem.OpenXml.OfficeWord.Pdf@1.1.1
#addin nuget:?package=Pacem.OpenXml.OfficeWord.Pdf&version=1.1.1
#tool nuget:?package=Pacem.OpenXml.OfficeWord.Pdf&version=1.1.1
Office Word Extensions
About
Pacem.OpenXml.OfficeWord.Pdf provides a lightweight set
of utilities that ease the conversion of .docx documents into PDFs.
This library depends on
Pacem.OpenXml.OfficeWord. Please, check its documentation first.
The following paragraphs exemplify the library utilization.
Getting started
Inject the IOfficeWordPdfConverter singleton service into the DI container.
Currently, there's no other [than the dependency injection] way to reach the instance of the processor service.
Like this:
// Program.cs
// You can inject your own instance of `IOfficeWordPdfConverter`
services.AddPacemOfficeWordProcessor()
.WithPdfConverter<MyConverterType>();
Or you can lean on the - actually sole - implementation
of IOfficeWordPdfConverter, which relies on Microsoft Graph API:
the document is uploaded to a user's OneDrive, downloaded as PDF and then deleted.
You need a user licensed with OneDrive (Microsoft 365) and a Microsoft Entra application granted the
Files.ReadWrite.Allapplication permission on Microsoft Graph (admin consent).
// Program.cs
services.AddPacemOfficeWordProcessor()
.WithMicrosoftGraphPdfConverter(options => {
options.TenantId = "00000000-0000-0000-0000-000000000000"; // your tenant id
options.ClientId = "00000000-0000-0000-0000-000000000000"; // your application id
options.ClientSecret = "Sneaky~Password"; // your application secret
options.UserId = "00000000-0000-0000-0000-000000000000"; // id (or principal name) of the OneDrive user
});
Or bind the options from configuration:
// Program.cs
services.AddPacemOfficeWordProcessor()
.WithMicrosoftGraphPdfConverter(builder.Configuration.GetSection("GraphApi").Bind);
// appsettings.json
{
"GraphApi": {
"TenantId": "...",
"ClientId": "...",
"ClientSecret": "...",
"UserId": "..."
}
}
All four values are required: a missing one fails with an OptionsValidationException
when the converter is first resolved.
The application token (client_credentials) is requested, cached and renewed by Pacem.Net.Http,
through the named HttpClient Pacem.OpenXml.OfficeWord.Pdf.MicrosoftGraph:
the converter is safe to use concurrently.
Throttled requests (429, 503) are retried twice, waiting what Microsoft Graph asks (Retry-After);
failures surface as Pacem.Net.Http.HttpApiException (status code and response body).
The uploaded document is deleted even when the conversion fails.
Then use it in your services, like this:
namespace Pacem.OpenXml.OfficeWord;
public class MyController : ControllerBase
{
private readonly IOfficeWordProcessor _word;
private readonly IOfficeWordPdfConverter _pdf;
public MyController(IOfficeWordProcessor word, IOfficeWordPdfConverter pdf)
{
_word = word;
_pdf = pdf;
}
// [...]
}
Usage
For a comprehensive list of usage example, please refer to
Pacem.OpenXml.OfficeWorddocumentation.
Here's a simple find and replace code sample.
The important part is the conversion function, which is the last instruction in the code sample.
string root = AppContext.BaseDirectory;
string source = Path.Combine(root, "file1.docx");
string target = Path.Combine(root, "file1-replaced.pdf");
await _word.Open(source)
.Find("[_my_placeholder_]")
.ReplaceWith("Hello, World!")
.FlushPdfAsync(_pdf, target); // an existing file gets replaced
The PDF can be written straight into a response body, and every conversion accepts a CancellationToken:
[HttpGet("invoice")]
public async Task GetInvoiceAsync(CancellationToken cancellationToken)
{
Response.ContentType = "application/pdf";
await _word.Open(source)
.Find("[_my_placeholder_]")
.ReplaceWith("Hello, World!")
.FlushPdfAsync(_pdf, Response.Body, cancellationToken);
}
Or get the bytes: byte[] pdf = await _pdf.ConvertToPdfAsync(document, cancellationToken);.
One converter per account
When documents must go through different accounts (e.g. every user converts through their own OneDrive),
use the IOfficeWordPdfConverterFactory, as you would with IStorageServiceFactory in Pacem.Storage.
// Program.cs: the factory alone (no default account)...
services.AddPacemOfficeWordProcessor()
.WithMicrosoftGraphPdfConverter();
// ...or a default account plus the factory
services.AddPacemOfficeWordProcessor()
.WithMicrosoftGraphPdfConverter(builder.Configuration.GetSection("GraphApi").Bind);
public class InvoicesController(IOfficeWordProcessor word, IOfficeWordPdfConverterFactory pdfs) : ControllerBase
{
[HttpGet("invoice")]
public async Task GetInvoiceAsync(CancellationToken cancellationToken)
{
IOfficeWordPdfConverter pdf = pdfs.CreateMicrosoftGraphPdfConverter(options =>
{
options.TenantId = "..."; // the user's tenant
options.ClientId = "..."; // the application registered (or consented) there
options.ClientSecret = "...";
options.UserId = "..."; // the user's object id or principal name
});
Response.ContentType = "application/pdf";
await word.Open(source)
.FlushPdfAsync(pdf, Response.Body, cancellationToken);
}
}
GetPdfConverter()returns the default converter, and throws when none was registered.- Created converters are cheap. Tokens are cached per tenant and application, not per converter, so users of the same tenant share one token.
- The application acts on the user's drive with its own
Files.ReadWrite.Allpermission, granted by each tenant's admin. It doesn't act with the user's delegated permission.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Microsoft.Extensions.Http (>= 11.0.0)
- Pacem.Net.Http (>= 1.1.1)
- Pacem.OpenXml.OfficeWord (>= 1.1.1)
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 |
|---|---|---|
| 1.1.1 | 95 | 11/18/2026 |
| 1.1.1-davinci | 94 | 11/15/2026 |
| 1.1.1-cauchy | 99 | 11/15/2026 |
| 1.1.1-bessel | 96 | 11/13/2026 |
| 1.1.0 | 97 | 11/11/2026 |
| 1.1.0-archimedes | 89 | 11/11/2026 |
| 1.0.2 | 105 | 11/9/2026 |
| 1.0.1 | 92 | 11/9/2026 |
| 1.0.0 | 97 | 11/9/2026 |
| 1.0.0-tartaglia | 97 | 11/5/2026 |
| 1.0.0-stokes | 97 | 11/4/2026 |
| 1.0.0-saint-venant | 105 | 11/4/2026 |
| 1.0.0-ricci | 100 | 10/28/2026 |
| 1.0.0-peano | 99 | 10/27/2026 |
| 1.0.0-maiorana | 101 | 10/27/2026 |
| 0.10.19 | 109 | 10/27/2026 |
| 0.10.19-galilei | 103 | 10/27/2026 |
| 0.10.19-fermi | 93 | 10/27/2026 |
| 0.10.19-degiorgi | 106 | 10/26/2026 |