How to Get Started with the IronPDF C# PDF Library

IronPDF takes care of the difficult problem of adding PDF generation to your app, and automates turning formatted documents into a PDF.

  • Convert web forms, local HTML pages, and other web pages to PDF with .NET
  • Allow users to download documents, send them by email, or store them in the cloud.
  • Produce invoices, quotes, reports, contracts, and other documents.
  • Work with ASP.NET, ASP.NET Core, web forms, MVC, Web APIs on .NET Framework, and .NET Core.

Step 1

1. Install the IronPDF C# Library to your Project

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.

C# NuGet Library for PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip

Manually install into your project

Download DLL

1.1. Install with NuGet Package Manager

Install IronPDF in Visual Studio or at the command line with the NuGet Package Manager. In Visual Studio, navigate to the console with:

  • Tools ->
  • NuGet Package Manager ->
  • Package Manager Console
Install-Package IronPdf

And check out IronPDF on NuGet for more about version updates and installation.

There are other IronPdf NuGet Packages available for specific deployments to Linux, Mac, Azure and AWS targets which are documeted in our IronPDF advanced NuGet installation guide .

1.2. Directly Download the DLL

Alternatively, you can directly download the DLL.

Here are other IronPDF DLL zip packages available for specific platforms:

Remember to add this statement to the top of any cs class file using IronPDF:

using IronPdf;

1.3. Install and Deploy the Library

For more details, check the guide on how to install and deploy the IronPDF C# Library.

1.4A Optional: Linux Deployment

We officially support: Ubuntu, Debian, CentOS, Fedora & Amazon Linux 2.

1.4B Optional: Docker Deployment

We officially support Docker for: Windows, Ubuntu, Debian, CentOS & Amazon Linux 2 and provide working Docker files.

1.4C Optional: Azure Deployment

1.4D Optional: Amazon AWS Deployment

1.4E Optional: macOS Support


How to Tutorials

Apply License Key

Include this code at the startup of your application, before using IronPDF. This approach is universally effective and easy to implement.

IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-IRONPDF-LICENSE-KEY"
VB   C#

If you prefer not to apply the license key using inline code, please visit the 'IronPDF License Keys' article to explore alternative methods.

2. Render HTML String to PDF

IronPDF can render HTML text to PDF quite easily. This example illustrates the capability. Use this option when you only need to add simple text to your PDF document.

  • Create a new .NET Core console application
  • Install the NuGet package
  • Import the IronPdf namespace with the using keyword
  • Create a new ChromePdfRenderer renderer
  • Call RenderHtmlAsPdf and then SaveAs on the result.
:path=/static-assets/pdf/content-code-examples/get-started/get-started-1.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdf.SaveAs("html-string.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
pdf.SaveAs("html-string.pdf")
VB   C#

3. Convert HTML File to PDF

You can render HTML files with images, CSS, forms, hyperlinks, and JavaScript as a PDF document. Use this method for scenarios where you have access to the source document locally.

This example calls RenderHtmlFileAsPdf, which returns a variable called PDF.

Call SaveAs to save the output to a PDF file.

The sample assumes that there is a HTML file in the folder Assets.

:path=/static-assets/pdf/content-code-examples/get-started/get-started-2.cs
using IronPdf;

// Create a PDF from an existing HTML using C#
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlFileAsPdf("Assets/MyHTML.html");
pdf.SaveAs("MyPdf.pdf");
Imports IronPdf

' Create a PDF from an existing HTML using C#
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlFileAsPdf("Assets/MyHTML.html")
pdf.SaveAs("MyPdf.pdf")
VB   C#

4. Render Existing URL to PDF

Render existing web pages to PDFs in a few lines of C# or VB.NET code. Use this option when you need to convert a website that already has a well-formatted document to a PDF.

Call the RenderUrlAsPdf to download web page content so that you can call SaveAs to export the content locally.

:path=/static-assets/pdf/content-code-examples/get-started/get-started-3.cs
using IronPdf;

// Create a PDF from any existing web page
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
pdf.SaveAs("wikipedia.pdf");
Imports IronPdf

' Create a PDF from any existing web page
Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")
pdf.SaveAs("wikipedia.pdf")
VB   C#

5. ASP.NET Web Forms to PDF

Render ASP.NET web forms as PDF instead of HTML with a single line of code. Place the line of code in the Page_Load method of the page's code-behind.

  • Create a new ASP.NET WebForms application or open an existing one
  • Install the NuGet package
  • Import the IronPdf namespace with the using keyword
  • Open the code-behind for the page that you want to render to PDF. For example, Default.aspx.cs
  • Call RenderThisPageAsPdf on AspxToPdf
:path=/static-assets/pdf/content-code-examples/get-started/get-started-4.cs
using IronPdf;
using System;
using System.Web.UI;

namespace WebApplication
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
        }
    }
}
Imports IronPdf
Imports System
Imports System.Web.UI

Namespace WebApplication
	Partial Public Class _Default
		Inherits Page

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
			AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser)
		End Sub
	End Class
End Namespace
VB   C#

6. Route ASP MVC View to PDF

Route the user to a PDF document with the ASP MVC framework. Use this option when creating a new ASP MVC app or add an existing MVC controller to an app.

Start the new project wizard in Visual Studio, and choose ASP.NET Web Application (.NET Framework) -> MVC. Or open an existing MVC project. Open the file HomeController in the Controllers folder and replace the Index method, or add a new controller.

This is an example of how the code should look:

:path=/static-assets/pdf/content-code-examples/get-started/get-started-5.cs
using IronPdf;
using System;
using System.Web.Mvc;

namespace WebApplication8.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            using var PDF = IronPdf.ChromePdfRenderer.StaticRenderUrlAsPdf(new Uri("https://en.wikipedia.org"));
            return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf");
        }
        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            return View();
        }
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";
            return View();
        }
    }
}
Imports IronPdf
Imports System
Imports System.Web.Mvc

Namespace WebApplication8.Controllers
	Public Class HomeController
		Inherits Controller

		Public Function Index() As ActionResult
			Dim PDF = IronPdf.ChromePdfRenderer.StaticRenderUrlAsPdf(New Uri("https://en.wikipedia.org"))
			Return File(PDF.BinaryData, "application/pdf", "Wiki.Pdf")
		End Function
		Public Function About() As ActionResult
			ViewBag.Message = "Your application description page."
			Return View()
		End Function
		Public Function Contact() As ActionResult
			ViewBag.Message = "Your contact page."
			Return View()
		End Function
	End Class
End Namespace
VB   C#

7. Add Headers and Footers

The RenderingOptions property allows you to craft headers and footers for each page of the document. Access these options on the ChromePdfRenderer object. This sample works inside a .NET Core console app.

Use these template properties to build the content.

{page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}

:path=/static-assets/pdf/content-code-examples/get-started/get-started-6.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.FirstPageNumber = 1;

// Header options
renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
renderer.RenderingOptions.TextHeader.CenterText = "{url}";
renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica;
renderer.RenderingOptions.TextHeader.FontSize = 12;

// Footer options
renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Arial;
renderer.RenderingOptions.TextFooter.FontSize = 10;
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdf.SaveAs("html-string.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
renderer.RenderingOptions.FirstPageNumber = 1

' Header options
renderer.RenderingOptions.TextHeader.DrawDividerLine = True
renderer.RenderingOptions.TextHeader.CenterText = "{url}"
renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Helvetica
renderer.RenderingOptions.TextHeader.FontSize = 12

' Footer options
renderer.RenderingOptions.TextFooter.DrawDividerLine = True
renderer.RenderingOptions.TextHeader.Font = IronSoftware.Drawing.FontTypes.Arial
renderer.RenderingOptions.TextFooter.FontSize = 10
renderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
pdf.SaveAs("html-string.pdf")
VB   C#

7.1. Add Headers and Footers with HTML

As above, this sample works in a .NET Core console app. Specify HTML with the HtmlFragment property.

:path=/static-assets/pdf/content-code-examples/get-started/get-started-7.cs
using IronPdf;
using System;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Build a footer using html to style the text
// mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    MaxHeight = 30,
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};

// Build a header using an image asset
// Note the use of BaseUrl to set a relative path to the assets
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    MaxHeight = 30,
    HtmlFragment = "<img src='logo.jpg'>",
    BaseUrl = new Uri(@"C:\assets\images").AbsoluteUri
};
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdf.SaveAs("html-string.pdf");
Imports IronPdf
Imports System

Private renderer As New ChromePdfRenderer()

' Build a footer using html to style the text
' mergeable fields are:
' {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
	.MaxHeight = 30,
	.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
	.DrawDividerLine = True
}

' Build a header using an image asset
' Note the use of BaseUrl to set a relative path to the assets
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
	.MaxHeight = 30,
	.HtmlFragment = "<img src='logo.jpg'>",
	.BaseUrl = (New Uri("C:\assets\images")).AbsoluteUri
}
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
pdf.SaveAs("html-string.pdf")
VB   C#

8. Encrypt PDFs with a Password

Set the Password property of a PDF document to encrypt it and force the user to enter the correct password to view the document. This sample works in a .NET Core Console app

:path=/static-assets/pdf/content-code-examples/get-started/get-started-8.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdf.Password = "strong!@#pass&^%word";
pdf.SaveAs("secured.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
pdf.Password = "strong!@#pass&^%word"
pdf.SaveAs("secured.pdf")
VB   C#

9. Merge and Split PDF Documents

Use the Merge method to merge multiple PDF documents together, or CopyPages to split a number of pages out of an existing document. Include PDFs in your project as Content to access them by filename.

:path=/static-assets/pdf/content-code-examples/get-started/get-started-9.cs
using IronPdf;
using System.Collections.Generic;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Join Multiple Existing PDFs into a single document
List<PdfDocument> pdfs = new List<PdfDocument>();
pdfs.Add(PdfDocument.FromFile("A.pdf"));
pdfs.Add(PdfDocument.FromFile("B.pdf"));
pdfs.Add(PdfDocument.FromFile("C.pdf"));
PdfDocument mergedPdfDocument = PdfDocument.Merge(pdfs);
mergedPdfDocument.SaveAs("merged.pdf");

// Add a cover page
mergedPdfDocument.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"));

// Remove the last page from the PDF and save again
mergedPdfDocument.RemovePage(mergedPdfDocument.PageCount - 1);
mergedPdfDocument.SaveAs("merged.pdf");

// Copy pages 1,2 and save them as a new document.
mergedPdfDocument.CopyPages(1, 2).SaveAs("exerpt.pdf");
foreach (PdfDocument pdfDocument in pdfs)
{
    pdfDocument.Dispose();
}
Imports IronPdf
Imports System.Collections.Generic

Private renderer As New ChromePdfRenderer()

' Join Multiple Existing PDFs into a single document
Private pdfs As New List(Of PdfDocument)()
pdfs.Add(PdfDocument.FromFile("A.pdf"))
pdfs.Add(PdfDocument.FromFile("B.pdf"))
pdfs.Add(PdfDocument.FromFile("C.pdf"))
Dim mergedPdfDocument As PdfDocument = PdfDocument.Merge(pdfs)
mergedPdfDocument.SaveAs("merged.pdf")

' Add a cover page
mergedPdfDocument.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"))

' Remove the last page from the PDF and save again
mergedPdfDocument.RemovePage(mergedPdfDocument.PageCount - 1)
mergedPdfDocument.SaveAs("merged.pdf")

' Copy pages 1,2 and save them as a new document.
mergedPdfDocument.CopyPages(1, 2).SaveAs("exerpt.pdf")
For Each pdfDocument As PdfDocument In pdfs
	pdfDocument.Dispose()
Next pdfDocument
VB   C#

10. Extract Images from PDF Documents

This feature requires an additional NuGet package. Install System.Drawing.Common. Use the ExtractAllText to get text and the ExtractAllImages method to get images.

:path=/static-assets/pdf/content-code-examples/get-started/get-started-10.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("A.pdf");

// Get all text
string allText = pdf.ExtractAllText();

// Get all Images
var allImages = pdf.ExtractAllImages();

// Or even find the images and text by page
for (var index = 0 ; index < pdf.PageCount ; index++)
{
    int pageNumber = index + 1;
    string pageText = pdf.ExtractTextFromPage(index);
    var pageImages = pdf.ExtractImagesFromPage(index);
}
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("A.pdf")

' Get all text
Private allText As String = pdf.ExtractAllText()

' Get all Images
Private allImages = pdf.ExtractAllImages()

' Or even find the images and text by page
For index = 0 To pdf.PageCount - 1
	Dim pageNumber As Integer = index + 1
	Dim pageText As String = pdf.ExtractTextFromPage(index)
	Dim pageImages = pdf.ExtractImagesFromPage(index)
Next index
VB   C#

11. Enable JavaScript

:path=/static-assets/pdf/content-code-examples/get-started/get-started-11.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.RenderDelay(500);
renderer.RenderingOptions = new ChromePdfRenderOptions()
{
    EnableJavaScript = true,
};
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

12. PDF OCR and Text Extraction

In many cases you can extract embedded text from PDFs directly:

:path=/static-assets/pdf/content-code-examples/get-started/get-started-12.cs
using IronPdf;

PdfDocument pdf = PdfDocument.FromFile("Invoice.pdf", "password");

// Get all text
string text = pdf.ExtractAllText();
Imports IronPdf

Private pdf As PdfDocument = PdfDocument.FromFile("Invoice.pdf", "password")

' Get all text
Private text As String = pdf.ExtractAllText()
VB   C#

If that doesn't work - your text is probably embedded in an image:

Use the IronOCR library to scan documents for visual text that is not plain text.

You will need to install the NuGet package IronOcr. Learn more about scanning PDFs with IronOCR.

:path=/static-assets/pdf/content-code-examples/get-started/get-started-13.cs
using IronOcr;
using System;

IronTesseract ocr = new IronTesseract();
using (OcrInput Input = new OcrInput())
{
    // OCR entire document
    Input.LoadPdf("Invoice.pdf", Password: "password");

    // Use filters to increase image quality
    Input.Deskew();
    Input.DeNoise();

    OcrResult Result = ocr.Read(Input);

    Console.WriteLine(Result.Text);
    var Barcodes = Result.Barcodes;
    string Text = Result.Text;
}
Imports IronOcr
Imports System

Private ocr As New IronTesseract()
Using Input As New OcrInput()
	' OCR entire document
	Input.LoadPdf("Invoice.pdf", Password:= "password")

	' Use filters to increase image quality
	Input.Deskew()
	Input.DeNoise()

	Dim Result As OcrResult = ocr.Read(Input)

	Console.WriteLine(Result.Text)
	Dim Barcodes = Result.Barcodes
	Dim Text As String = Result.Text
End Using
VB   C#

13. Use More Rendering Options

Here are some more detailed rendering options

:path=/static-assets/pdf/content-code-examples/get-started/get-started-14.cs
using IronPdf;
using System.Text;

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.SetCustomPaperSizeInInches(12.5, 20);
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait;
renderer.RenderingOptions.Title = "My PDF Document Name";
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.RenderDelay(50);
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
renderer.RenderingOptions.GrayScale = false;
renderer.RenderingOptions.FitToPaperMode = IronPdf.Engines.Chrome.FitToPaperModes.Automatic;
renderer.RenderingOptions.InputEncoding = Encoding.UTF8;
renderer.RenderingOptions.Zoom = 100;
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;

// Change margins (millimeters)
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 40;

// Use 2 if a cover page will be appended
renderer.RenderingOptions.FirstPageNumber = 1;
renderer.RenderHtmlFileAsPdf("my-content.html").SaveAs("my-content.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

14. Download the C# PDF Cheat Sheet

We have compiled this tutorial as an easy to read and share PDF document that explains in full how to create and edit PDFs in C# and VB.NET using the IronPDF library.

You can download it and use it as a develop guide for your .NET projects, or print it as a handy companion for IronPDF development. This saves time and effort in getting started adding PDF features to any .NET project.


15. Learn More

To learn more about HTML to PDF in C# or VB.NET applications, please read the detailed C# HTML to PDF Tutorial. The tutorial clearly explains advanced PDF settings with HTML templates, CSS, Images, and JavaScript.

If you're interested in how to dynamically render ASPX pages in ASP.NET applications as PDFs, check out the full ASPX to PDF Tutorial.

A full IronPDF API reference for .NET developers is also available.

16. Apply License

IronPDF requires a license key to use. To learn more about how the license key can be applied, visit the IronPDF License Keys page.


Tutorial Quick Access

View the API Reference

Explore the API Reference for IronPDF, outlining the details of all of IronPDF’s features, namespaces, classes, methods fields and enums.

View the API Reference