USING IRONPDF

Generating PDF Files in C#

Published January 20, 2022
Share:

Office documents and reports often need to be converted into PDF documents from a wide variety of formats, including HTML. We may also need to generate PDF files from web pages or URLs. For this purpose, we need a versatile generator that can generate PDF files that contain the exact same content as the original HTML files. In this tutorial, we will learn how to generate PDF files in C#.

What is IronPDF?

IronPDF .NET PDF Library is recognized by many designers, particularly computer programmers who use C#. You can undoubtedly make a core PDF library for .NET with this exceptional tool.

IronPDF utilizes a .NET Chromium engine to convert HTML pages to PDF documents. With HTML to PDF there is no reason to utilizes complex APIs to position or plan PDFs. IronPDF upholds standard web documents: HTML, ASPX, JS, CSS, and images.

It additionally empowers you to make a .NET PDF library utilizing HTML5, CSS, JavaScript, and images. You can easily edit, stamp, and add headers and footers to a PDF. Furthermore, it makes it extremely simple to read PDF text and extract the pictures.

IronPDF gives a solitary answer for all PDF issues. Whenever you buy IronPDF, you get every one of the changes in a single library, in addition to tasks connected with PDF documents, requiring no extra conditions. IronPDF is a complete package and a great tool for designers and c# programmers alike. You can without much of a stretch make a core PDF library for .NET with this useful tool. IronPDF utilizes a .NET Chromium engine to deliver HTML pages to PDF documents.

IronPDF helps us to generate PDF files from the following:

  • HTML Strings and Templates
  • ASPX WebForms
  • MVC Views
  • HTML documents
  • any URL

IronPDF Features

IronPDF assists you with creating PDF documents from HTML 4 and 5, CSS, and JavaScript and creates PDF files from URLs. It likewise assists with loading URLs with custom network login accreditations, User-Agents, Proxies, Cookies, HTTP headers, and form variables permitting login behind HTML login forms. You can without much of a stretch, extract images and texts from PDF and stamp new HTML content onto any current PDF page additionally permitting you to add logical or HTML headers and footers. It can easily load and extract existing PDF files and merge and split content in PDF documents. It has a custom 'base URL' to permit accessible asset files across the web and acknowledge HTML encoded in any significant document encoding (Default to UTF-8). You can simply save and load from document, binary data, or MemoryStreams and transform PDF documents into a PrintDocument item and print without Adobe (with insignificant code) and export official log documents with details concerning API achievement and debug errors in source code.

Compatibility

It supports .NET languages like C#, VB.NET AND F#. It is viable on .NET Platforms like .NET 6, 5, Core 2x and 3x, Standard 2 and Framework 4x. It works on applications like Console, Web and Desktop, and is supported by Microsoft Windows, Linux, macOS, Docker and Azure Operating System. IronPDF IDEs incorporate Microsoft Visual Studio, JetBeans Resharper and Rider.

Steps to Generate PDF Documents

Firstly, we need to create a new Visual Studio Project. I will use the Console Application template for this demonstration. You can use any according to your requirements and choice.

You may also use your existing project in which you want to add a PDF file-generating tool.

Create a Visual Studio Project

Open Microsoft Visual Studio 2019, or any other version. The latest version is recommended. Click on "Create New Project". Select the C# Console Application for the application template. Click the "Next" button.

Assign a name to your project. I have named it "C Sharp Generate PDF". You may name it as you choose. Click the "Next" button, and set the target .NET Framework. I have set my target .NET framework to ".NET 5.0", the latest stable version. Click the "Create" button. A new project will be created for you.

Install the NuGet Package in Visual Studio

Before we begin, we need to install the IronPDF Library. You can install it by using one of the following three methods:

Package Manager Console

Write the following command in the Package Manager console. It will download and install the package for you.

Install-Package IronPdf

Image 1

The NuGet Package Manager Solution

You can also install the Barcode Library by using the NuGet Packages Solution. Simply follow these steps:

Click on Tools > NuGet Package Manager > Manage NuGet package Solution.

Image 2

This will open NuGet Package Manager for you. Click on "Browse" and search IronPDF as shown below.

Image 3

Click on IronPDF and then click the "Installed" button. This will install IronPDF in your project. Now you are free to use any of its functions in your project.

Alternatively, you can directly download the DLL.

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

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

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 that are documented in the IronPDF advanced NuGet installation guide.

IronPDF library has now been installed, so let's move to the next step.

Generate PDF Files from HTML String

IronPDF can generate PDF files from HTML text quite easily. The following example illustrates this capability. Use this option when you only need to add simple text to your PDF file.

Add the following namespace:

using IronPdf;
using IronPdf;
Imports IronPdf
VB   C#

Using the following code example:

var Renderer = new ChromePdfRenderer();
Renderer.RenderHtmlAsPdf("<h1>This is the Tutorial for C# Generate PDF<h1>").SaveAs("GeneratePDF.pdf");
var Renderer = new ChromePdfRenderer();
Renderer.RenderHtmlAsPdf("<h1>This is the Tutorial for C# Generate PDF<h1>").SaveAs("GeneratePDF.pdf");
Dim Renderer = New ChromePdfRenderer()
Renderer.RenderHtmlAsPdf("<h1>This is the Tutorial for C# Generate PDF<h1>").SaveAs("GeneratePDF.pdf")
VB   C#

In the above code, I have created a new ChromePdfRenderer renderer. I have then called it RenderHtmlAsPdf and then SaveAs for the result.

Output

Run the program. It will generate a PDF document in the bin folder of the project as we have not specified the path.

Image 4

Generate your First PDF Files

After the IronPDF library is installed we can effortlessly create a PDF file and PDF Page by using just a few lines of code. Now we will help you create your first PDF document in C#. Copy the below code and paste it into your Visual Studio and Run the program.

var PDF = new ChromePdfRenderer();
PdfDocument doc = pdf.RenderHtmlAsPdf("<h1>This is a heading</h1>");
mypdf.SaveAs("FirstPDFDocument.pdf");
var PDF = new ChromePdfRenderer();
PdfDocument doc = pdf.RenderHtmlAsPdf("<h1>This is a heading</h1>");
mypdf.SaveAs("FirstPDFDocument.pdf");
Dim PDF = New ChromePdfRenderer()
Dim doc As PdfDocument = pdf.RenderHtmlAsPdf("<h1>This is a heading</h1>")
mypdf.SaveAs("FirstPDFDocument.pdf")
VB   C#

After execution of your C# project there will be a file mane "FirstPDFDocument.pdf" in the bin folder of your project, double-click on the said file, and the PDF file will open in the browser tab.

Image 9

Creating PDF files in C# or creating PDF files converting HTML to PDF is just a few lines of code using IronPDF.

Generate PDF Document from URL

Creating a PDF file in C# using a URL is just as easy as the above example with just these three lines of code, following code will demonstrate how to create PDF files from a URL.

using IronPdf;
var Renderer = new IronPdf.ChromePdfRenderer();
// Create a PDF from a URL or local file path
using var PDF = Renderer.RenderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
using IronPdf;
var Renderer = new IronPdf.ChromePdfRenderer();
// Create a PDF from a URL or local file path
using var PDF = Renderer.RenderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
// Export to a file or Stream
pdf.SaveAs("url.pdf");
Imports IronPdf
Private Renderer = New IronPdf.ChromePdfRenderer()
' Create a PDF from a URL or local file path
Private PDF = Renderer.RenderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20")
' Export to a file or Stream
pdf.SaveAs("url.pdf")
VB   C#

Here is the output of the above code.

Image 10

Other examples of converting popular complex sites to PDF.

Image 11

PDF File Generation from an HTML File

We can generate a PDF document from HTML files with images, CSS, forms, hyperlinks, and JavaScript. Use this method for scenarios where you have local access to the source document.

This example is named RenderHTMLFileAsPdf and returns a variable called PDF.

Call SaveAs to save the output to a PDF file.

The following code example assumes there is an HTML file in the bin folder of the project.

var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderHTMLFileAsPdf("AccountPage.html");
PDF.SaveAs("AccountPdf.pdf");
var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderHTMLFileAsPdf("AccountPage.html");
PDF.SaveAs("AccountPdf.pdf");
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderHTMLFileAsPdf("AccountPage.html")
PDF.SaveAs("AccountPdf.pdf")
VB   C#

Input HTML:

Image 5

Generated PDF file output:

Image 6

Generate PDF Files from URLs

We can generate PDF files from existing web pages with a few lines of C# or VB.NET code. Use this option when you need to generate a PDF document from a website that already has a well-formatted document.

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

var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
var Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");
PDF.SaveAs("wikipedia.pdf");
Dim Renderer = New ChromePdfRenderer()
Dim PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format")
PDF.SaveAs("wikipedia.pdf")
VB   C#

PDF file output:

Image 7

Generate a PDF Document from ASP.NET Web Forms

We can generate a PDF document from ASP.NET web forms 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 Web Forms application or open an existing one
  • Install the NuGet package for the IronPDF Library
  • Import the IronPDF namespace using the appropriate keyword
  • Open the code-behind for the page that you want to render to PDF. For example, Default.aspx.cs
  • Write the following code inside the Page Load Function.
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser);
AspxToPdf.RenderThisPageAsPdf(AspxToPdf.FileBehavior.InBrowser)
VB   C#

PDF file output:

Image 8

Generate PDF Files from ASP MVC View

We can generate PDF documents from the ASP MVC framework.

Start the new project wizard in Visual Studio, and choose ASP.NET Web Application (.NET Framework) > MVC. Alternatively, 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:

public ActionResult Index()
{
    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();
}
public ActionResult Index()
{
    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();
}
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
VB   C#

PDF file output:

Image 9

Add Headers and Footers

The Print Options property allows you to craft headers and footers for each page of the PDF file. 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}

Write the following code:

 var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1;
//Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "This is Header {date} {time}";
ChromePdfRenderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12;
//Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextFooter.FontFamily = "Arial";
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10;
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is my Sample Page for Generate PDF Tutorial<h1>").SaveAs("GeneratePDF.pdf");
 var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1;
//Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "This is Header {date} {time}";
ChromePdfRenderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial";
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12;
//Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = true;
ChromePdfRenderer.RenderingOptions.TextFooter.FontFamily = "Arial";
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10;
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}";
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is my Sample Page for Generate PDF Tutorial<h1>").SaveAs("GeneratePDF.pdf");
Dim ChromePdfRenderer As New ChromePdfRenderer()
ChromePdfRenderer.RenderingOptions.FirstPageNumber = 1
'Header options
ChromePdfRenderer.RenderingOptions.TextHeader.DrawDividerLine = True
ChromePdfRenderer.RenderingOptions.TextHeader.CenterText = "This is Header {date} {time}"
ChromePdfRenderer.RenderingOptions.TextHeader.FontFamily = "Helvetica,Arial"
ChromePdfRenderer.RenderingOptions.TextHeader.FontSize = 12
'Footer options
ChromePdfRenderer.RenderingOptions.TextFooter.DrawDividerLine = True
ChromePdfRenderer.RenderingOptions.TextFooter.FontFamily = "Arial"
ChromePdfRenderer.RenderingOptions.TextFooter.FontSize = 10
ChromePdfRenderer.RenderingOptions.TextFooter.LeftText = "{date} {time}"
ChromePdfRenderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is my Sample Page for Generate PDF Tutorial<h1>").SaveAs("GeneratePDF.pdf")
VB   C#

Image 10

Add Headers and Footers with HTML

We can add text, date, time, and page numbers in the headers and footers of our PDF document.

var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    Height = 15,
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};
ChromePdfRenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    Height = 20,
    HtmlFragment = "This is Footer {date}",
};
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is sample page for Tutorial.<h1>").SaveAs("GeneratePDF.pdf");
var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    Height = 15,
    HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
    DrawDividerLine = true
};
ChromePdfRenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    Height = 20,
    HtmlFragment = "This is Footer {date}",
};
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is sample page for Tutorial.<h1>").SaveAs("GeneratePDF.pdf");
Dim ChromePdfRenderer As New ChromePdfRenderer()
ChromePdfRenderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter() With {
	.Height = 15,
	.HtmlFragment = "<center><i>{page} of {total-pages}<i></center>",
	.DrawDividerLine = True
}
ChromePdfRenderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {
	.Height = 20,
	.HtmlFragment = "This is Footer {date}"
}
ChromePdfRenderer.RenderHtmlAsPdf("<h1>This is sample page for Tutorial.<h1>").SaveAs("GeneratePDF.pdf")
VB   C#

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

Image 11

Encrypt PDF Documents with a Password

We can generate PDF documents with passwords. 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.

var ChromePdfRenderer = new ChromePdfRenderer();
var pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello world<h1>");
pdfDocument.Password = "Password@1234";
pdfDocument.SaveAs("secured.pdf");
var ChromePdfRenderer = new ChromePdfRenderer();
var pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello world<h1>");
pdfDocument.Password = "Password@1234";
pdfDocument.SaveAs("secured.pdf");
Dim ChromePdfRenderer As New ChromePdfRenderer()
Dim pdfDocument = ChromePdfRenderer.RenderHtmlAsPdf("<h1>Hello world<h1>")
pdfDocument.Password = "Password@1234"
pdfDocument.SaveAs("secured.pdf")
VB   C#

PDF file output:

Image 12

Merge and Split PDF Documents

We can merge multiple PDF documents into a single new PDF document. Use the following code to merge and split.

var ChromePdfRenderer = new ChromePdfRenderer();
//Join Multiple Existing PDFs into a single document
var pdfDocuments = new List<PdfDocument>();
pdfDocuments.Add(PdfDocument.FromFile("AccountPdf.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("GeneratePDF.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("wikipedia.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");
var ChromePdfRenderer = new ChromePdfRenderer();
//Join Multiple Existing PDFs into a single document
var pdfDocuments = new List<PdfDocument>();
pdfDocuments.Add(PdfDocument.FromFile("AccountPdf.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("GeneratePDF.pdf"));
pdfDocuments.Add(PdfDocument.FromFile("wikipedia.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");
Dim ChromePdfRenderer As New ChromePdfRenderer()
'Join Multiple Existing PDFs into a single document
Dim pdfDocuments = New List(Of PdfDocument)()
pdfDocuments.Add(PdfDocument.FromFile("AccountPdf.pdf"))
pdfDocuments.Add(PdfDocument.FromFile("GeneratePDF.pdf"))
pdfDocuments.Add(PdfDocument.FromFile("wikipedia.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")
VB   C#

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

Image 13

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 function to get images.

var ChromePdfRenderer = new ChromePdfRenderer();
var pdfDocument = PdfDocument.FromFile("wikipedia.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);
}
var ChromePdfRenderer = new ChromePdfRenderer();
var pdfDocument = PdfDocument.FromFile("wikipedia.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);
}
Dim ChromePdfRenderer As New ChromePdfRenderer()
Dim pdfDocument = PdfDocument.FromFile("wikipedia.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
VB   C#

You need to add System.Drawing to save images as bitmap.

Enable JavaScript

We can enable JavaScript in our browser using IronPDF. The code example below shows us how to enable JavaScript in our web browser.

var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions = new ChromePdfRenderer()
{
    EnableJavaScript = true,
    RenderDelay = 100
}               
var ChromePdfRenderer = new ChromePdfRenderer();
ChromePdfRenderer.RenderingOptions = new ChromePdfRenderer()
{
    EnableJavaScript = true,
    RenderDelay = 100
}               
Dim ChromePdfRenderer As New ChromePdfRenderer()
ChromePdfRenderer.RenderingOptions = New ChromePdfRenderer() With {
	.EnableJavaScript = True,
	.RenderDelay = 100
}
VB   C#

PDF OCR and Text Extraction

In many cases, you can extract embedded text from PDF documents directly.

Write the following code:

PdfDocument PDF = PdfDocument.FromFile("GeneratePDF.pdf");
//Get all text 
string Text = PDF.ExtractAllText();
PdfDocument PDF = PdfDocument.FromFile("GeneratePDF.pdf");
//Get all text 
string Text = PDF.ExtractAllText();
Dim PDF As PdfDocument = PdfDocument.FromFile("GeneratePDF.pdf")
'Get all text 
Dim Text As String = PDF.ExtractAllText()
VB   C#

If this does not work, it is probably because your text is actually embedded in an image.

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

Summary

You can generate PDF documents, manipulate and format them in any way you want with just a few lines of code. Simply specify the operations, and IronPDF will have them. There is also a free version for development and a free trial before purchase. There are other PDF generating libraries on the market, but many are either slow in terms of performance or require you to pay for development.

The IronPDF .NET library is now available with Iron Suite for .NET. Iron Suite contains other .NET libraries such as the IronXL library, the IronOCR library, the IronBarcode library, etc. These other libraries are very useful for developers, so it is recommended to try them. If you purchase the complete Iron Suite, you can get all five products for the price of just two. For more details, click here.

You can download a file project from this link.

< PREVIOUS
x509certificate2 Add Digital Signature to PDF Programmatically
NEXT >
Create PDF in C# from HTML File

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 11,308,499 View Licenses >