Skip to footer content
USING IRONPDF

IronPDF: .NET PDF Library

IronPDF is a .NET library that makes it easy to generate, edit, and merge PDF files in C# and VB.NET. With IronPDF, you can create PDF in C# from scratch or convert existing HTML, CSS, and JavaScript files into PDFs. You can also add headers, footers, watermarks, and page numbers to PDF files. If you need to collaborate with others on a project, IronPDF makes it easy to merge multiple PDF files into one.

Throughout this article, IronPDF will be used to show how to convert an Excel document into a PDF document using C# and .NET Core.

Prerequisites

There are some prerequisites to convert an XLSX file to a PDF document.

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

Let's move to the main steps to convert an Excel file to PDF.

Step 1: Export your Excel Workbooks as HTML

To export your Excel file to HTML format, do the following steps:

  1. Launch Microsoft Excel and load the Excel spreadsheets there.

    Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 1: Load Excel Spreadsheets Load Excel Spreadsheets

  2. Select "Save As" from the "File" menu.

    Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 2: SaveAs Option SaveAs Option

  3. Click on the 'Browse' button and navigate to your desired location. From the File type dropdown, select the 'HTML Page'.

    Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 3: File Type Selection File Type Selection

Your Excel file will be converted to an HTML file if you complete the procedures above. The exported HTML file can now be used to convert to PDF.

Step 2: Add IronPDF to your solution

The next step is to install IronPDF in this project using the NuGet Package Manager Console. Click on the Tools from the top menu and select the NuGet Package Manager followed by "Package Manager Console".

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 4: NuGet Package Manager NuGet Package Manager

In the Console, write the following command to install IronPDF.

Install-Package IronPdf

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

Step 3: Code Examples to Convert Excel file to PDF

Open the Program.cs file and write the following code fragments to convert the Excel file to PDF using the IronPDF C# library.

Step 3.1: Include IronPDF

Import the IronPDF in the code file to use it in the program.

using IronPdf;
using IronPdf;
Imports IronPdf
$vbLabelText   $csharpLabel

The above code enables IronPDF to take part in the code.

Step 3.2: Instantiate a ChromePdfRenderer Object

Instantiate a ChromePdfRenderer object in the program. It'll enable the use of IronPDF to convert the HTML file to PDF.

var renderer = new ChromePdfRenderer();
var renderer = new ChromePdfRenderer();
Dim renderer = New ChromePdfRenderer()
$vbLabelText   $csharpLabel

Step 3.3: Convert the HTML (Exported from Excel File) to PDF

Use the RenderHtmlFileAsPdf function to convert HTML files to PDFs. In the parameter list, pass the HTML file path. IronPDF will read the contents of the HTML file and convert it into a PDF document.

var document = renderer.RenderHtmlFileAsPdf(@"C:\Users\Administrator\Desktop\Inventory.html");
var document = renderer.RenderHtmlFileAsPdf(@"C:\Users\Administrator\Desktop\Inventory.html");
Dim document = renderer.RenderHtmlFileAsPdf("C:\Users\Administrator\Desktop\Inventory.html")
$vbLabelText   $csharpLabel

IronPDF also supports adding passwords to the PDF file, preventing the PDF file from editing, opening, printing, and other operations. Let's have a look at how to add passwords using IronPDF.

Add Password to PDF File

Use the SecuritySettings property on the PDF document created in the previous step to set a password. It helps to secure the PDF file from data breaches.

document.SecuritySettings.UserPassword = "editable";
document.SecuritySettings.UserPassword = "editable";
document.SecuritySettings.UserPassword = "editable"
$vbLabelText   $csharpLabel

Step 3.4: Save the PDF File

Now, it's time to save the generated PDF file on the local machine by using the following code.

document.SaveAs("C:\\excelToPdf.pdf");
document.SaveAs("C:\\excelToPdf.pdf");
document.SaveAs("C:\excelToPdf.pdf")
$vbLabelText   $csharpLabel

Output PDF File

When you try to open the file, it'll require a password.

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 5: Enter Password for File Enter Password for File

After entering the password, you can see the converted PDF file from an Excel file. IronPDF beautifully converts Excel documents to PDFs while retaining the original formatting.

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 6: Output PDF File Output PDF File

You can see all the worksheets there. You can open any worksheet by clicking on any worksheet name.

Convert Excel to PDF in C# (Step-by-Step) Tutorial, Figure 6: PDF Worksheets PDF Worksheets

Summary

IronPDF is an easy-to-use PDF converter that can be used in C# .NET. In this blog post, you were shown how to convert Excel to PDF using IronPDF. If you want to learn more about the capabilities of IronPDF or see some examples of how it can be used, check out some of the other articles on how to create a PDF file from HTML.

You can start with a free trial of IronPDF, and then purchase IronPDF for as little as $749. Or, if you need more than one Iron Software product, you can buy the Iron Suite bundle of five software programs for the price of two.

Frequently Asked Questions

Why should I convert Excel files to PDF?

Converting Excel files to PDF ensures that the data cannot be altered, allows the file to be viewed on any device or program, and makes it possible to password-protect the file, preventing unauthorized access.

What is a .NET library that makes it easy to generate, edit, and merge PDF files?

IronPDF is a .NET library that makes it easy to generate, edit, and merge PDF files in C# and VB.NET. It can convert Excel files to PDF while retaining the original formatting.

What are the prerequisites for converting an Excel file to PDF using a C# library?

You need Visual Studio 2022, a running .NET application with the latest .NET Framework, Microsoft Office installed, and a stable internet connection to install the IronPDF library.

How do I install a C# library to convert Excel files to PDF?

You can install IronPDF using the NuGet Package Manager Console in Visual Studio. Use the command 'Install-Package IronPdf' to add it to your project.

What are the steps to convert an Excel file to PDF using a C# library?

First, export the Excel file as an HTML file. Then, install IronPDF and use it to convert the HTML file to a PDF using the RenderHtmlFileAsPdf method. Optionally, add a password and save the PDF.

Can I add a password to the PDF file created with a C# library?

Yes, you can use the SecuritySettings property on the PDF document to set a user password, which helps secure the PDF file from unauthorized access.

Does a C# library preserve the original formatting of the Excel file when converting to PDF?

Yes, IronPDF beautifully converts Excel documents to PDFs while retaining the original formatting.

How do I save the PDF file after conversion using a C# library?

Use the SaveAs method on the PDF document object to save the generated PDF file to your local machine.

Can I try a C# library for PDF conversion before purchasing?

Yes, you can start with a free trial of IronPDF and purchase it later if it meets your needs.

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.