IRONSOFTWAREHOME
Execute Custom JavaScript
using IronPdf;

var renderOptions = new ChromePdfRenderOptions();

// Example that change all to red style in JavaScript:
renderOptions.Javascript = "document.querySelectorAll('h1').forEach(function(el){el.style.color='red';})";

// Make sure to use the render options object that you created:
var renderer = new ChromePdfRenderer { RenderingOptions = renderOptions };
var pdf = renderer.RenderHtmlAsPdf("<h1>Happy New Year!</h1>");
pdf.SaveAs("executed_js.pdf");
Imports IronPdf

Dim renderOptions = New ChromePdfRenderOptions()

' Example that change all to red style in JavaScript:
renderOptions.Javascript = "document.querySelectorAll('h1').forEach(function(el){el.style.color='red';})"

' Make sure to use the render options object that you created:
Dim renderer = New ChromePdfRenderer With {.RenderingOptions = renderOptions}
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Happy New Year!</h1>")
pdf.SaveAs("executed_js.pdf")
NuGet Download
PM > Install-Package IronPdf
Execute Custom JavaScript

Execute Custom JavaScript

With IronPDF, you can execute custom JavaScript to better customize your PDFs rendered from HTML content. With the ChromePdfRenderOptions.Javascript property, you can make sure the JavaScript executes after the HTML content has loaded but before your PDF is rendered, allowing for dynamic content rendering and interactivity. IronPDF's ChromePdfRenderer class ensures you create high-quality PDF documents tailored to your needs without losing any of the original quality or interactivity.

Before we can begin rendering our custom HTML and JavaScript into PDFs, we must first create a new ChromePdfRenderer instance. The ChromePdfRenderer class handles the rendering of HTML, CSS, and JavaScript content into PDF format, while maintaining a pixel-perfect quality. Then, using the Javascript property of the ChromePdfRenderOptions class, we can create a custom JavaScript code that will execute as soon as the HTML content has loaded, ensuring that the changes dictated by our JavaScript string are reflected in the final PDF document. In this case, we are selecting any h1 tags within the HTML content and changing their text color to red.

We then handle assigning our renderOptions object to the ChromePdfRenderer RenderingOptions property, which controls the rendering options for that specific instance of the ChromePdfRenderer. This means that when we use our instance of the ChromePdfRenderer class, we will be running our JavaScript before the PDF is rendered, ensuring the changes are made in the resulting PDF. Then, we render our HTML content using RenderHtmlAsPdf and use the PdfDocument class to create a new PDF document from the rendered content, which is stored in the variable pdf.

Finally, we save the rendered PDF using the final line, SaveAs, which will export the PDF to the specified filepath.

Explore JavaScript to PDF with IronPDF
View on GitHub

Ready to Get Started?

Nuget Downloads 21,105,021Version:2026.9just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999