How to Convert DOCX to PDF Using C#

How to Convert Microsoft Word to PDF in C#

IronPDF enables C# developers to convert DOCX files to PDF format using the DocxToPdfRenderer class with a simple one-line method call, preserving all formatting and supporting Mail Merge functionality for batch document generation.

A DOCX file is a document created in Microsoft Word, a word processing program within the Microsoft Office suite provided by Microsoft. It uses the Office Open XML (OOXML) standard, making it efficient and compatible with various software. It's the default format for Word documents since Word 2007, replacing the older DOC format when it was originally published. The DOCX format offers several advantages including smaller file sizes through ZIP compression, better data recovery capabilities, and improved compatibility across different platforms.

IronPDF has the ability to convert Word documents to PDF files instantly, along with providing a Mail Merge feature for generating personalized batches of documents for individual recipients. Converting from DOCX to PDF ensures universal compatibility, preserves formatting, and adds a layer of security. This functionality is particularly useful when you need to distribute documents that should maintain their exact appearance across different devices and operating systems, similar to creating PDFs from HTML or converting images to PDF.

Quickstart: Convert DOCX to PDF Using IronPDF

Effortlessly convert DOCX files to PDF in C# with IronPDF. This quick guide demonstrates how to use the DocxToPdfRenderer class and the RenderDocxAsPdf method for a seamless conversion in just one line of code. Perfect for developers looking to streamline document processing, ensuring that the resulting PDF retains all original formatting and compatibility.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.DocxToPdfRenderer()
        .RenderDocxAsPdf("document.docx")
        .SaveAs("output.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer


How Do I Convert a DOCX File to PDF?

IronPDF allows developers to convert Word documents to PDF programmatically, streamlining efficiency and allowing them to integrate into existing .NET applications or any cross-platform .NET framework applications they might have. The library supports various installation methods including NuGet packages and can be deployed across multiple platforms including Windows, Linux, and macOS.

The conversion process to convert a Microsoft Word file to PDF format is simple. We first instantiate the DocxToPdfRenderer class. Utilize the RenderDocxAsPdf method of the DocxToPdfRenderer object by providing the filepath of the DOCX file. This method returns a PdfDocument object, allowing you to customize the PDF further, including adding headers and footers, applying watermarks, or setting security options. You can download the Modern Chronological Resume DOCX example file.

What Does the Original Document Look Like?

Microsoft Word displaying a modern pharmacist resume with red accents in chronological format

How Do I Write the C# Code?

Additionally, the RenderDocxAsPdf method also accepts DOCX data as bytes and streams. This flexibility allows you to work with documents from various sources, whether they're stored on disk, in memory, or retrieved from a database. The method handles all the complex conversion logic internally, ensuring that fonts, images, tables, and formatting are accurately preserved.

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-from-file.cs
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
using IronPdf;

// Instantiate Renderer
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render from DOCX file
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the PDF
pdf.SaveAs("pdfFromDocx.pdf");
$vbLabelText   $csharpLabel

For more advanced scenarios, you can also convert from byte arrays or streams:

// Convert from byte array
byte[] docxBytes = File.ReadAllBytes("document.docx");
PdfDocument pdfFromBytes = renderer.RenderDocxAsPdf(docxBytes);

// Convert from stream
using (FileStream stream = new FileStream("document.docx", FileMode.Open))
{
    PdfDocument pdfFromStream = renderer.RenderDocxAsPdf(stream);
}
// Convert from byte array
byte[] docxBytes = File.ReadAllBytes("document.docx");
PdfDocument pdfFromBytes = renderer.RenderDocxAsPdf(docxBytes);

// Convert from stream
using (FileStream stream = new FileStream("document.docx", FileMode.Open))
{
    PdfDocument pdfFromStream = renderer.RenderDocxAsPdf(stream);
}
$vbLabelText   $csharpLabel

What Will the Converted PDF Look Like?

The resulting PDF maintains all the original formatting, including fonts, colors, layout, and embedded images. This makes IronPDF ideal for generating professional documents that need to be shared or archived in a universally accessible format.


How Can I Use Mail Merge for Batch PDF Generation?

Mail Merge, located on the "Mailings" tab in Microsoft Word, allows you to create a batch of documents with personalized information for each recipient or data entry. It's often used to generate personalized letters, envelopes, labels, or email messages, such as invitations, newsletters, or form letters, where much of the content is the same, but certain details vary for each recipient. This feature is particularly powerful when combined with IronPDF's ability to merge multiple PDFs or generate documents with custom metadata.

What Data Model Should I Create?

First, let's create a model to store the information that will be mail-merged into its corresponding placeholder. The property names in your data model must match the merge field names in your Word template exactly (case-sensitive).

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-mail-merge-model.cs
internal class RecipientsDataModel
{
    public string Date { get; set; }
    public string Location{ get; set; }
    public string Recipients_Name { get; set; }
    public string Contact_Us { get; set; }
}
internal class RecipientsDataModel
{
    public string Date { get; set; }
    public string Location{ get; set; }
    public string Recipients_Name { get; set; }
    public string Contact_Us { get; set; }
}
internal class RecipientsDataModel
{
    public string Date { get; set; }
    public string Location{ get; set; }
    public string Recipients_Name { get; set; }
    public string Contact_Us { get; set; }
}
$vbLabelText   $csharpLabel

I have modified a template provided by Microsoft Word for our purposes. Please download the Party Invitation DOTX example file. For our use case, let's set the MailMergePrintAllInOnePdfDocument property to true, which combines the PDFs into a single PdfDocument object. The merge fields that we are going to use are Date, Location, Recipient's Name, and Contact Us.

What Does the Mail Merge Template Look Like?

Word mail merge party invitation template showing merge fields and Mailings ribbon with mail merge tools

How Do I Implement the Mail Merge Code?

The Mail Merge functionality in IronPDF supports various rendering options that can be configured through the DocxPdfRenderOptions class. This allows you to control aspects like page orientation, margins, and whether to combine all merged documents into a single PDF or generate separate files.

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-mail-merge.cs
using IronPdf;
using System.Collections.Generic;
using System.Linq;

var recipients = new List<RecipientsDataModel>()
    {
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Olivia Smith",
            Contact_Us = "support@ironsoftware.com"
        },
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Ethan Davis",
            Contact_Us = "support@ironsoftware.com"
        },
    };

DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Apply render options
DocxPdfRenderOptions options = new DocxPdfRenderOptions();

// Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = true;

// Convert DOTX to PDF
var pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
     recipients,
     "Party-invitation.dotx",
     options);

pdfs.First().SaveAs("mailMerge.pdf");
using IronPdf;
using System.Collections.Generic;
using System.Linq;

var recipients = new List<RecipientsDataModel>()
    {
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Olivia Smith",
            Contact_Us = "support@ironsoftware.com"
        },
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Ethan Davis",
            Contact_Us = "support@ironsoftware.com"
        },
    };

DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Apply render options
DocxPdfRenderOptions options = new DocxPdfRenderOptions();

// Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = true;

// Convert DOTX to PDF
var pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
     recipients,
     "Party-invitation.dotx",
     options);

pdfs.First().SaveAs("mailMerge.pdf");
using IronPdf;
using System.Collections.Generic;
using System.Linq;

var recipients = new List<RecipientsDataModel>()
    {
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Olivia Smith",
            Contact_Us = "support@ironsoftware.com"
        },
        new RecipientsDataModel()
        {
            Date ="Saturday, October 15th, 2023",
            Location="Iron Software Cafe, Chiang Mai",
            Recipients_Name="Ethan Davis",
            Contact_Us = "support@ironsoftware.com"
        },
    };

DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Apply render options
DocxPdfRenderOptions options = new DocxPdfRenderOptions();

// Configure the output PDF to be combined into a single PDF document
options.MailMergePrintAllInOnePdfDocument = true;

// Convert DOTX to PDF
var pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
     recipients,
     "Party-invitation.dotx",
     options);

pdfs.First().SaveAs("mailMerge.pdf");
$vbLabelText   $csharpLabel

What Does the Final Merged PDF Look Like?

Once the PDF document is created, you have the flexibility to make additional changes. These include exporting it as PDF/A or PDF/UA for archival and accessibility compliance, as well as adding a digital certificate for document authentication. You can also manipulate individual pages by merging or splitting PDFs, rotating pages, and you have the option to apply annotations and bookmarks for better document navigation.

Aside from those features, IronPDF also supports converting to other document formats. For example, you can convert Markdown to PDF, convert RTF to PDF, or even export PDFs back to HTML. For additional information regarding the other functionality that IronPDF has to offer, please refer to the comprehensive HTML to PDF tutorial.

Frequently Asked Questions

How do I convert a DOCX file to PDF in C#?

With IronPDF, you can convert DOCX to PDF in just one line of code using the DocxToPdfRenderer class. Simply instantiate the DocxToPdfRenderer, call the RenderDocxAsPdf method with your DOCX file path, and save the output using SaveAs method.

What is the DocxToPdfRenderer class?

The DocxToPdfRenderer is IronPDF's specialized class for converting Microsoft Word documents to PDF format. It provides a simple API that allows developers to convert DOCX files while preserving all original formatting, layouts, and document properties.

Does the Word to PDF conversion preserve formatting?

Yes, IronPDF's DocxToPdfRenderer preserves all formatting from the original Word document, including fonts, styles, images, tables, and layouts, ensuring the PDF output matches the original DOCX file exactly.

Can I perform Mail Merge when converting DOCX to PDF?

Yes, IronPDF supports Mail Merge functionality, allowing you to generate personalized batches of PDF documents from a single DOCX template. This is particularly useful for creating customized documents for individual recipients while maintaining consistent formatting.

What are the main steps to convert DOCX to PDF?

The process involves 5 simple steps with IronPDF: 1) Download the C# library, 2) Prepare your DOCX file, 3) Instantiate the DocxToPdfRenderer class, 4) Use the RenderDocxAsPdf method with your file path, and 5) Optionally utilize Mail Merge for batch processing.

Why should I convert Word documents to PDF format?

Converting to PDF using IronPDF ensures universal compatibility across all devices and platforms, preserves exact formatting and layout, adds document security features, and prevents unwanted editing. PDFs maintain their appearance regardless of the viewing software or operating system.

Is the DOCX to PDF conversion compatible with different platforms?

Yes, IronPDF supports cross-platform deployment and can be used on Windows, Linux, and macOS. The library works with various .NET framework applications and offers multiple installation methods including NuGet packages.

What types of Word documents can be converted?

IronPDF can convert DOCX files created in Microsoft Word 2007 or later that use the Office Open XML (OOXML) standard. This includes documents with complex formatting, images, tables, and other advanced Word features.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Reviewed by
Jeff Fritz
Jeffrey T. Fritz
Principal Program Manager - .NET Community Team
Jeff is also a Principal Program Manager for the .NET and Visual Studio teams. He is the executive producer of the .NET Conf virtual conference series and hosts 'Fritz and Friends' a live stream for developers that airs twice weekly where he talks tech and writes code together with viewers. Jeff writes workshops, presentations, and plans content for the largest Microsoft developer events including Microsoft Build, Microsoft Ignite, .NET Conf, and the Microsoft MVP Summit
Ready to Get Started?
Nuget Downloads 17,012,929 | Version: 2025.12 just released