How to Convert Word to PDF in C# (Tutorial)

Word-to-PDF conversion is currently an accepted practice and it can be used prior to sharing documents. You can use any online tool to convert Word document to PDF files. MS Word has built-in features for Word docx files into PDF conversion, but there may be situations where you want to rewrite Word documents into PDF programmatically, like:

  • Generate PDF on the server from a Word doc.
  • MS Word is not licensed on the server. So, we've to use C# libraries for converting Word to PDF.
  • Create customized PDF with popular formats like tickets, invoices, management reports etc.
  • Design or edit a PDF template in MS Word and then create custom copies on a web server with customized information.

Let's see how we can convert DOCX or doc to PDF using a C# PDF Library.

IronPDF: C# PDF Library

IronPDF is a C# PDF library that makes it easy for .NET developers to create and manipulate PDFs. With IronPDF, you can easily convert HTML content into a PDF in C#. The IronPDF library also includes support for digital signatures, form filling, PDF-to-image conversion and more.

Whether you need to generate PDFs for your web application, or simply want to add some PDF functionality to your existing .NET app, IronPDF as a .NET API. Check out our comprehensive documentation to get started today for converting MS Office Word DOCX to PDF.

IronPDF will be utilized throughout this article to demonstrate how to convert and save word document into a PDF document using C# and .NET.

Prerequisites

There are some prerequisites to converting word file to a PDF document.

  1. Visual Studio 2022 (Recommended)
  2. A running .NET application system with the latest .NET Framework (Recommended)
  3. Microsoft Office installed
  4. A stable internet connection to install the IronPDF library for PDF conversion

Let's move to the main steps to convert word files to PDF documents.

Step 1: Export your Word document as HTML

As the first step, we've to convert word document to HTML then, we'll use the exported HTML to convert it to a PDF document.

To export your DOC or DOCX file to HTML format, do the following:

  1. Launch Microsoft Word and open the Word file.

    How to Convert Word Documents to PDF Files Using C# - Figure 1: Load Sample Word Document

    Load Sample Word Document

  2. Go to the "File" tab and select "Save As" from the side menu.

    How to Convert Word Documents to PDF Files Using C# - Figure 2: Save As Option

    Save As Option

  3. Click on the Browse button. Select desired location and from the File type dropdown, choose the "HTML Page" option.

    How to Convert Word Documents to PDF Files Using C# - Figure 3: Save File

    Save File

By following the above steps, your Word file will convert to an HTML file. Now, we'll use the exported HTML file for PDF conversion.

Step 2: Add IronPDF to a Visual Studio Solution

NuGet Package Manager

In Visual Studio, right-click on your project's Solution Explorer and select `Manage NuGet Packages for Solution...`.

How to Convert Word Documents to PDF Files Using C# - Figure 4: NuGet Package Manager

NuGet Package Manager

From there, simply search for IronPDF and install the latest version of the library. Click OK on any dialog boxes that come up. This will also work just as well in VB.NET projects.

How to Convert Word Documents to PDF Files Using C# - Figure 5: Search for IronPDF

Search for IronPDF

Package Manager Console

Alternatively, in Visual Studio, navigate to the 'Tools' menu at the top and select 'NuGet Package Manager' and choose 'Package Manager Console' from the menu.

How to Convert Word Documents to PDF Files Using C# - Figure 6: Package Manager Console

Package Manager Console

In the shell that opens, paste the following and press enter:

Install-Package IronPdf

This command will install the latest version of IronPDF in the project.

IronPDF on NuGet Website

For a comprehensive rundown of IronPDF's features, compatibility and downloads, please check out IronPDF on NuGet's official website.

Install via DLL

Another option is to install the IronPDF DLL directly. IronPDF can be downloaded and manually installed to the project or GAC from the following link.

Step 3: Replace placeholder text, add Headers, and Watermarks

Now it's time to convert new document files to PDF programmatically. Open the "Program.cs" file and write the code from the following example. Using the following code, we can add an HTML header and a watermark image in the center of the document.

Include IronPDF

We've to include IronPDF in our code file to utilize it.

using IronPdf;
var ironRenderer = new ChromePdfRenderer();  
using IronPdf;
var ironRenderer = new ChromePdfRenderer();  
Imports IronPdf
Private ironRenderer = New ChromePdfRenderer()
VB   C#

Add HTML Header

HTML Fragment helps to add an HTML string at the header of the PDF file. You can set multiple rendering options like header, footer, margin, paper size, and much more.

// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  
// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  
' Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>"
VB   C#

Convert HTML File to PDF

I use the RenderHtmlFileAsPdf function to convert the exported HMTL file from the word document to a PDF file. We pass the HTML file path in the method's argument.

// Add our word document as the source file and apply ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  
// Add our word document as the source file and apply ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  
' Add our word document as the source file and apply ironRenderer settings to it  
Dim pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html")
VB   C#

Apply a Stamp in the PDF Document

Next, we apply a Stamp to the generated PDF file using an image of our choosing.

// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg")); 
// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg")); 
' Adds a stamp  
pdf.ApplyStamp(New ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"))
VB   C#

Save the PDF

Now, we add the next line of code to save the generated PDF file to the disk:

pdf.SaveAs("word.pdf");
pdf.SaveAs("word.pdf");
pdf.SaveAs("word.pdf")
VB   C#

Just to recap, here is the full body of the program code we used:

using IronPdf;

var ironRenderer = new ChromePdfRenderer();  

// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  

// Adds our word document as the source file and applies ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  

// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"));  

// Saves the document to a PDF file
pdf.SaveAs("word.pdf");
using IronPdf;

var ironRenderer = new ChromePdfRenderer();  

// Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>";  

// Adds our word document as the source file and applies ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  

// Adds a stamp  
pdf.ApplyStamp(new ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"));  

// Saves the document to a PDF file
pdf.SaveAs("word.pdf");
Imports IronPdf

Private ironRenderer = New ChromePdfRenderer()

' Adds a header
ironRenderer.RenderingOptions.HtmlHeader.HtmlFragment = "<h1>Proudly created using IronPDF</h1>"

' Adds our word document as the source file and applies ironRenderer settings to it  
Dim pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html")

' Adds a stamp  
pdf.ApplyStamp(New ImageStamper("https://ironpdf.com/img/products/ironpdf-logo-text-dotnet.svg"))

' Saves the document to a PDF file
pdf.SaveAs("word.pdf")
VB   C#

By following the above method, we successfully converted a Word file to a PDF file without the help of Word Interop.

Output of the PDF File

Here, we can see the output PDF file converted from a Word document.

IronPDF perfectly converts documents to PDF with all formatting and stamp applied to them.

How to Convert Word Documents to PDF Files Using C# - Figure 7: IronPDF Output

IronPDF Output

Summary

If you’re looking for an easy way to convert your HTML documents into PDFs using C#, we recommend IronPDF. It’s a great library that makes the process simple and straightforward.

Check out more tutorials about how you can use IronPDF in your own projects. Thanks for reading!