USING IRONPDF

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

Updated February 25, 2024
Share:

Word-to-PDF conversion is currently an accepted practice and it can be used before sharing documents. You can use any online tool to convert Word documents to PDF files. Microsoft 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 document.
  • Microsoft Word is not licensed on the server. So, it is necessary to use C# libraries for converting Word to PDF.
  • Create customized PDFs 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 to 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 is a .NET API. Check out the comprehensive documentation to get started today for converting Microsoft Office Word DOCX to PDF.

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

Prerequisites

There are some prerequisites to converting a 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, a Word document is converted to HTML then, it will be used to convert 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 to PDF in C# (Tutorial), 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 to PDF in C# (Tutorial), Figure 2: Save As Option Save As Option

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

    How to Convert Word to PDF in C# (Tutorial), 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 to PDF in C# (Tutorial), 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 to PDF in C# (Tutorial), Figure 5: Search for IronPDF Search for IronPDF

Package Manager Console

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

How to Convert Word to PDF in C# (Tutorial), 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. Use the following code to add an HTML header and a watermark image in the center of the document.

Include IronPDF

To include IronPDF in our code file, add a namespace using IronPdf; 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

The RenderHtmlFileAsPdf function is used to convert the exported HMTL file from a Word document to a PDF file, then pass the HTML file path in the method's argument.

// Add the Word document as the source file and apply ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  
// Add the Word document as the source file and apply ironRenderer settings to it  
var pdf = ironRenderer.RenderHtmlFileAsPdf("Author.html");  
' Add the 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, apply a Stamp to the generated PDF file using a sample image.

// 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, 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 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, it is simple to successfully convert a Word file to a PDF file without the help of Word Interop.

Output of the PDF File

The output PDF file converted from a Word document, is perfectly maintained with all formatting and stamp applied to them using IronPDF.

How to Convert Word to PDF in C# (Tutorial), 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. Some of the important features include:

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

< PREVIOUS
PDF Viewer C# Windows Application (Tutorial)
NEXT >
How to Read PDF Files in C#

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,622,374 View Licenses >