Execute Custom JavaScript

Effortlessly modify HTML content appearance with JavaScript code using IronPDF. IronPDF is a part of Iron Software's product libraries, known for its robust capabilities in converting HTML to PDF while supporting JavaScript, CSS, and more.

To achieve this, start by defining the JavaScript code. In our example, we'll change the text color of all H1 tags to red.

// JavaScript code to change the text color of all H1 elements to red
const changeTextColor = () => {
    const h1Elements = document.querySelectorAll('h1');
    h1Elements.forEach(element => {
        element.style.color = 'red'; // Set each H1 element's text color to red
    });
};

// Invoke the function to apply the style changes
changeTextColor();
// JavaScript code to change the text color of all H1 elements to red
const changeTextColor = () => {
    const h1Elements = document.querySelectorAll('h1');
    h1Elements.forEach(element => {
        element.style.color = 'red'; // Set each H1 element's text color to red
    });
};

// Invoke the function to apply the style changes
changeTextColor();
JAVASCRIPT

Next, create a rendering options object and set the enableJavaScript property to true. Assign the previously defined JavaScript code to the javascript property within this object.

using IronPdf;

var renderOptions = new HtmlToPdfOptions
{
    EnableJavaScript = true, // Enable JavaScript execution
    JavaScript = @"
// JavaScript code to change the text color of all H1 elements to red
const changeTextColor = () => {
    const h1Elements = document.querySelectorAll('h1');
    h1Elements.forEach(element => {
        element.style.color = 'red'; // Set each H1 element's text color to red
    });
};

// Invoke the function to apply the style changes
changeTextColor();
"
};
using IronPdf;

var renderOptions = new HtmlToPdfOptions
{
    EnableJavaScript = true, // Enable JavaScript execution
    JavaScript = @"
// JavaScript code to change the text color of all H1 elements to red
const changeTextColor = () => {
    const h1Elements = document.querySelectorAll('h1');
    h1Elements.forEach(element => {
        element.style.color = 'red'; // Set each H1 element's text color to red
    });
};

// Invoke the function to apply the style changes
changeTextColor();
"
};
Imports IronPdf

Private renderOptions = New HtmlToPdfOptions With {
	.EnableJavaScript = True,
	.JavaScript = "
// JavaScript code to change the text color of all H1 elements to red
const changeTextColor = () => {
    const h1Elements = document.querySelectorAll('h1');
    h1Elements.forEach(element => {
        element.style.color = 'red'; // Set each H1 element's text color to red
    });
};

// Invoke the function to apply the style changes
changeTextColor();
"
}
$vbLabelText   $csharpLabel

Utilize the PdfDocument.FromHtml method to render the HTML content into a PDF document. Pass the renderOptions object as the second parameter.

// Sample HTML content to be converted
string htmlContent = "<html><body><h1>Hello World</h1></body></html>";

// Render the HTML content to PDF using the specified rendering options
PdfDocument pdf = PdfDocument.FromHtml(htmlContent, renderOptions);
// Sample HTML content to be converted
string htmlContent = "<html><body><h1>Hello World</h1></body></html>";

// Render the HTML content to PDF using the specified rendering options
PdfDocument pdf = PdfDocument.FromHtml(htmlContent, renderOptions);
' Sample HTML content to be converted
Dim htmlContent As String = "<html><body><h1>Hello World</h1></body></html>"

' Render the HTML content to PDF using the specified rendering options
Dim pdf As PdfDocument = PdfDocument.FromHtml(htmlContent, renderOptions)
$vbLabelText   $csharpLabel

Finally, save the resulting PDF, which incorporates the effects of the executed JavaScript code (changing text color to red), as 'executed_js.pdf'.

// Save the PDF document with the effects of JavaScript execution
pdf.SaveAs("executed_js.pdf");
// Save the PDF document with the effects of JavaScript execution
pdf.SaveAs("executed_js.pdf");
' Save the PDF document with the effects of JavaScript execution
pdf.SaveAs("executed_js.pdf")
$vbLabelText   $csharpLabel

To learn more about the capabilities of IronPDF or other Iron Software libraries such as IronBarcode, IronOCR, and more, visit the Iron Software official website.