执行自定义JavaScript
利用 IronPDF 的 "ChromePdfRenderOptions.Javascript "属性,您可以指定在页面加载后以字符串形式执行 JavaScipt,如示例所示。
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 Private 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")
Install-Package IronPdf
利用 IronPDF 的 "ChromePdfRenderOptions.Javascript "属性,您可以指定在页面加载后以字符串形式执行 JavaScipt,如示例所示。