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

Install with NuGet
Install-Package IronPdf
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
PM > 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
- Deploying IronPDF on Linux is well documented and a popular choice for cloud deployments such as Azure.
We officially support: Ubuntu, Debian, CentOS, Fedora & Amazon Linux 2.
1.4B Optional: Docker Deployment
- Deploying IronPDF on Docker is well documented.
We officially support Docker for: Windows, Ubuntu, Debian, CentOS & Amazon Linux 2 and provide working Docker files.
1.4C Optional: Azure Deployment
- Official Support for Azure WebApps, Azure WebJobs, Azure Functions and Azure Docker instances and Azure VMs.
- Read the IronPDF Azure & Azure Function Setup Guide.
1.4D Optional: Amazon AWS Deployment
- A tutorial and support for Amazon AWS Lambda is included.
1.4E Optional: macOS Support
- Official Support for macOS deployments and development using Rider and "Visual Studio for Mac" are supported.
How to Tutorials
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 theusing
keyword - Create a new
ChromePdfRenderer
renderer - Call
RenderHtmlAsPdf
and thenSaveAs
on the result.
/**
HTML String to PDF
anchor-render-html-string-to-pdf
**/
using IronPdf;
namespace IronPDFSample
{
class Program
{
static void Main()
{
var Renderer = new ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
PDF.SaveAs("html-string.pdf");
}
}
}
/**
HTML String to PDF
anchor-render-html-string-to-pdf
**/
using IronPdf;
namespace IronPDFSample
{
class Program
{
static void Main()
{
var Renderer = new ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
PDF.SaveAs("html-string.pdf");
}
}
}
'''
'''HTML String to PDF
'''anchor-render-html-string-to-pdf
'''*
Imports IronPdf
Namespace IronPDFSample
Friend Class Program
Shared Sub Main()
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
PDF.SaveAs("html-string.pdf")
End Sub
End Class
End Namespace
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.
/**
HTML File to PDF
anchor-convert-html-file-to-pdf
**/
using IronPdf;
namespace IronPDFSample
{
class Program
{
static void Main()
{
// Create a PDF from an existing HTML using C#
var Renderer = new ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlFileAsPdf("Assets/MyHTML.html");
PDF.SaveAs("MyPdf.pdf");
}
}
}
/**
HTML File to PDF
anchor-convert-html-file-to-pdf
**/
using IronPdf;
namespace IronPDFSample
{
class Program
{
static void Main()
{
// Create a PDF from an existing HTML using C#
var Renderer = new ChromePdfRenderer();
using var PDF = Renderer.RenderHtmlFileAsPdf("Assets/MyHTML.html");
PDF.SaveAs("MyPdf.pdf");
}
}
}
'''
'''HTML File to PDF
'''anchor-convert-html-file-to-pdf
'''*
Imports IronPdf
Namespace IronPDFSample
Friend Class Program
Shared Sub Main()
' Create a PDF from an existing HTML using C#
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlFileAsPdf("Assets/MyHTML.html")
PDF.SaveAs("MyPdf.pdf")
End Sub
End Class
End Namespace
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.
/**
URL to PDF
anchor-render-existing-url-to-pdf
**/
using IronPdf;
namespace IronPDFSample
{
class Program
{
static void Main()
{
// Create a PDF from any existing web page
var Renderer = new ChromePdfRenderer();
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}
}
}
/**
URL to PDF
anchor-render-existing-url-to-pdf
**/
using IronPdf;
namespace IronPDFSample
{
class Program
{
static void Main()
{
// Create a PDF from any existing web page
var Renderer = new ChromePdfRenderer();
using var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
}
}
}
'''
'''URL to PDF
'''anchor-render-existing-url-to-pdf
'''*
Imports IronPdf
Namespace IronPDFSample
Friend Class Program
Shared Sub Main()
' Create a PDF from any existing web page
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")
PDF.SaveAs("wikipedia.pdf")
End Sub
End Class
End Namespace
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 theusing
keyword - Open the code-behind for the page that you want to render to PDF. For example,
Default.aspx.cs
- Call
RenderThisPageAsPdf
on AspxToPdf
/**
ASP NET WebForms PDF
anchor-asp-net-web-forms-to-pdf
**/
using IronPdf;
using System;
using System.Web.UI;
namespace WebApplication7
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
}
}
}
/**
ASP NET WebForms PDF
anchor-asp-net-web-forms-to-pdf
**/
using IronPdf;
using System;
using System.Web.UI;
namespace WebApplication7
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
}
}
}
'''
'''ASP NET WebForms PDF
'''anchor-asp-net-web-forms-to-pdf
'''*
Imports IronPdf
Imports System
Imports System.Web.UI
Namespace WebApplication7
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
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:
/**
ASP MVC View to PDF
anchor-route-asp-mvc-view-to-pdf
**/
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();
}
}
}
/**
ASP MVC View to PDF
anchor-route-asp-mvc-view-to-pdf
**/
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();
}
}
}
'''
'''ASP MVC View to PDF
'''anchor-route-asp-mvc-view-to-pdf
'''*
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
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}
/**
Add Headers Footers
anchor-add-headers-and-footers
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1;
//Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "{url}";
ChromePdfRenderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica;
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12;
//Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Arial;
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10;
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
using var PDF = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
PDF.SaveAs("html-string.pdf");
}
}
}
/**
Add Headers Footers
anchor-add-headers-and-footers
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1;
//Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "{url}";
ChromePdfRenderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica;
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12;
//Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Arial;
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10;
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
using var PDF = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
PDF.SaveAs("html-string.pdf");
}
}
}
'''
'''Add Headers Footers
'''anchor-add-headers-and-footers
'''*
Imports IronPdf
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ChromePdfRenderer As New ChromePdfRenderer()
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1
'Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = True
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "{url}"
ChromePdfRenderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12
'Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = True
ChromePdfRenderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Arial
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
Dim PDF = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
PDF.SaveAs("html-string.pdf")
End Sub
End Class
End Namespace
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.
/**
Add HTML Headers Footers
anchor-add-headers-and-footers
**/
using IronPdf;
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
// Build a footer using html to style the text // mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
ChromePdfRenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
Height = 15,
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
ChromePdfRenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
Height = 20,
HtmlFragment = "<img src='logo.jpg'>",
BaseUrl = new Uri(@"C:\assets\images").AbsoluteUri
};
using var PDF = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
PDF.SaveAs("html-string.pdf");
}
}
}
/**
Add HTML Headers Footers
anchor-add-headers-and-footers
**/
using IronPdf;
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
// Build a footer using html to style the text // mergeable fields are:
// {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
ChromePdfRenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
Height = 15,
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
ChromePdfRenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
Height = 20,
HtmlFragment = "<img src='logo.jpg'>",
BaseUrl = new Uri(@"C:\assets\images").AbsoluteUri
};
using var PDF = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
PDF.SaveAs("html-string.pdf");
}
}
}
'''
'''Add HTML Headers Footers
'''anchor-add-headers-and-footers
'''*
Imports IronPdf
Imports System
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ChromePdfRenderer 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}
ChromePdfRenderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
.Height = 15,
.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
ChromePdfRenderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
.Height = 20,
.HtmlFragment = "<img src='logo.jpg'>",
.BaseUrl = (New Uri("C:\assets\images")).AbsoluteUri
}
Dim PDF = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
PDF.SaveAs("html-string.pdf")
End Sub
End Class
End Namespace
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
/**
Add PDF Password
anchor-encrypt-pdfs-with-a-password
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
using var pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdfDocument.Password = "strong!@#pass&^%word";
pdfDocument.SaveAs("secured.pdf");
}
}
}
/**
Add PDF Password
anchor-encrypt-pdfs-with-a-password
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
using var pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>");
pdfDocument.Password = "strong!@#pass&^%word";
pdfDocument.SaveAs("secured.pdf");
}
}
}
'''
'''Add PDF Password
'''anchor-encrypt-pdfs-with-a-password
'''*
Imports IronPdf
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ChromePdfRenderer As New ChromePdfRenderer()
Dim pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello World<h1>")
pdfDocument.Password = "strong!@#pass&^%word"
pdfDocument.SaveAs("secured.pdf")
End Sub
End Class
End Namespace
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.
/**
Merge Split PDFs
anchor-merge-and-split-pdf-documents
**/
using IronPdf;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
//Join Multiple Existing PDFs into a single document
var pdfDocuments = new List<PdfDocument>();
pdfDocuments.Add(PdfDocument.FromFile("A.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("B.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("C.pdf"));
var mergedPdfDocument = PdfDocument.Merge(pdfDocuments);
mergedPdfDocument.SaveAs("merged.pdf");
//Add a cover page
mergedPdfDocument.PrependPdf(ChromePdfRenderer.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(var pdfDocument in pdfDocuments){
pdfDocument.Dispose();
}
}
}
}
/**
Merge Split PDFs
anchor-merge-and-split-pdf-documents
**/
using IronPdf;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
//Join Multiple Existing PDFs into a single document
var pdfDocuments = new List<PdfDocument>();
pdfDocuments.Add(PdfDocument.FromFile("A.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("B.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("C.pdf"));
var mergedPdfDocument = PdfDocument.Merge(pdfDocuments);
mergedPdfDocument.SaveAs("merged.pdf");
//Add a cover page
mergedPdfDocument.PrependPdf(ChromePdfRenderer.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(var pdfDocument in pdfDocuments){
pdfDocument.Dispose();
}
}
}
}
'''
'''Merge Split PDFs
'''anchor-merge-and-split-pdf-documents
'''*
Imports IronPdf
Imports System.Collections.Generic
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ChromePdfRenderer As New ChromePdfRenderer()
'Join Multiple Existing PDFs into a single document
Dim pdfDocuments = New List(Of PdfDocument)()
pdfDocuments.Add(PdfDocument.FromFile("A.pdf"))
pdfDocuments.Add(PdfDocument.FromFile("B.pdf"))
pdfDocuments.Add(PdfDocument.FromFile("C.pdf"))
Dim mergedPdfDocument = PdfDocument.Merge(pdfDocuments)
mergedPdfDocument.SaveAs("merged.pdf")
'Add a cover page
mergedPdfDocument.PrependPdf(ChromePdfRenderer.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 In pdfDocuments
pdfDocument.Dispose()
Next pdfDocument
End Sub
End Class
End Namespace
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.
/**
Extract Images
anchor-extract-images-from-pdf-documents
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
var pdfDocument = PdfDocument.FromFile("A.pdf");
//Get all text
var allText = pdfDocument.ExtractAllText();
//Get all Images
var allImages = pdfDocument.ExtractAllImages();
//Or even find the images and text by page
for (var index = 0; index < pdfDocument.PageCount; index++)
{
var pageNumber = index + 1;
var pageText = pdfDocument.ExtractTextFromPage(index);
var pageImages = pdfDocument.ExtractImagesFromPage(index);
}
}
}
}
/**
Extract Images
anchor-extract-images-from-pdf-documents
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
var pdfDocument = PdfDocument.FromFile("A.pdf");
//Get all text
var allText = pdfDocument.ExtractAllText();
//Get all Images
var allImages = pdfDocument.ExtractAllImages();
//Or even find the images and text by page
for (var index = 0; index < pdfDocument.PageCount; index++)
{
var pageNumber = index + 1;
var pageText = pdfDocument.ExtractTextFromPage(index);
var pageImages = pdfDocument.ExtractImagesFromPage(index);
}
}
}
}
'''
'''Extract Images
'''anchor-extract-images-from-pdf-documents
'''*
Imports IronPdf
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ChromePdfRenderer As New ChromePdfRenderer()
Dim pdfDocument = PdfDocument.FromFile("A.pdf")
'Get all text
Dim allText = pdfDocument.ExtractAllText()
'Get all Images
Dim allImages = pdfDocument.ExtractAllImages()
'Or even find the images and text by page
For index = 0 To pdfDocument.PageCount - 1
Dim pageNumber = index + 1
Dim pageText = pdfDocument.ExtractTextFromPage(index)
Dim pageImages = pdfDocument.ExtractImagesFromPage(index)
Next index
End Sub
End Class
End Namespace
11. Enable JavaScript
/**
Enable JavaScript
anchor-enable-javascript
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions = new ChromePdfRenderOptions()
{
EnableJavaScript = true,
RenderDelay = 100
};
}
}
}
/**
Enable JavaScript
anchor-enable-javascript
**/
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions = new ChromePdfRenderOptions()
{
EnableJavaScript = true,
RenderDelay = 100
};
}
}
}
'''
'''Enable JavaScript
'''anchor-enable-javascript
'''*
Imports IronPdf
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim ChromePdfRenderer As New ChromePdfRenderer()
ChromePdfRenderer.RenderingOptions = New ChromePdfRenderOptions() With {
.EnableJavaScript = True,
.RenderDelay = 100
}
End Sub
End Class
End Namespace
12. PDF OCR and Text Extraction
In many cases you can extract embedded text from PDFs directly:
using IronPdf;
using PdfDocument PDF = PdfDocument.FromFile("Invoice.pdf", "password");
//Get all text
string Text = PDF.ExtractAllText();
using IronPdf;
using PdfDocument PDF = PdfDocument.FromFile("Invoice.pdf", "password");
//Get all text
string Text = PDF.ExtractAllText();
Imports IronPdf
Private PdfDocument As using
'Get all text
Private Text As String = PDF.ExtractAllText()
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.
/**
OCR Scan
anchor-use-ocr-scanning
**/
using IronOcr;
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (var Input = new OcrInput())
{
// OCR entire document
Input.AddPdf("Invoice.pdf", "password");
// Image Quality
Input.Deskew();
Input.DeNoise();
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
var Barcodes = Result.Barcodes;
var Text = Result.Text;
}
}
}
}
/**
OCR Scan
anchor-use-ocr-scanning
**/
using IronOcr;
using IronPdf;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
using (var Input = new OcrInput())
{
// OCR entire document
Input.AddPdf("Invoice.pdf", "password");
// Image Quality
Input.Deskew();
Input.DeNoise();
var Result = Ocr.Read(Input);
Console.WriteLine(Result.Text);
var Barcodes = Result.Barcodes;
var Text = Result.Text;
}
}
}
}
'''
'''OCR Scan
'''anchor-use-ocr-scanning
'''*
Imports IronOcr
Imports IronPdf
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Using Input = New OcrInput()
' OCR entire document
Input.AddPdf("Invoice.pdf", "password")
' Image Quality
Input.Deskew()
Input.DeNoise()
Dim Result = Ocr.Read(Input)
Console.WriteLine(Result.Text)
Dim Barcodes = Result.Barcodes
Dim Text = Result.Text
End Using
End Sub
End Class
End Namespace
13. Use More Rendering Options
Here are some more detailed rendering options
using IronPdf;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var 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.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");
}
}
}
using IronPdf;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
var 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.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");
}
}
}
Imports IronPdf
Imports System.Text
Namespace ConsoleApp
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim 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.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")
End Sub
End Class
End Namespace
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.
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