IronPDF How-Tos Use WaitFor to Delay PDF Render How to Use the WaitFor Class to Delay C# PDF Rendering ByChaknith Bin July 19, 2023 Updated June 22, 2025 Share: When rendering PDF documents, a common issue arises where the rendering process occurs before all the necessary assets and animations of JavaScript have finished loading. This can result in incomplete or incorrect rendering of the PDF document. Initially, we addressed this issue by allowing the user to set an arbitrary delay as a solution. However, relying on an arbitrary delay is not a reliable or efficient approach.To provide a more robust solution, we have implemented a WaitFor class that enhances the PDF rendering process. The WaitFor object from RenderOptions offers several options, including:PageLoad: Default render with no waiting.RenderDelay: Setting an arbitrary waiting time.Fonts: Waits until all the fonts have loaded.JavaScript: Triggering the render with a JavaScript function.HTML elements: Waits for specific HTML elements, such as element IDs, names, tag names, and query selectors to target elements.NetworkIdle: Waiting for network idle (0, 2, or a custom amount).These features are available for converting HTML strings to PDF with IronPDF, HTML files to PDF with IronPDF, and web URLs to PDF with IronPDF. Let's explore the key aspects of this new feature. View the IronPDF YouTube Playlist How to Use WaitFor to Delay PDF Render in C# Download the C# IronPDF library to delay PDF rendering Generate a PDF document from an HTML string, file, or web URL Utilize the JavaScript method of WaitFor to trigger the render from a JavaScript function Delay the render based on the number of network activities Wait for specific HTML elements as well as for all the fonts to be loaded Get started with IronPDF Start using IronPDF in your project today with a free trial. First Step: Start for Free Default Immediate Render Example By default, the rendering process occurs immediately after the page has finished loading. The PageLoad method does not need to be called if you want to render normally. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-pageload.cs using IronPdf; // Import IronPdf namespace to use PDF rendering classes // Create a new instance of ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set the rendering option to wait for the page to fully load before rendering renderer.RenderingOptions = new PdfRenderOptions { // Delay the rendering to occur immediately after the page load RenderDelay = 0 }; // Render an HTML string as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Note: You may want to save or further process 'pdf' as needed, e.g., pdf.SaveAs("output.pdf"); Imports IronPdf ' Import IronPdf namespace to use PDF rendering classes ' Create a new instance of ChromePdfRenderer Private renderer As New ChromePdfRenderer() ' Set the rendering option to wait for the page to fully load before rendering renderer.RenderingOptions = New PdfRenderOptions With {.RenderDelay = 0} ' Render an HTML string as a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Note: You may want to save or further process 'pdf' as needed, e.g., pdf.SaveAs("output.pdf"); $vbLabelText $csharpLabel Custom Render Delay Example In situations where a specific delay is required before rendering the PDF, you can set an arbitrary number of milliseconds as a delay. This provides flexibility in accommodating any specific timing requirements. This option works the same way as the old implementation using the RenderingOptions.RenderDelay property. However, the old property has been deprecated, and it is highly recommended to use the new API, RenderingOptions.WaitFor.RenderDelay. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-delay-time.cs using IronPdf; // Create a new instance of the ChromePdfRenderer to handle rendering operations. ChromePdfRenderer renderer = new ChromePdfRenderer(); // Configure the rendering options to introduce a delay of 3000 milliseconds (3 seconds). // This can be useful if the HTML content relies on delayed loading or asynchronous scripts. renderer.RenderingOptions.RenderDelay = 3000; // Render a simple HTML content as a PDF document. // The HTML content here is a basic header with the text "testing". PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Note: Ensure you have added the necessary using directive for IronPdf and that // you have properly installed the IronPdf library in your project, as it is necessary // for executing this code. Imports IronPdf ' Create a new instance of the ChromePdfRenderer to handle rendering operations. Private renderer As New ChromePdfRenderer() ' Configure the rendering options to introduce a delay of 3000 milliseconds (3 seconds). ' This can be useful if the HTML content relies on delayed loading or asynchronous scripts. renderer.RenderingOptions.RenderDelay = 3000 ' Render a simple HTML content as a PDF document. ' The HTML content here is a basic header with the text "testing". Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Note: Ensure you have added the necessary using directive for IronPdf and that ' you have properly installed the IronPdf library in your project, as it is necessary ' for executing this code. $vbLabelText $csharpLabel All Fonts Loaded Example The AllFontsLoaded method in the WaitFor class allows the PDF rendering process to pause until all fonts are loaded from external sources like Google Fonts or other servers. This ensures that the final PDF includes all required fonts, preserving the document's intended typography and visual appearance. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-all-fonts.cs // Ensure the IronPdf library is referenced in your project using IronPdf; // Define the HTML content as a string. Use of '@' allows for multi-line string literals. string htmlContent = @" <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Test Registration of Extension</title> <!-- Using Google Fonts for web typography --> <link rel=""preconnect"" href=""https://fonts.googleapis.com""> <link rel=""preconnect"" href=""https://fonts.gstatic.com"" crossorigin> <link rel=""stylesheet"" href=""https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap""> <style> /* Define a remote custom font */ @font-face { font-family: 'CustomFont'; src: url('https://stage.gradfinale.co.uk/tcpdf/fonts/avgr65wttf.ttf'); } /* Apply custom font to paragraph with id 'p1' */ p#p1 { font-family: CustomFont, sans-serif; } /* Define a local custom font, assuming Arial is available locally */ @font-face { font-family: 'LocalCustomFont'; src: local('Arial'); } /* Apply local custom font to paragraph with id 'p3' */ p#p3 { font-family: LocalCustomFont, sans-serif; } </style> </head> <body> <!-- Header for the HTML document --> <h1>This is Delayed Render Test!</h1> <!-- Paragraph using the Roboto font, falling back to monospace if unavailable --> <p style=""font-family: Roboto, monospace;"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p> <!-- Paragraph styled using the remote custom font --> <p id=""p1"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p> <!-- Paragraph styled using the local custom font --> <p id=""p3"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p> </body> </html>"; // Instantiate a new PDF renderer using the ChromePdfRenderer class ChromePdfRenderer renderer = new ChromePdfRenderer(); // Configure the renderer to wait for all fonts to load before rendering, with a timeout of 10000 milliseconds (10 seconds) renderer.RenderingOptions.WaitFor = new IronPdf.Rendering.RenderingOptions.WaitForObject(); renderer.RenderingOptions.WaitFor.AllFontsLoaded(10000); // Render the HTML content to a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Save the PDF document to a file (optional, add this if you want to save the generated PDF) // pdf.SaveAs("output.pdf"); ' Ensure the IronPdf library is referenced in your project Imports IronPdf ' Define the HTML content as a string. Use of '@' allows for multi-line string literals. Private htmlContent As String = " <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Test Registration of Extension</title> <!-- Using Google Fonts for web typography --> <link rel=""preconnect"" href=""https://fonts.googleapis.com""> <link rel=""preconnect"" href=""https://fonts.gstatic.com"" crossorigin> <link rel=""stylesheet"" href=""https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap""> <style> /* Define a remote custom font */ @font-face { font-family: 'CustomFont'; src: url('https://stage.gradfinale.co.uk/tcpdf/fonts/avgr65wttf.ttf'); } /* Apply custom font to paragraph with id 'p1' */ p#p1 { font-family: CustomFont, sans-serif; } /* Define a local custom font, assuming Arial is available locally */ @font-face { font-family: 'LocalCustomFont'; src: local('Arial'); } /* Apply local custom font to paragraph with id 'p3' */ p#p3 { font-family: LocalCustomFont, sans-serif; } </style> </head> <body> <!-- Header for the HTML document --> <h1>This is Delayed Render Test!</h1> <!-- Paragraph using the Roboto font, falling back to monospace if unavailable --> <p style=""font-family: Roboto, monospace;"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p> <!-- Paragraph styled using the remote custom font --> <p id=""p1"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p> <!-- Paragraph styled using the local custom font --> <p id=""p3"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p> </body> </html>" ' Instantiate a new PDF renderer using the ChromePdfRenderer class Private renderer As New ChromePdfRenderer() ' Configure the renderer to wait for all fonts to load before rendering, with a timeout of 10000 milliseconds (10 seconds) renderer.RenderingOptions.WaitFor = New IronPdf.Rendering.RenderingOptions.WaitForObject() renderer.RenderingOptions.WaitFor.AllFontsLoaded(10000) ' Render the HTML content to a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Save the PDF document to a file (optional, add this if you want to save the generated PDF) ' pdf.SaveAs("output.pdf"); $vbLabelText $csharpLabel Custom JavaScript Execution Example For more control over the rendering process, our feature allows you to specify a custom JavaScript function that needs to be executed before rendering the PDF document. This enables you to perform any necessary tasks or checks before initiating the rendering process. This gives the user control over when to trigger the render. In JavaScript, the function window.ironpdf.notifyRender() is used to trigger the rendering task. Once notifyRender() is invoked, the rendering process will start. You have full control over when to invoke the function. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-javascript.cs using IronPdf; // Define the HTML content to be converted to PDF string html = @"<!DOCTYPE html> <html> <body> <h1>Testing</h1> <script type='text/javascript'> // Notify IronPdf to render the PDF after a delay setTimeout(function() { window.ironpdf.notifyRender(); }, 1000); </script> </body> </html>"; // Create an instance of ChromePdfRenderOptions to configure PDF rendering settings ChromePdfRenderOptions renderingOptions = new ChromePdfRenderOptions(); // Configure the renderer to wait for JavaScript execution before rendering the PDF // Specifically, it will wait up to 5000 milliseconds for the 'notifyRender' call renderingOptions.JavaScript.LoadingMode = PdfJsLoadingModes.Eager; renderingOptions.JavaScript.WaitForWindowNotifyResultTimeout = 5000; // Static method to render HTML to PDF, using the specified rendering options PdfDocument pdf = ChromePdfRenderer.RenderHtmlAsPdf(html, renderingOptions); // You can now save the PDF document to a file or work with it further // Example: pdf.SaveAs("output.pdf"); JAVASCRIPT HTML Elements Example With this option, the rendering process can be set to wait for specific HTML elements, such as element IDs, names, tag names, and even using query selectors to target elements. Wait for Element ID In the code example below, the rendering will wait for a specific element ID. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-id.cs using IronPdf; // HTML content with a JavaScript delay to manipulate the DOM after 1 second. string htmlContent = @" <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Delayed Render Test</title> <script type=""text/javascript""> setTimeout(function() { // Create a new h1 tag and set its inner HTML and id attribute var h1Tag = document.createElement(""h1""); h1Tag.innerHTML = ""bla bla bla""; h1Tag.setAttribute(""id"", ""myid""); // Append the new h1 tag to the div with id 'x' var block = document.querySelector(""div#x""); block.appendChild(h1Tag); }, 1000); </script> </head> <body> <!-- Initial h1 header for the HTML page --> <h1>This is Delayed Render Test!</h1> <!-- Placeholder div where the new h1 will be appended --> <div id=""x""></div> </body> </html>"; // Create a new renderer using the IronPdf library ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set rendering options to wait for the HTML element with id 'myid' for up to 5000 ms (5 seconds) renderer.RenderingOptions.WaitForHtmlElementById("myid", 5000); // Render the HTML content to a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Optionally, save the rendered PDF document to a file pdf.SaveAs("DelayedRenderTest.pdf"); Imports IronPdf ' HTML content with a JavaScript delay to manipulate the DOM after 1 second. Private htmlContent As String = " <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Delayed Render Test</title> <script type=""text/javascript""> setTimeout(function() { // Create a new h1 tag and set its inner HTML and id attribute var h1Tag = document.createElement(""h1""); h1Tag.innerHTML = ""bla bla bla""; h1Tag.setAttribute(""id"", ""myid""); // Append the new h1 tag to the div with id 'x' var block = document.querySelector(""div#x""); block.appendChild(h1Tag); }, 1000); </script> </head> <body> <!-- Initial h1 header for the HTML page --> <h1>This is Delayed Render Test!</h1> <!-- Placeholder div where the new h1 will be appended --> <div id=""x""></div> </body> </html>" ' Create a new renderer using the IronPdf library Private renderer As New ChromePdfRenderer() ' Set rendering options to wait for the HTML element with id 'myid' for up to 5000 ms (5 seconds) renderer.RenderingOptions.WaitForHtmlElementById("myid", 5000) ' Render the HTML content to a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Optionally, save the rendered PDF document to a file pdf.SaveAs("DelayedRenderTest.pdf") $vbLabelText $csharpLabel Wait for Element Name In the code example below, the rendering process will wait for a specific element Name. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-name.cs using IronPdf; // HTML content to be converted into a PDF. // This HTML includes a delayed JavaScript function that creates an <h1> tag with a specific name attribute. string htmlContent = @" <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Delayed Render Test</title> <script type=""text/javascript""> // JavaScript function that executes after a 1-second delay setTimeout(function() { // Create an <h1> element with particular text content and attributes var h1Tag = document.createElement(""h1""); h1Tag.innerHTML = ""bla bla bla""; h1Tag.setAttribute(""name"", ""myName""); // Append the newly created <h1> element to the <div> with id 'x' var block = document.querySelector(""div#x""); block.appendChild(h1Tag); }, 1000); // 1-second delay </script> </head> <body> <h1>This is Delayed Render Test!</h1> <div id=""x""></div> <!-- Empty div where the new <h1> will be appended --> </body> </html>"; // Create an instance of ChromePdfRenderer from IronPdf ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set rendering options to wait for a specific HTML element // The element to wait for is <h1> with the name attribute "myName" // Wait up to 5000 milliseconds (5 seconds) for the element to be rendered renderer.RenderingOptions.WaitFor = PdfRenderWaitForType.ElementName; renderer.RenderingOptions.HtmlElementToWaitFor = "myName"; renderer.RenderingOptions.RenderDelay = 5000; // Render the HTML content as a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Save the rendered PDF document pdf.SaveAs("DelayedRenderTest.pdf"); Imports IronPdf ' HTML content to be converted into a PDF. ' This HTML includes a delayed JavaScript function that creates an <h1> tag with a specific name attribute. Private htmlContent As String = " <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Delayed Render Test</title> <script type=""text/javascript""> // JavaScript function that executes after a 1-second delay setTimeout(function() { // Create an <h1> element with particular text content and attributes var h1Tag = document.createElement(""h1""); h1Tag.innerHTML = ""bla bla bla""; h1Tag.setAttribute(""name"", ""myName""); // Append the newly created <h1> element to the <div> with id 'x' var block = document.querySelector(""div#x""); block.appendChild(h1Tag); }, 1000); // 1-second delay </script> </head> <body> <h1>This is Delayed Render Test!</h1> <div id=""x""></div> <!-- Empty div where the new <h1> will be appended --> </body> </html>" ' Create an instance of ChromePdfRenderer from IronPdf Private renderer As New ChromePdfRenderer() ' Set rendering options to wait for a specific HTML element ' The element to wait for is <h1> with the name attribute "myName" ' Wait up to 5000 milliseconds (5 seconds) for the element to be rendered renderer.RenderingOptions.WaitFor = PdfRenderWaitForType.ElementName renderer.RenderingOptions.HtmlElementToWaitFor = "myName" renderer.RenderingOptions.RenderDelay = 5000 ' Render the HTML content as a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Save the rendered PDF document pdf.SaveAs("DelayedRenderTest.pdf") $vbLabelText $csharpLabel Wait for Element Tag Name In the code example below, the rendering process will wait for a specific element tag name. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-tag-name.cs using IronPdf; // Import IronPdf namespace for PDF generation // Define a string containing HTML content. string htmlContent = @" <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Delayed render tests</title> <script type=""text/javascript""> // Use setTimeout to delay rendering setTimeout(function() { var newElem = document.createElement(""h2""); newElem.innerHTML = ""bla bla bla""; // Find the div with id 'x' and append the new h2 element var block = document.querySelector(""div#x""); block.appendChild(newElem); }, 1000); // Delay the execution by 1 second </script> </head> <body> <h1>This is Delayed Render Test!</h1> <div id=""x""></div> </body> </html>"; // Create an instance of ChromePdfRenderer, which uses Google Chrome headless to render PDFs ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set rendering options // Wait for up to 5000 milliseconds for an <h2> element to appear before rendering renderer.RenderingOptions.WaitFor = HtmlMatchType.ElementByTagName; renderer.RenderingOptions.HtmlElement = "h2"; renderer.RenderingOptions.RenderDelay = 5000; // Render the HTML content to a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Further code can save the pdf to a file or use it as needed Imports IronPdf ' Import IronPdf namespace for PDF generation ' Define a string containing HTML content. Private htmlContent As String = " <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Delayed render tests</title> <script type=""text/javascript""> // Use setTimeout to delay rendering setTimeout(function() { var newElem = document.createElement(""h2""); newElem.innerHTML = ""bla bla bla""; // Find the div with id 'x' and append the new h2 element var block = document.querySelector(""div#x""); block.appendChild(newElem); }, 1000); // Delay the execution by 1 second </script> </head> <body> <h1>This is Delayed Render Test!</h1> <div id=""x""></div> </body> </html>" ' Create an instance of ChromePdfRenderer, which uses Google Chrome headless to render PDFs Private renderer As New ChromePdfRenderer() ' Set rendering options ' Wait for up to 5000 milliseconds for an <h2> element to appear before rendering renderer.RenderingOptions.WaitFor = HtmlMatchType.ElementByTagName renderer.RenderingOptions.HtmlElement = "h2" renderer.RenderingOptions.RenderDelay = 5000 ' Render the HTML content to a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Further code can save the pdf to a file or use it as needed $vbLabelText $csharpLabel Use Query Selector In the code example below, the rendering process will wait for the element selected by the query selector. The HtmlQuerySelector method will wait for an img tag with an id of 'myid' and a class of 'blablastyle'. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-query-selector.cs // Import IronPdf namespace to use its classes and methods. using IronPdf; // Define the HTML content string with a delayed image loading script. string htmlContent = @" <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Test Registration of Extension</title> <script type=""text/javascript""> // Use setTimeout to delay execution of the image loading. setTimeout(function() { var img = document.createElement(""img""); img.onload = function() { // Set attributes for the newly created image element once it's loaded. img.setAttribute(""id"", ""myid""); img.setAttribute(""class"", ""blablastyle""); var block = document.getElementById(""x""); block.appendChild(img); }; // Load the image and set the src property after defining onload to handle cached images. img.src = ""https://www.w3schools.com/images/picture.jpg""; }, 1000); // Delay of 1 second before executing the image load. </script> </head> <body> <!-- Main content of the HTML page with a header and a div for the image --> <h1>This is Delayed Render Test!</h1> <div id=""x""></div> </body> </html>"; // Create an instance of the ChromePdfRenderer to render HTML to PDF. ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set the rendering options to wait for a specific element to be available in the DOM. // The WaitForHtmlQuerySelector method is used to wait for the image with specified id and class. renderer.RenderingOptions.WaitForHtmlQuerySelector("img#myid.blablastyle", 5000); // Render the HTML content to a PDF document. PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent); // Save the rendered PDF document to a file. // This is where the PDF document is saved to an appropriate location. pdf.SaveAs("output.pdf"); // Note: After this point, additional actions can be taken such as displaying or emailing the PDF. ' Import IronPdf namespace to use its classes and methods. Imports IronPdf ' Define the HTML content string with a delayed image loading script. Private htmlContent As String = " <!DOCTYPE html> <html lang=""en""> <head> <meta charset=""UTF-8""> <title>Test Registration of Extension</title> <script type=""text/javascript""> // Use setTimeout to delay execution of the image loading. setTimeout(function() { var img = document.createElement(""img""); img.onload = function() { // Set attributes for the newly created image element once it's loaded. img.setAttribute(""id"", ""myid""); img.setAttribute(""class"", ""blablastyle""); var block = document.getElementById(""x""); block.appendChild(img); }; // Load the image and set the src property after defining onload to handle cached images. img.src = ""https://www.w3schools.com/images/picture.jpg""; }, 1000); // Delay of 1 second before executing the image load. </script> </head> <body> <!-- Main content of the HTML page with a header and a div for the image --> <h1>This is Delayed Render Test!</h1> <div id=""x""></div> </body> </html>" ' Create an instance of the ChromePdfRenderer to render HTML to PDF. Private renderer As New ChromePdfRenderer() ' Set the rendering options to wait for a specific element to be available in the DOM. ' The WaitForHtmlQuerySelector method is used to wait for the image with specified id and class. renderer.RenderingOptions.WaitForHtmlQuerySelector("img#myid.blablastyle", 5000) ' Render the HTML content to a PDF document. Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent) ' Save the rendered PDF document to a file. ' This is where the PDF document is saved to an appropriate location. pdf.SaveAs("output.pdf") ' Note: After this point, additional actions can be taken such as displaying or emailing the PDF. $vbLabelText $csharpLabel Network Idle Example No Network Activity This type of network idle allows you to wait until there is no network activity, which typically indicates that the content has been fully loaded. This is suitable for a Single-Page Application (SPA) or a simple web page that doesn't have any long-polling network requests or ongoing network activity. The rendering process will only commence once there has been no ongoing network activity for at least 500ms. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-network-idle-0.cs using IronPdf; // Initialize a ChromePdfRenderer instance ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set the renderer's options to wait until there has been no network activity // detected for at least 500 milliseconds before rendering. renderer.RenderingOptions.RenderDelay = 500; // Render a simple HTML string into a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // Save the rendered PDF to a file // Specify the path and filename where you want to save the PDF pdf.SaveAs("output.pdf"); Imports IronPdf ' Initialize a ChromePdfRenderer instance Private renderer As New ChromePdfRenderer() ' Set the renderer's options to wait until there has been no network activity ' detected for at least 500 milliseconds before rendering. renderer.RenderingOptions.RenderDelay = 500 ' Render a simple HTML string into a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' Save the rendered PDF to a file ' Specify the path and filename where you want to save the PDF pdf.SaveAs("output.pdf") $vbLabelText $csharpLabel Two Network Activities Allowed The NetworkIdle2 method is suitable for web applications or web pages that have long-polling network requests or heartbeat pings. Typically, there are 1-2 requests involved. In this case, even if these requests are ongoing, they won't be considered to invalidate the triggering of the network idle event, as it is acceptable to have at most two of them. Before initiating the rendering process, there should be at most two network activities remaining for at least 500ms. This option provides a quick configuration for handling a fixed number of network activities. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-network-idle-2.cs ```csharp using IronPdf; // Create a new ChromePdfRenderer instance ChromePdfRenderer renderer = new ChromePdfRenderer(); // Configure the renderer to wait until the network is idle or only has up to 2 network activities for at least 500ms // The `WaitFor` property of `RenderingOptions` should be set to the appropriate strategy for waiting. // For network idle with up to 2 activities you can use `NetworkRequestInterceptionCount`. Example: renderer.RenderingOptions.WaitFor = NetworkIdle(2); // Render HTML content as a PDF document // Renders the specified HTML content into a PDF document PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); // The resulting PDF document is now stored in the variable `pdf` ``` ```csharp using IronPdf ' Create a new ChromePdfRenderer instance Dim renderer As New ChromePdfRenderer() ' Configure the renderer to wait until the network is idle or only has up to 2 network activities for at least 500ms ' The `WaitFor` property of `RenderingOptions` should be set to the appropriate strategy for waiting. ' For network idle with up to 2 activities you can use `NetworkRequestInterceptionCount`. Example: renderer.RenderingOptions.WaitFor = NetworkIdle(2) ' Render HTML content as a PDF document ' Renders the specified HTML content into a PDF document Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") ' The resulting PDF document is now stored in the variable `pdf` 'INSTANT VB TODO TASK: The following line uses invalid syntax: '``` $vbLabelText $csharpLabel Customize Network Activity Allowance In cases where multiple network requests are involved, you have the flexibility to customize both the network idle duration and the number of allowed network requests that don't invalidate the triggering of the network idle event. This option is suitable for web applications or web pages with specific requirements that don't fit the previous two methods. By providing this customization, we ensure that we address a wide range of use cases under different circumstances. :path=/static-assets/pdf/content-code-examples/how-to/waitfor-customize-network.cs using IronPdf; // Instantiates a new ChromePdfRenderer that will be used to render HTML to PDF. ChromePdfRenderer renderer = new ChromePdfRenderer(); // Configure rendering options to handle network activities before rendering the PDF. // `WaitForNetworkIdle` is used to ensure the renderer waits for the network to be idle // with no more than a defined number of active network connections for a specified period (in milliseconds). renderer.RenderingOptions.WaitForNetworkIdle = new IronPdf.Rendering.WaitForNetworkIdle { // Allows a maximum of 5 active network connections before the renderer deems the network 'idle'. MaximumActivities = 5, // Minimum idle time (in milliseconds) to wait before considering the network to be idle. IdleTimeMilliseconds = 1000 }; // Render the HTML content into a PDF document. // The HTML string "<h1>testing</h1>" will be converted to a PDF format. PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>"); Imports IronPdf ' Instantiates a new ChromePdfRenderer that will be used to render HTML to PDF. Private renderer As New ChromePdfRenderer() ' Configure rendering options to handle network activities before rendering the PDF. ' `WaitForNetworkIdle` is used to ensure the renderer waits for the network to be idle ' with no more than a defined number of active network connections for a specified period (in milliseconds). renderer.RenderingOptions.WaitForNetworkIdle = New IronPdf.Rendering.WaitForNetworkIdle With { .MaximumActivities = 5, .IdleTimeMilliseconds = 1000 } ' Render the HTML content into a PDF document. ' The HTML string "<h1>testing</h1>" will be converted to a PDF format. Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>") $vbLabelText $csharpLabel Set Maximum Waiting Time In addition, the JavaScript, NetworkIdle, NetworkIdle0, and NetworkIdle2 methods also allow you to set a maximum waiting time to ensure that the wait will not be indefinite. The maxWaitTime parameter of these methods can be used for this purpose. Please noteAll specified time values are considered to be in milliseconds. Frequently Asked Questions What is the purpose of the WaitFor class in a PDF rendering library? The WaitFor class in IronPDF is designed to delay PDF rendering until all necessary assets and animations have been loaded, ensuring complete and correct rendering of the PDF document. What are the available options in a WaitFor class for rendering PDFs? The WaitFor class offers several options, including PageLoad, RenderDelay, Fonts, JavaScript, HTML elements, and NetworkIdle, each designed to wait for different conditions before rendering the PDF using IronPDF. How does the PageLoad option work in a WaitFor class? The PageLoad option allows the PDF to render immediately after the page has finished loading, requiring no additional waiting when using IronPDF. How can I use the RenderDelay option in a WaitFor class? The RenderDelay option in IronPDF allows you to set an arbitrary number of milliseconds as a delay before rendering the PDF, providing flexibility for specific timing requirements. What does the AllFontsLoaded method do in a PDF rendering context? The AllFontsLoaded method in IronPDF pauses the PDF rendering process until all fonts are loaded from external sources, ensuring the final PDF includes all required fonts. How can JavaScript be used with a WaitFor class for PDF rendering? In IronPDF, you can specify a custom JavaScript function to execute before rendering the PDF, giving you control over when to trigger the render. What is the purpose of a NetworkIdle option in a WaitFor class? The NetworkIdle option in IronPDF waits for a specified level of network activity to cease before rendering, ensuring all content is loaded. Can I customize the network activity allowance in a WaitFor class? Yes, in IronPDF, you can customize both the network idle duration and the number of allowed network requests that don't invalidate the network idle event. How do I set a maximum waiting time in a WaitFor class? The JavaScript, NetworkIdle, NetworkIdle0, and NetworkIdle2 methods in IronPDF allow you to set a maximum waiting time using the maxWaitTime parameter to prevent indefinite waiting. Is the RenderDelay property still available in PDF rendering libraries? The old RenderingOptions.RenderDelay property in IronPDF has been deprecated, and it is recommended to use the new API, RenderingOptions.WaitFor.RenderDelay. Chaknith Bin Chat with engineering team now Software Engineer Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking. Ready to Get Started? Free NuGet Download Total downloads: 14,143,061 View Licenses