Here's how to convert DOCX to PDF in one line of code:

new IronPdf.DocxToPdfRenderer()
       .RenderDocxAsPdf("path/to/word/file.docx")
       .SaveAs("docxToPdf.pdf");
Install with NuGet
green arrow pointer

PM >  Install-Package IronPdf


Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Convert DOCX file to PDF Example

To convert a Microsoft Word file to PDF, 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. I've used the "Modern Chronological Resume" template from Microsoft Word as an example. You can download the Modern Chronological Resume DOCX example file.

Microsoft Word Preview

Microsoft Word Preview

Code Sample

Additionally, the RenderDocxAsPdf method also accepts DOCX data as bytes and streams.

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-from-file.cs
// Import the IronPdf library to handle PDF creation and conversion tasks
using IronPdf;

// Instantiate DocxToPdfRenderer to handle DOCX to PDF conversion
// This object will be used to perform the rendering process.
DocxToPdfRenderer renderer = new DocxToPdfRenderer();

// Render a PDF document from a DOCX file
// The RenderDocxAsPdf method takes the path of a DOCX file and converts it into a PDF document.
PdfDocument pdf = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx");

// Save the rendered PDF document to a file
// The SaveAs method is called on the PdfDocument object to save the converted file as a PDF.
pdf.SaveAs("pdfFromDocx.pdf");
' Import the IronPdf library to handle PDF creation and conversion tasks

Imports IronPdf



' Instantiate DocxToPdfRenderer to handle DOCX to PDF conversion

' This object will be used to perform the rendering process.

Private renderer As New DocxToPdfRenderer()



' Render a PDF document from a DOCX file

' The RenderDocxAsPdf method takes the path of a DOCX file and converts it into a PDF document.

Private pdf As PdfDocument = renderer.RenderDocxAsPdf("Modern-chronological-resume.docx")



' Save the rendered PDF document to a file

' The SaveAs method is called on the PdfDocument object to save the converted file as a PDF.

pdf.SaveAs("pdfFromDocx.pdf")
$vbLabelText   $csharpLabel

Output PDF


Mail Merge Example

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.

Model

First, let's create a model to store the information that will be mail-merged into its corresponding placeholder.

:path=/static-assets/pdf/content-code-examples/how-to/docx-to-pdf-mail-merge-model.cs
internal class RecipientsDataModel
{
    // Property for storing the date information in string format
    public string Date { get; set; }

    // Property for storing the location information in string format
    public string Location { get; set; }

    // Property for storing the recipient's name in string format
    public string RecipientsName { get; set; }

    // Property for storing the contact information in string format
    public string ContactUs { get; set; }
}
Friend Class RecipientsDataModel

	' Property for storing the date information in string format

	Public Property [Date]() As String



	' Property for storing the location information in string format

	Public Property Location() As String



	' Property for storing the recipient's name in string format

	Public Property RecipientsName() As String



	' Property for storing the contact information in string format

	Public Property ContactUs() As String

End Class
$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.

Microsoft Word Preview

Microsoft Word Preview

Code Sample

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

// Define the data model for recipient information.
public class RecipientsDataModel
{
    // Date of the event.
    public string Date { get; set; }

    // Location of the event.
    public string Location { get; set; }

    // Recipient's name.
    public string Recipients_Name { get; set; }

    // Contact information.
    public string Contact_Us { get; set; }
}

// List of recipient data for the mail merge.
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"
    },
};

// Instantiate a new DocxToPdfRenderer.
DocxToPdfRenderer docxToPdfRenderer = new DocxToPdfRenderer();

// Initialize rendering options.
DocxPdfRenderOptions options = new DocxPdfRenderOptions
{
    // Set the option to combine all documents into a single PDF.
    MailMergePrintAllInOnePdfDocument = true
};

// Perform the mail merge and convert the DOCX/DOTX template to PDF.
// This method will use the template "Party-invitation.dotx" to generate PDFs for each recipient.
IEnumerable<PdfDocument> pdfs = docxToPdfRenderer.RenderDocxMailMergeAsPdf<RecipientsDataModel>(
    recipients,
    "Party-invitation.dotx",
    options);

// Save the first generated PDF document to a file.
// Here, we assume that there is at least one PDF in the sequence.
pdfs.FirstOrDefault()?.SaveAs("mailMerge.pdf");
Imports IronPdf

Imports System.Collections.Generic

Imports System.Linq



' Define the data model for recipient information.

Public Class RecipientsDataModel

	' Date of the event.

	Public Property [Date]() As String



	' Location of the event.

	Public Property Location() As String



	' Recipient's name.

	Public Property Recipients_Name() As String



	' Contact information.

	Public Property Contact_Us() As String

End Class



' List of recipient data for the mail merge.

Private recipients = New List(Of RecipientsDataModel)() From {

	New RecipientsDataModel() With {

		.Date = "Saturday, October 15th, 2023",

		.Location = "Iron Software Cafe, Chiang Mai",

		.Recipients_Name = "Olivia Smith",

		.Contact_Us = "support@ironsoftware.com"

	},

	New RecipientsDataModel() With {

		.Date = "Saturday, October 15th, 2023",

		.Location = "Iron Software Cafe, Chiang Mai",

		.Recipients_Name = "Ethan Davis",

		.Contact_Us = "support@ironsoftware.com"

	}

}



' Instantiate a new DocxToPdfRenderer.

Private docxToPdfRenderer As New DocxToPdfRenderer()



' Initialize rendering options.

Private options As New DocxPdfRenderOptions With {.MailMergePrintAllInOnePdfDocument = True}



' Perform the mail merge and convert the DOCX/DOTX template to PDF.

' This method will use the template "Party-invitation.dotx" to generate PDFs for each recipient.

Private pdfs As IEnumerable(Of PdfDocument) = docxToPdfRenderer.RenderDocxMailMergeAsPdf(Of RecipientsDataModel)(recipients, "Party-invitation.dotx", options)



' Save the first generated PDF document to a file.

' Here, we assume that there is at least one PDF in the sequence.

If pdfs.FirstOrDefault() IsNot Nothing Then

	pdfs.FirstOrDefault().SaveAs("mailMerge.pdf")

End If
$vbLabelText   $csharpLabel

Output PDF

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

Frequently Asked Questions

What is DOCX format?

A DOCX file is a document created in Microsoft Word using the Office Open XML (OOXML) standard. It is the default format for Word documents since Word 2007.

Why convert DOCX to PDF?

Converting DOCX to PDF ensures universal compatibility, preserves formatting, and adds a layer of security to the document.

How can I convert a DOCX file to PDF using C#?

You can use a C# library like IronPDF's DocxToPdfRenderer class and its RenderDocxAsPdf method to convert a DOCX file to a PDF.

What is a PDF conversion library?

A PDF conversion library is a tool that allows developers to create, edit, and convert PDF documents in .NET applications.

Does the library support Mail Merge?

Yes, using IronPDF, you can take advantage of the Mail Merge feature to generate personalized documents for individual recipients.

Can I customize a PDF after converting from DOCX?

Yes, after converting a DOCX file to PDF using a library like IronPDF, you can further customize the PDF document.

What are some additional features of the library?

IronPDF allows exporting PDFs as PDF/A or PDF/UA, adding digital certificates, merging or splitting PDFs, and applying annotations and bookmarks.

Is there a sample DOCX file available for testing?

Yes, you can download the 'Modern Chronological Resume' DOCX example file from the provided link on the page.

Can the library handle DOCX data in streams?

Yes, the RenderDocxAsPdf method in IronPDF can accept DOCX data as bytes and streams.

Where can I download a C# PDF conversion library?

You can download a C# PDF conversion library, such as IronPDF, from the NuGet package manager.

Chaknith related to Output PDF
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?

Nuget Passed