Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Being able to create PDF documents from various file types is a great way of making a compiled, easy-to-read version of your original file. For example, you might have your portfolio displayed as a web page but want to convert it into an easy-to-present PDF file to share with potential employers. Or perhaps you have charts chock full of essential data you want to display as part of a report, with a PDF converter, you could convert your report and charts to PDF, and combine them into one easily shared document.
Today, we will look at how to convert files to PDF format using C#. We will also be looking at IronPDF, a powerful .NET PDF library, and how it can be used for PDF conversion. Whether you're dealing with HTML, images, or other documents, IronPDF provides a simple and efficient way to create and manipulate PDF files programmatically.
IronPDF is an industry-standard library that simplifies PDF creation and conversion. It supports converting a wide range of formats to PDF such as HTML, images, and even other PDFs. Key features include:
Before we can begin converting files to PDF documents using IronPDF, we must install it and get it set up within our project. Thankfully, thanks to IronPDF's easy implementation, this can be done it no time. To install the IronPDF library, you can run the following command in the NuGet Package Manager Console:
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
Alternatively, you can install it using Manage NuGet Packages for Solution:
Now, we can look at how IronPDF tackles PDF conversion from different file types. As we touched on earlier, IronPDF is a powerful PDF library capable of handling PDF conversion from many file types. This makes it extremely versatile in situations where you need to convert more than just HTML to PDF, which is what many other PDF libraries focus on. With IronPDF, you're able to do all your conversions in one place, without the need to install additional libraries.
One of the most common scenarios for PDF conversion is converting HTML content into a PDF document, and IronPDF makes this process seamless. For this example, we'll be converting HTML string, but the process is similar for an HTML document, HTML page, or HTML files. First, we'll create a ChromePdfRenderer instance to render our HTML content as a PDF. Then, using renderHtmlAsPdf(), we can convert the HTML content to PDF, before finally saving the PDF.
using IronPdf;
class Program
{
static void Main(string[] args)
{
// HTML content
string html = "<h1>This Document was Converted using IronPDF</h1><p>This document was converted from HTML content</p>";
// Instantiate the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create our PDF from the HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the PDF
pdf.SaveAs("html-to-pdf.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// HTML content
string html = "<h1>This Document was Converted using IronPDF</h1><p>This document was converted from HTML content</p>";
// Instantiate the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create our PDF from the HTML content
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the PDF
pdf.SaveAs("html-to-pdf.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' HTML content
Dim html As String = "<h1>This Document was Converted using IronPDF</h1><p>This document was converted from HTML content</p>"
' Instantiate the ChromePdfRenderer class
Dim renderer As New ChromePdfRenderer()
' Create our PDF from the HTML content
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
' Save the PDF
pdf.SaveAs("html-to-pdf.pdf")
End Sub
End Class
Another common method of PDF conversion is converting URL to PDF. This way, we can capture entire web pages as a pixel-perfect PDF. Using the URL https://www.nuget.org/packages/IronPdf/, we will utilize the ChromePdfRenderer class and IronPDF's support for modern web standards to generate a high-quality PDF in just a few lines of code.
using System;
using IronPdf;
class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");
pdf.SaveAs("urlPdf.pdf");
}
}
using System;
using IronPdf;
class Program
{
static void Main(string[] args)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");
pdf.SaveAs("urlPdf.pdf");
}
}
Imports System
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer As New ChromePdfRenderer()
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/")
pdf.SaveAs("urlPdf.pdf")
End Sub
End Class
IronPDF also supports the conversion of image file types, such as PNG, JPG, and GIF, to PDFs. This is a great way of compiling multiple images into one document. For our example, we'll take three different images, each in a different image format, and convert them into new PDFs, before merging them into one PDF to demonstrate how this can be used to convert multiple images into the same PDF. The ImageToPdfConverter class is used to carry out the conversion of the images.
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Getting the image file paths
string jpgFile = "image-jpg.jpg";
string pngFile = "image-png.png";
string gifFile = "image-gif.gif";
// Converting the images to seperate PDF files
PdfDocument pdf_a = ImageToPdfConverter.ImageToPdf(gifFile);
PdfDocument pdf_b = ImageToPdfConverter.ImageToPdf(pngFile);
PdfDocument pdf_c = ImageToPdfConverter.ImageToPdf(jpgFile);
// Adding these newly converted PDFs into a list
List<PdfDocument> pdf_files = new List<PdfDocument>()
{
pdf_a,
pdf_b,
pdf_c,
};
// Merging the PDFs into one PDF with multiple PDF pages displaying each one
PdfDocument pdf = PdfDocument.Merge(pdf_files);
// Saving the PDF
pdf.SaveAs("Image-To-Pdf.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Getting the image file paths
string jpgFile = "image-jpg.jpg";
string pngFile = "image-png.png";
string gifFile = "image-gif.gif";
// Converting the images to seperate PDF files
PdfDocument pdf_a = ImageToPdfConverter.ImageToPdf(gifFile);
PdfDocument pdf_b = ImageToPdfConverter.ImageToPdf(pngFile);
PdfDocument pdf_c = ImageToPdfConverter.ImageToPdf(jpgFile);
// Adding these newly converted PDFs into a list
List<PdfDocument> pdf_files = new List<PdfDocument>()
{
pdf_a,
pdf_b,
pdf_c,
};
// Merging the PDFs into one PDF with multiple PDF pages displaying each one
PdfDocument pdf = PdfDocument.Merge(pdf_files);
// Saving the PDF
pdf.SaveAs("Image-To-Pdf.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Getting the image file paths
Dim jpgFile As String = "image-jpg.jpg"
Dim pngFile As String = "image-png.png"
Dim gifFile As String = "image-gif.gif"
' Converting the images to seperate PDF files
Dim pdf_a As PdfDocument = ImageToPdfConverter.ImageToPdf(gifFile)
Dim pdf_b As PdfDocument = ImageToPdfConverter.ImageToPdf(pngFile)
Dim pdf_c As PdfDocument = ImageToPdfConverter.ImageToPdf(jpgFile)
' Adding these newly converted PDFs into a list
Dim pdf_files As New List(Of PdfDocument)() From {pdf_a, pdf_b, pdf_c}
' Merging the PDFs into one PDF with multiple PDF pages displaying each one
Dim pdf As PdfDocument = PdfDocument.Merge(pdf_files)
' Saving the PDF
pdf.SaveAs("Image-To-Pdf.pdf")
End Sub
End Class
IronPDF handles DOCX to PDF conversion seamlessly, in just a couple lines of code. First, we will create a new instance of the DocxToPdfRenderer class, which we then use to convert the DOCX file using RenderDocxAsPdf(), before finally saving the PDF.
using IronPdf;
class Program
{
static void Main(string[] args)
{
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("Meeting notes.docx");
pdf.SaveAs("DocxToPdf.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
DocxToPdfRenderer renderer = new DocxToPdfRenderer();
PdfDocument pdf = renderer.RenderDocxAsPdf("Meeting notes.docx");
pdf.SaveAs("DocxToPdf.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim renderer As New DocxToPdfRenderer()
Dim pdf As PdfDocument = renderer.RenderDocxAsPdf("Meeting notes.docx")
pdf.SaveAs("DocxToPdf.pdf")
End Sub
End Class
In today’s digital landscape, converting documents into a universally accessible format like PDF is crucial for ensuring consistency, portability, and professional presentation. With IronPDF, integrating robust C# PDF converter capabilities into your applications is not only efficient but also incredibly flexible.
Throughout this tutorial, we demonstrated how easy it is to convert HTML file, web pages, images, URLs, and even DOCX files into polished, professional PDFs. But this is just the tip of the iceberg. IronPDF offers a wide array of features beyond basic conversion, such as:
Ready to take your document processing to the next level? Start experimenting with IronPDF today with its free trial, and unlock the full potential of PDF manipulation in your C# projects.
For further exploration of advanced features, optimizations, and practical examples, visit the official IronPDF documentation and join a community of developers transforming the way PDFs are handled in modern applications.
9 .NET API products for your office documents