Office to PDF C#

Convert Word, Excel, and PowerPoint to PDF in .NET applications with our NuGet package. Simple API, pixel-perfect output, no Microsoft Office required.

Document Tools

Office to PDF C# Overview

Convert Microsoft Office documents - Word, Excel, and PowerPoint - to PDF directly from your .NET applications with our C# library. Install the ConvertApi package from NuGet and convert docx, xlsx, pptx, and more with just a few lines of code - no Microsoft Office or interop required.

The library targets .NET Framework, .NET Core, and .NET 5/6/7/8, so it fits desktop apps, ASP.NET Core services, Azure Functions, and Blazor. Open password-protected files, preserve fonts and layout exactly, and produce PDF/A output for long-term archiving.

Conversions run on our SOC2-, ISO 27001-, HIPAA-, and GDPR-compliant infrastructure, with strongly-typed parameters and full IntelliSense to keep your integration fast and reliable.

Converting just one format? Use our dedicated Word to PDF, Excel to PDF, and PowerPoint to PDF libraries for C#.

Lightning Fast Conversions

Process and convert files in seconds with our high-performance cloud infrastructure.

Accuracy Guaranteed

Our advanced algorithms ensure pixel-perfect and content-accurate file conversions.

Enterprise-Grade Security

ISO 27001, HIPAA, and GDPR compliant with encrypted file processing.

Global Infrastructure

Strategically located servers ensure low latency and high availability worldwide.

Developer Friendly

Comprehensive SDKs and clear documentation for quick and simple integration.

Time-Saving Automation

Automate repetitive document workflows and focus on what matters most.

Customizable Parameters

Fine-tune your automation with these powerful conversion options

File

File Supported formats: .docx .doc .dot .dotx .wpd .rtf .log .potx .pps .ppsx .ppt .pptx .csv .xls .xlsb .xlsx .xltx

File to be converted. Value can be URL or file content.

Password

String

Sets the password to open protected documents.

Pdfa

Bool Default: False

Create PDF/A-1b compliant document.

StoreFile

Bool Default: False

When the StoreFile parameter is set to True, your converted file is written to ConvertAPI’s encrypted, temporary storage and made available via a time-limited secure download URL, valid for up to 3 hours. After this period, the file is permanently deleted.

When StoreFile is False, the conversion runs in memory and the file comes back in the API response: Base64 inside the JSON body by default, or as raw bytes when the request sends Accept: application/octet-stream. Nothing is written to disk or external storage, so only you can access the content.

Integrate using C#

Ready-to-run code samples for quick conversion and automation.

Convert Word, Excel and PowerPoint files to PDF in C#

Use the Office to PDF converter when you don't know in advance which kind of document you will get. You send a Word, Excel or PowerPoint file and get a PDF back. The API reads the file extension and runs the matching conversion, so one call works for all of these formats:

  • Word: .docx, .doc, .dot, .dotx, .rtf
  • Excel: .xlsx, .xls, .xlsb, .xltx, .csv
  • PowerPoint: .pptx, .ppt, .pps, .ppsx, .potx
  • Other text documents: .wpd (WordPerfect) and .log

If all your files are the same type, a dedicated converter gives you more settings:

  • Word to PDF: page ranges, tracked changes and comments, table of contents updates
  • Excel to PDF: worksheet selection, page size, fit to page
  • PowerPoint to PDF: page ranges, hidden slides, speaker notes

Install the package

dotnet add package ConvertApi

In the Visual Studio Package Manager Console, run Install-Package ConvertApi instead. The package targets .NET Standard 2.0, so it also works in .NET Framework 4.6.1 and later.

Each example below is a complete Program.cs file for .NET 8 or later. Create a console app with dotnet new console, add the package and paste the code.

Set your API token

Copy your API token from your ConvertAPI account. The examples read it from the CONVERTAPI_TOKEN environment variable, so the token stays out of your source code:

  • macOS and Linux: export CONVERTAPI_TOKEN="your_api_token"
  • Windows: setx CONVERTAPI_TOKEN "your_api_token", then open a new terminal

Convert one file

using ConvertApiDotNet;

var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

var result = await convertApi.ConvertAsync("office", "pdf",
    new ConvertApiFileParam("File", "report.docx"));

var saved = await result.SaveFilesAsync("pdf");
Console.WriteLine($"Saved {saved.First().FullName}");

The SDK uploads report.docx, converts it and saves pdf/report.pdf. The PDF keeps the name of the original file. To convert a spreadsheet or a presentation, change only the file name.

Convert a folder of mixed documents

This converter is most useful when files of different types arrive in one place, such as documents your users upload. This example converts every Office file in the inbox folder and skips everything else.

using ConvertApiDotNet;

var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

var officeExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    ".doc", ".docx", ".dot", ".dotx", ".rtf", ".wpd", ".log",
    ".xls", ".xlsx", ".xlsb", ".xltx", ".csv",
    ".ppt", ".pptx", ".pps", ".ppsx", ".potx"
};

Directory.CreateDirectory("pdf");

foreach (var path in Directory.EnumerateFiles("inbox"))
{
    var name = Path.GetFileName(path);
    if (!officeExtensions.Contains(Path.GetExtension(path)))
    {
        Console.WriteLine($"Skipped {name}");
        continue;
    }

    var result = await convertApi.ConvertAsync("office", "pdf",
        new ConvertApiFileParam("File", path));

    var target = Path.Combine("pdf", name + ".pdf");
    await result.Files[0].SaveFileAsync(target);

    Console.WriteLine($"{name} -> {target}");
}

Here is the output from a test run with five files:

notes.rtf -> pdf/notes.rtf.pdf
report.docx -> pdf/report.docx.pdf
Skipped readme.md
report.xlsx -> pdf/report.xlsx.pdf
kickoff.pptx -> pdf/kickoff.pptx.pdf

Each PDF keeps the original extension in its name, so report.docx and report.xlsx don't overwrite each other. The loop converts one file at a time. For a large folder, you can run a few conversions at once with Parallel.ForEachAsync.

Convert a file from a URL

If the document is already online, pass its URL instead of a local path. ConvertAPI downloads the file itself, so the original never passes through your server.

using ConvertApiDotNet;

var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));
var source = new Uri("https://cdn.convertapi.com/public/files/demo.pptx");

var result = await convertApi.ConvertAsync("office", "pdf",
    new ConvertApiFileParam("File", source));

var pdf = result.Files[0];
var saved = await pdf.SaveFileAsync(Path.Combine(Path.GetTempPath(), pdf.FileName));
Console.WriteLine($"Saved {saved.FullName}");

ConvertAPI must be able to open the URL without logging in. For files in a private S3 bucket or Azure Blob container, use a pre-signed or SAS URL.

Convert uploads in ASP.NET Core

This minimal API endpoint accepts an uploaded file and sends the PDF back in the response. Create the project with dotnet new web, add the package and replace Program.cs with this code:

using ConvertApiDotNet;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

app.MapPost("/to-pdf", async (IFormFile file) =>
{
    await using var upload = file.OpenReadStream();

    var result = await convertApi.ConvertAsync("office", "pdf",
        new ConvertApiFileParam(upload, file.FileName));

    var pdf = result.Files[0];
    return Results.File(await pdf.FileStreamAsync(), "application/pdf", pdf.FileName);
}).DisableAntiforgery();

app.Run();

Two details matter here:

  • Pass the original file name, file.FileName, to ConvertApiFileParam. The API uses its extension to choose the conversion, and it rejects a file without an extension.
  • ASP.NET Core checks antiforgery tokens on form uploads. DisableAntiforgery() turns that check off for an endpoint that other services call. If the upload form is on your own site, add the antiforgery middleware instead.

To try it, post a file with curl. Use the port that your app prints when it starts:

curl -F "file=@budget.xlsx" -o budget.pdf http://localhost:5000/to-pdf

Open password-protected files and create PDF/A

using ConvertApiDotNet;

var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

var result = await convertApi.ConvertAsync("office", "pdf",
    new ConvertApiFileParam("File", "salaries.docx"),
    new ConvertApiParam("Password", "document-password"),
    new ConvertApiParam("Pdfa", "true"));

await result.SaveFilesAsync("pdf");
  • Password opens a protected document. Replace document-password with the real password, which in most apps comes from the user or from a secret store.
  • Pdfa saves the result as a PDF/A file for long-term archiving.

These are the only document settings that the Office to PDF converter accepts. For anything else, use one of the dedicated converters listed at the top of this guide.

Handle errors

When a conversion fails, the SDK throws a ConvertApiException. Its Response property holds the API's error, with a code and a message that you can log or show to the user.

using ConvertApiDotNet;
using ConvertApiDotNet.Exceptions;

var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

try
{
    var result = await convertApi.ConvertAsync("office", "pdf",
        new ConvertApiFileParam("File", "salaries.docx"));
    await result.SaveFilesAsync("pdf");
}
catch (ConvertApiException e)
{
    Console.WriteLine($"Conversion failed with HTTP {(int)e.StatusCode}");
    Console.WriteLine(e.Response);
}

These are the errors you are most likely to see:

  • HTTP 401: the API token is missing or wrong.
  • Error code 5003: the file is password protected, and the request has no password or the wrong one.
  • Error code 4000: the file extension is not on the list above. The message lists the supported extensions.

Integrate within minutes

Easy Office to PDF automation using our simple C# SDK

Try the Office to PDF conversion online

Try it Free

Compatible With All .NET Frameworks & Tools

Compatible with Microsoft Azure Compatible with .NET Core Available on NuGet C# SDK Available Compatible with VS Code Compatible with JetBrains Rider Compatible with Visual Studio

Frequently Asked Questions

What is ConvertAPI C# SDK?

The ConvertAPI C# SDK is a lightweight, easy-to-use library for .NET developers to integrate document and file conversions into their applications with minimal code. It connects directly to the ConvertAPI REST service, allowing you to automate file conversions, merging, splitting, and more.

What types of conversions are supported?

ConvertAPI offers 300+ converters and tools, including DOCX to PDF, XLSX to PDF, PDF to JPG, HTML to PDF, image processing, metadata extraction, compression, and advanced document workflows. You can automate complex document processing scenarios within your .NET applications using the SDK.

Can I build complex conversion workflows using the SDK?

Yes, the ConvertAPI .NET C# SDK allows you to chain conversions, merge documents, extract pages, and apply advanced parameters to automate end-to-end document workflows within your applications.

Is there a file size limit when using ConvertAPI?

The maximum file size you can convert depends on your ConvertAPI plan type. Higher-tier plans allow larger files and increased concurrency for high-volume document processing needs.

Can I convert files entirely in memory without storing them on your servers?

Yes, ConvertAPI supports in-memory conversions, allowing you to send and receive files as streams without saving them to disk on ConvertAPI servers. This enables secure, diskless workflows for sensitive or temporary files.

Which .NET versions does the ConvertAPI SDK support?

The SDK supports .NET Framework 4.5+, .NET Core, .NET 5, 6, 7, and 8, ensuring compatibility across your existing and new projects.

Businesses trust us

Highest rated File Conversion API on major B2B software listing platforms: Capterra, G2, and Trustpilot.

"ConvertAPI has been a game-changer for our document automation workflows. Their conversion accuracy and API reliability are unmatched in the industry for over 7 years."

"ConvertAPI is a reliable, cost-effective solution with a proven track record of stability. It has grown significantly in maturity, adopting enterprise-grade practices over the years."

"We've integrated ConvertAPI across our entire document processing platform. The performance is exceptional and the support team is always responsive. Highly recommended!"

Enterprise-Grade Security

We ensure that all document processing is handled securely in the cloud, adhering to industry-leading standards like ISO 27001, GDPR, and HIPAA. To enhance security even further, we can ensure that no files or data are stored on our servers and never leave your country.

Learn more about security

Ready to Streamline Your File Conversions?