How to Use the WaitFor to delay C# PDF Rendering

Using the C# WaitFor Class to Delay PDF Rendering

The C# WaitFor class in IronPDF helps developers delay PDF rendering until all assets, fonts, and JavaScript have loaded, preventing incomplete renders by using methods like RenderDelay, NetworkIdle, and JavaScript triggers.

Quickstart: Using WaitFor to Improve PDF Rendering

IronPDF's WaitFor feature allows developers to improve PDF rendering by managing asynchronous timing. By setting options like RenderDelay, you can ensure all assets and scripts are loaded before the conversion, preventing incomplete PDF documents. This guide shows how to implement WaitFor in your projects for precise and efficient rendering.

new IronPdf.ChromePdfRenderer { RenderingOptions = { WaitFor = IronPdf.WaitFor.RenderDelay(3000) } }
    .RenderUrlAsPdf("https://example.com")
    .SaveAs("output.pdf");
new IronPdf.ChromePdfRenderer { RenderingOptions = { WaitFor = IronPdf.WaitFor.RenderDelay(3000) } }
    .RenderUrlAsPdf("https://example.com")
    .SaveAs("output.pdf");
$vbLabelText   $csharpLabel

What Is the WaitFor Class and Its Options?

For rendering PDFs with optimal performance, the WaitFor class improves the PDF rendering process. The WaitFor object from RenderOptions offers several options:

  • PageLoad: Default render with no waiting.
  • RenderDelay: Sets an arbitrary waiting time.
  • Fonts: Waits until all fonts have loaded.
  • JavaScript: Triggers 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: Waits for network idle (0, 2, or a custom amount).

These features work when converting HTML strings to PDF with IronPDF, HTML files to PDF with IronPDF, and web URLs to PDF with IronPDF.

How Do I Render PDFs Immediately Without Waiting?

By default, rendering occurs immediately after the page finishes loading. The PageLoad method doesn't need to be called for normal rendering. This approach works well for simple HTML content without complex JavaScript or external resources.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-pageload.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render as soon as the page is loaded
renderer.RenderingOptions.WaitFor.PageLoad();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
$vbLabelText   $csharpLabel

How Can I Add a Custom Delay Before PDF Rendering?

When a specific delay is required before rendering the PDF, you can set an arbitrary number of milliseconds as a delay. This provides flexibility for specific timing requirements, especially when dealing with JavaScript-heavy content.

This option works the same as the old implementation using the RenderingOptions.RenderDelay property. However, the old property has been deprecated. Use the new API: RenderingOptions.WaitFor.RenderDelay.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-delay-time.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render after 3000ms
renderer.RenderingOptions.WaitFor.RenderDelay(3000);

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
$vbLabelText   $csharpLabel

The delay mechanism is useful when your HTML contains animations, lazy-loaded content, or requires time for dynamic content generation. For complex rendering scenarios, explore custom rendering options.

How Do I Wait for All Fonts to Load Before Rendering?

The AllFontsLoaded method in the WaitFor class pauses PDF rendering until all fonts load from external sources like Google Fonts. This ensures the final PDF includes all required fonts, preserving the document's intended typography and visual appearance. This feature maintains professional PDF output quality.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-all-fonts.cs
using IronPdf;

string htmlContent = @"
<!DOCTYPE html>
<html lang=""en"">
<head>
  <meta charset=""UTF-8"">
  <title>Test Registration of Extension</title>
  <!-- for google web fonts -->
  <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>
  /* for remote fonts */
  @font-face {
    font-family: 'CustomFont';
    src: url('https://stage.gradfinale.co.uk/tcpdf/fonts/avgr65wttf.ttf');
  }
  p#p1 { font-family: CustomFont, sans-serif; }

  /* for local fonts */
  @font-face {
    font-family: 'LocalCustomFont';
    src: local('Arial');
  }
  p#p3 { font-family: LocalCustomFont, sans-serif; }
  </style>
</head>
<body>
	<h1>This is Delayed Render Test!</h1>
	<p style=""font-family: Roboto, monospace;"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p>
	<p id=""p1"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p>
	<p id=""p3"">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla scelerisque ligula venenatis erat <strong>scelerisque</strong> auctor.</p>
</body>
</html>)";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.AllFontsLoaded(10000);

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
$vbLabelText   $csharpLabel

For more information on managing fonts in PDFs, including embedding and subsetting options, check the guide on managing fonts in PDFs.

How Do I Trigger PDF Rendering Using Custom JavaScript?

For greater control over the rendering process, you can specify a custom JavaScript function that executes before rendering the PDF document. This enables you to perform necessary tasks or checks before initiating the rendering process, giving you control over when to trigger the render.

In JavaScript, the function window.ironpdf.notifyRender() triggers the rendering task. Once notifyRender() is invoked, the rendering process starts. You control when to invoke the function.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-javascript.cs
using IronPdf;

string html = @"<!DOCTYPE html>
<html>
<body>
<h1>Testing</h1>
<script type='text/javascript'>

// Set delay
setTimeout(function() {
    window.ironpdf.notifyRender();
}, 1000);

</script>
</body>
</html>";

ChromePdfRenderOptions renderingOptions = new ChromePdfRenderOptions();

// Set rendering to wait for the notifyRender function
renderingOptions.WaitFor.JavaScript(5000);

PdfDocument pdf = ChromePdfRenderer.StaticRenderHtmlAsPdf(html, renderingOptions);
JAVASCRIPT

This approach is effective when combined with AJAX requests or when ensuring dynamically generated content fully loads before rendering. For advanced JavaScript scenarios, see the guide on executing custom JavaScript in PDFs.

How Can I Wait for Specific HTML Elements Before Rendering?

With this option, the rendering process waits for specific HTML elements, such as element IDs, names, tag names, and query selectors to target elements. This feature helps when working with Single Page Applications (SPAs) or content that loads asynchronously.

How Do I Wait for an Element by ID?

The rendering waits for a specific element ID in the code example below.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-id.cs
using IronPdf;

string htmlContent = @"
<!DOCTYPE html>
<html lang=""en"">
<head>
  <meta charset=""UTF-8"">
  <title>Delayed render tests</title>
  <script type=""text/javascript"">
	setTimeout(function() {
		var h1Tag = document.createElement(""h1"");
		h1Tag.innerHTML = ""bla bla bla"";
		h1Tag.setAttribute(""id"", ""myid"");

        var block = document.querySelector(""div#x"");
		block.appendChild(h1Tag);
	}, 1000);
  </script>
</head>
<body>
	<h1>This is Delayed Render Test!</h1>
    <div id=""x""></div>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.HtmlElementById("myid", 5000);

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
$vbLabelText   $csharpLabel

How Do I Wait for an Element by Name Attribute?

The rendering process waits for a specific element name in the code example below.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-id.cs
using IronPdf;

string htmlContent = @"
<!DOCTYPE html>
<html lang=""en"">
<head>
  <meta charset=""UTF-8"">
  <title>Delayed render tests</title>
  <script type=""text/javascript"">
	setTimeout(function() {
		var h1Tag = document.createElement(""h1"");
		h1Tag.innerHTML = ""bla bla bla"";
		h1Tag.setAttribute(""id"", ""myid"");

        var block = document.querySelector(""div#x"");
		block.appendChild(h1Tag);
	}, 1000);
  </script>
</head>
<body>
	<h1>This is Delayed Render Test!</h1>
    <div id=""x""></div>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.HtmlElementById("myid", 5000);

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
$vbLabelText   $csharpLabel

How Do I Wait for Elements by Tag Name?

The rendering process waits for a specific element tag name in the code example below.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-html-element-tag-name.cs
using IronPdf;

string htmlContent = @"
<!DOCTYPE html>
<html lang=""en"">
<head>
  <meta charset=""UTF-8"">
  <title>Delayed render tests</title>
  <script type=""text/javascript"">
	setTimeout(function() {
		var newElem = document.createElement(""h2"");
		newElem.innerHTML = ""bla bla bla"";

        var block = document.querySelector(""div#x"");
		block.appendChild(newElem);
	}, 1000);
  </script>
</head>
<body>
	<h1>This is Delayed Render Test!</h1>
    <div id=""x""></div>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.HtmlElementByTagName("h2", 5000);

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
$vbLabelText   $csharpLabel

How Do I Use Query Selectors to Wait for Elements?

The rendering process waits for the element selected by the query selector in the code example below. The HtmlQuerySelector method waits 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
using IronPdf;

string htmlContent = @"
<!DOCTYPE html>
<html lang=""en"">
<head>
  <meta charset=""UTF-8"">
  <title>Test Registration of Extension</title>
  <script type=""text/javascript"">
	setTimeout(function() {
		var img = document.createElement(""img"");
		img.onload = function() {
			img.setAttribute(""id"", ""myid"");
			img.setAttribute(""class"", ""blablastyle"");
			var block = document.getElementById(""x"");
			block.appendChild(img);
		};
		img.src = ""https://www.w3schools.com/images/picture.jpg"";	// .src after .onload to ignore cached, if any
	}, 1000);
  </script>
</head>
<body>
	<h1>This is Delayed Render Test!</h1>
    <div id=""x""></div>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.HtmlQuerySelector("img#myid.blablastyle", 5000);

PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
$vbLabelText   $csharpLabel

How Do I Wait for Network Activity to Complete?

When rendering web pages that make network requests for resources, data, or API calls, ensure all network activity completes before generating the PDF. IronPDF provides several methods to handle different network scenarios.

When Should I Wait for Zero Network Activity?

This type of network idle waits until there is no network activity, typically indicating that content has fully loaded. This suits Single-Page Applications (SPAs) or simple web pages without long-polling network requests or ongoing network activity.

The rendering process commences 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;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render unless there has been no network activity for at least 500ms
renderer.RenderingOptions.WaitFor.NetworkIdle0();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
$vbLabelText   $csharpLabel

When Should I Allow Two Network Activities?

The NetworkIdle2 method suits web applications or web pages with long-polling network requests or heartbeat pings. Typically, 1-2 requests are involved. Even if these requests are ongoing, they won't invalidate the triggering of the network idle event, as having at most two is acceptable.

Before initiating the rendering process, there should be at most two network activities remaining for at least 500ms. This option provides quick configuration for handling a fixed number of network activities.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-network-idle-2.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render unless there are at most 2 network activities for at least 500ms
renderer.RenderingOptions.WaitFor.NetworkIdle2();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
$vbLabelText   $csharpLabel

How Do I Customize Network Activity Thresholds?

When multiple network requests are involved, you can customize both the network idle duration and the number of allowed network requests that don't invalidate the network idle event. This option suits web applications or web pages with specific requirements that don't fit the previous two methods. This customization addresses a wide range of use cases under different circumstances.

:path=/static-assets/pdf/content-code-examples/how-to/waitfor-customize-network.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render unless there are at most 5 network activities for at least 1000ms
renderer.RenderingOptions.WaitFor.NetworkIdle(1000, 5);

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");
$vbLabelText   $csharpLabel

This level of customization is valuable when working with complex web applications that maintain persistent connections or have predictable background network activity patterns.

How Do I Set a Maximum Waiting Time?

The JavaScript, NetworkIdle, NetworkIdle0, and NetworkIdle2 methods allow you to set a maximum waiting time to ensure the wait won't be indefinite. The maxWaitTime parameter of these methods serves this purpose. This prevents your application from hanging indefinitely if the expected conditions never occur.

All specified time values are in milliseconds.

When implementing these waiting strategies, balance ensuring complete content loading with maintaining reasonable performance. For applications requiring precise timing control, explore async PDF generation techniques.

Ready to see what else you can do? Check the tutorial page here: Additional Features or explore creating PDFs with IronPDF for more advanced techniques.

Frequently Asked Questions

What is the WaitFor class and why should I use it?

The WaitFor class in IronPDF allows developers to delay PDF rendering until all assets, fonts, and JavaScript have finished loading. This prevents incomplete renders and ensures that complex web pages are fully loaded before converting to PDF, resulting in more accurate and complete PDF documents.

What are the different WaitFor options available?

IronPDF's WaitFor class offers several options: PageLoad (default immediate rendering), RenderDelay (custom delay in milliseconds), Fonts (waits for all fonts to load), JavaScript (triggers render via JavaScript function), HTML elements (waits for specific elements by ID, name, tag, or query selector), and NetworkIdle (waits for network activity to cease).

How do I add a custom delay before PDF rendering?

You can add a custom delay using the RenderDelay method in IronPDF. Simply specify the delay in milliseconds: `new ChromePdfRenderer { RenderingOptions = { WaitFor = WaitFor.RenderDelay(3000) } }`. This is particularly useful for JavaScript-heavy content that needs extra time to execute.

Can I wait for specific HTML elements to load before rendering?

Yes, IronPDF allows you to wait for specific HTML elements using element IDs, names, tag names, or query selectors. This ensures that critical content is present on the page before the PDF conversion begins, preventing missing elements in your final PDF document.

What is NetworkIdle and when should I use it?

NetworkIdle in IronPDF waits for network activity to cease before rendering the PDF. You can set it to wait for 0, 2, or a custom number of network connections. This is ideal for pages that load content dynamically through AJAX calls or other asynchronous network requests.

How can I trigger PDF rendering from JavaScript?

IronPDF's WaitFor JavaScript option allows you to trigger the render process from a JavaScript function. This gives you complete control over when the PDF generation occurs, making it perfect for single-page applications or complex JavaScript-driven websites where you need precise timing control.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Ready to Get Started?
Nuget Downloads 16,901,161 | Version: 2025.12 just released