Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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:
Let's see how to convert DOCX or doc to PDF using a 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.
There are some prerequisites to converting a Word file to a PDF document.
Let's move to the main steps to convert Word files to PDF documents.
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:
Launch Microsoft Word and open the Word file.
Load Sample Word document
Go to the "File" tab and select "Save As" from the side menu.
Save As Option
Click on the Browse button. Select the desired location and from the File type dropdown, choose the "HTML Page" option.
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.
In Visual Studio, right-click on your project's Solution Explorer and select Manage NuGet Packages for Solution....
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.
Search for IronPDF
Alternatively, in Visual Studio, navigate to the 'Tools' menu at the top, select 'NuGet Package Manager' and choose 'Package Manager Console' from the menu.
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.
For a comprehensive rundown of IronPDF's features, compatibility and downloads, please check out IronPDF on NuGet's official website.
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.
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.
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()
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>"
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")
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"))
Now, save the generated PDF file to the disk:
pdf.SaveAs("word.pdf");
pdf.SaveAs("word.pdf");
pdf.SaveAs("word.pdf")
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")
By following the above method, it is simple to successfully convert a Word file to a PDF file without the help of Word Interop.
The output PDF file converted from a Word document, is perfectly maintained with all formatting and stamp applied to them using IronPDF.
IronPDF Output
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!
9 .NET API products for your office documents