How to Use Rendering Options in IronPDF with C#
Rendering options in IronPDF customize PDF generation through the ChromePdfRenderer class, controlling settings like margins, headers, footers, paper size, JavaScript execution, and CSS media types to create precisely formatted PDF documents from HTML, CSS, and other content sources.
- Install IronPDF via NuGet Package Manager
- Create a
ChromePdfRendererinstance - Configure rendering options through the
RenderingOptionsproperty - Render your content (HTML, Markdown, etc.) to PDF
- Save the resulting PDF document
-
1Install IronPDF with NuGet Package Manager
-
2Copy and run this code snippet.
new IronPdf.ChromePdfRenderer { RenderingOptions = { PrintHtmlBackgrounds = true, MarginTop = 0, MarginBottom = 0, CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print, HtmlHeader = new IronPdf.HtmlHeaderFooter { HtmlFragment = "<div>My Header</div>" }, Timeout = 120000 } } .RenderHtmlStringAsPdf("<h1>Hello Options</h1>") .SaveAs("renderingOptions.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Rendering options in PDF generation are settings that determine how a PDF document is created, displayed, and printed. These options include rendering form elements, enabling JavaScript, generating tables of contents, adding headers and footers, adjusting margins, setting paper sizes, and more.
The ChromePdfRenderer class in IronPDF provides various rendering options for customizing PDF generation. It includes PaperFit, a manager that controls content layout on PDF pages, offering different styles such as responsive CSS3 layouts or continuous feed. When working with complex documents, you might need to merge or split PDFs after applying rendering options.
Minimal Workflow (5 steps)
- Download the C# library to use rendering options
- Prepare the HTML assets or file for PDF conversion
- Instantiate the ChromePdfRenderer class
- Access the RenderingOptions property to configure settings
- Render the PDF in the desired format
How Do I Use Rendering Options in IronPDF?
While many rendering option properties are designed for HTML-to-PDF conversion, they work with other PDF conversion types too. Let's render Markdown to PDF and configure the output using rendering options. For HTML conversions specifically, learn about converting HTML files to PDF or converting HTML strings to PDF.
Why Should I Configure Rendering Options?
Configuring rendering options ensures PDFs generate with exact specifications: custom paper sizes, specific margins, headers and footers, or enabled JavaScript for dynamic content. This control is crucial when creating new PDFs for professional documents or reports.
What Happens When I Apply Multiple Rendering Options?
Multiple rendering options work together to create the final PDF output. Each option modifies a specific rendering aspect, and IronPDF applies them sequentially during conversion. For example, when setting both margins and headers, header content respects margin settings unless overridden using the UseMarginsOnHeaderAndFooter property.
using IronPdf;
// Instantiate a ChromePdfRenderer object, which uses a headless version of the Chrome browser
// to render HTML/CSS as a PDF document.
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Configure rendering options
// Enable printing of HTML backgrounds to ensure all styles are visible.
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
// Set HTML header content using HtmlHeaderFooter.
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
// HTML fragment to add a header at the top of every page in the PDF.
HtmlFragment = "<h1>Header Content</h1>"
};
// Set a custom paper size for the PDF in millimeters (width and height).
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(150, 150);
// Set the top margin to zero to start the content from the very top of the page.
renderer.RenderingOptions.MarginTop = 0;
// Define a Markdown string that will be rendered as a PDF.
// Markdown text allows basic formatting like bold and italic styles.
string md = "This is some **bold** and *italic* text.";
// Render the Markdown string to a PDF document.
// The library will convert Markdown syntax into equivalent HTML before rendering it as a PDF.
PdfDocument pdf = renderer.RenderMarkdownStringAsPdf(md);
// Save the generated PDF to a file named "renderingOptions.pdf."
pdf.SaveAs("renderingOptions.pdf");Imports IronPdf
' Instantiate a ChromePdfRenderer object, which uses a headless version of the Chrome browser
' to render HTML/CSS as a PDF document.
Private renderer As New ChromePdfRenderer()
' Configure rendering options
' Enable printing of HTML backgrounds to ensure all styles are visible.
renderer.RenderingOptions.PrintHtmlBackgrounds = True
' Set HTML header content using HtmlHeaderFooter.
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {.HtmlFragment = "<h1>Header Content</h1>"}
' Set a custom paper size for the PDF in millimeters (width and height).
renderer.RenderingOptions.SetCustomPaperSizeinMilimeters(150, 150)
' Set the top margin to zero to start the content from the very top of the page.
renderer.RenderingOptions.MarginTop = 0
' Define a Markdown string that will be rendered as a PDF.
' Markdown text allows basic formatting like bold and italic styles.
Dim md As String = "This is some **bold** and *italic* text."
' Render the Markdown string to a PDF document.
' The library will convert Markdown syntax into equivalent HTML before rendering it as a PDF.
Dim pdf As PdfDocument = renderer.RenderMarkdownStringAsPdf(md)
' Save the generated PDF to a file named "renderingOptions.pdf."
pdf.SaveAs("renderingOptions.pdf")Advanced Rendering Options Example
This comprehensive example demonstrates combining multiple rendering options for professional document generation. This approach helps when you need to add headers and footers or work with custom paper sizes:
using IronPdf;
using IronPdf.Rendering;
// Create renderer with advanced options
var renderer = new ChromePdfRenderer();
// Configure paper and layout settings
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Portrait;
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PrintHtmlBackgrounds = true;
// Set margins for professional layout
renderer.RenderingOptions.MarginTop = 40; // mm
renderer.RenderingOptions.MarginBottom = 40; // mm
renderer.RenderingOptions.MarginLeft = 20; // mm
renderer.RenderingOptions.MarginRight = 20; // mm
// Enable JavaScript for dynamic content
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.RenderDelay(2000); // Wait 2 seconds for JS to execute
// Add professional header with page numbers
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
Height = 25,
HtmlFragment = @"<div style='text-align: center; padding: 10px;'>
<span>Document Title</span> - Page {page} of {total-pages}
</div>",
DrawDividerLine = true
};
// Add footer with timestamp
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
Height = 20,
HtmlFragment = @"<div style='text-align: center; font-size: 10px;'>
Generated on {date} at {time}
</div>"
};
// Render HTML content
string htmlContent = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
.content { padding: 20px; }
</style>
</head>
<body>
<div class='content'>
<h1>Professional Document</h1>
<p>This document demonstrates advanced rendering options.</p>
</div>
</body>
</html>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("professional-document.pdf");
The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
What Are All Available Rendering Options?
Advanced options define PDF-rendering settings like margins, paper orientation, paper size, and more. Understanding these options helps when you need to set custom margins or work with different viewport settings and zoom levels.
Which Rendering Options Are Most Commonly Used?
The most commonly used rendering options include margin settings, paper size configuration, header/footer setup, and JavaScript enabling. These options cover most PDF customization needs. For web-based content, JavaScript rendering options ensure dynamic elements render correctly.
How Do I Choose the Right Rendering Options?
Choose rendering options based on your requirements: use margin settings for print layouts, enable JavaScript for dynamic web content, configure headers/footers for professional documents, and adjust paper size for specific output formats. When working with web content, consider CSS media types for optimal rendering.
When Should I Use Advanced Rendering Options?
Use advanced rendering options for specialized features like custom CSS injection, LaTeX mathematical rendering, grayscale output, or when working with complex web applications requiring specific JavaScript execution timing. The WaitFor class provides precise control over rendering delays for complex scenarios.
Working with Form Elements
When HTML contains form elements, IronPDF automatically converts them to interactive PDF forms:
using IronPdf;
var renderer = new ChromePdfRenderer();
// Enable PDF form creation from HTML forms
renderer.RenderingOptions.CreatePdfFormsFromHtml = true;
// HTML with form elements
string formHtml = @"
<html>
<body>
<form>
<label>Name: <input type='text' name='name' /></label><br>
<label>Email: <input type='email' name='email' /></label><br>
<label>Subscribe: <input type='checkbox' name='subscribe' /></label><br>
<button type='submit'>Submit</button>
</form>
</body>
</html>";
PdfDocument pdfWithForms = renderer.RenderHtmlAsPdf(formHtml);
pdfWithForms.SaveAs("interactive-form.pdf");Imports IronPdf
Dim renderer = New ChromePdfRenderer()
' Enable PDF form creation from HTML forms
renderer.RenderingOptions.CreatePdfFormsFromHtml = True
' HTML with form elements
Dim formHtml As String = "
<html>
<body>
<form>
<label>Name: <input type='text' name='name' /></label><br>
<label>Email: <input type='email' name='email' /></label><br>
<label>Subscribe: <input type='checkbox' name='subscribe' /></label><br>
<button type='submit'>Submit</button>
</form>
</body>
</html>"
Dim pdfWithForms As PdfDocument = renderer.RenderHtmlAsPdf(formHtml)
pdfWithForms.SaveAs("interactive-form.pdf")Below is a table illustrating the different options available.
| Class | ChromePdfRenderer | |
|---|---|---|
| Description | Used to define PDF printout options, like paper size, DPI, headers, and footers | |
| Properties / functions | Type | Description |
CustomCookies | Dictionary<string, string> | Custom cookies for the HTML render. Cookies do not persist between renders and must be set each time. |
PaperFit | VirtualPaperLayoutManager | A manager for setting up virtual paper layouts, controlling how content will be laid out on PDF "paper" pages. Includes options for Default Chrome Behavior, Zoomed, Responsive CSS3 Layouts, Scale-To-Page & Continuous Feed style PDF page setups. |
UseMarginsOnHeaderAndFooter | UseMargins | Use margin values from the main document when rendering headers and footers. |
CreatePdfFormsFromHtml | bool | Turns all HTML form elements into editable PDF forms. Default value is true. |
CssMediaType | PdfCssMediaType | Enables Media="screen" CSS Styles and StyleSheets. Default value is PdfCssMediaType.Screen. |
CustomCssUrl | string | Allows a custom CSS style-sheet to be applied to HTML before rendering. May be a local file path or a remote URL. Only applicable when rendering HTML to PDF. |
EnableJavaScript | bool | Enables JavaScript and JSON to be executed before the page is rendered. Ideal for printing from Ajax / Angular Applications. Default value is false. |
EnableMathematicalLaTex | bool | Enables rendering of Mathematical LaTeX Elements. |
Javascript | string | A custom JavaScript string to be executed after all HTML has loaded but before PDF rendering. |
JavascriptMessageListener | StringDelegate | A method callback to be invoked whenever a browser JavaScript console message becomes available. |
FirstPageNumber | int | First page number to be used in PDF Headers and Footers. Default value is 1. |
TableOfContents | TableOfContentsTypes | Generates a table of contents at the location in the HTML document where an element is found with id "IronPDF-toc". |
GrayScale | bool | Outputs a black-and-white PDF. Default value is false. |
TextHeader | ITextHeaderFooter | Sets the footer content for every PDF page as text, supporting 'mail-merge' and automatically turning URLs into hyperlinks. |
TextFooter | ||
HtmlHeader | HtmlHeaderFooter | Sets the header content for every PDF page as HTML. Supports 'mail-merge'. |
HtmlFooter | ||
InputEncoding | Encoding | The input character encoding as a string. Default value is Encoding.UTF8. |
MarginTop | double | Top PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
MarginRight | double | Right PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
MarginBottom | double | Bottom PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
MarginLeft | double | Left PDF "paper" margin in millimeters. Set to zero for border-less and commercial printing applications. Default value is 25. |
PaperOrientation | PdfPaperOrientation | The PDF paper orientation, such as Portrait or Landscape. Default value is Portrait. |
PaperSize | PdfPaperSize | Sets the paper size |
SetCustomPaperSizeinCentimeters | double | Sets the paper size in centimeters. |
SetCustomPaperSizeInInches | Sets the paper size in inches. | |
SetCustomPaperSizeinMilimeters | Sets the paper size in millimeters. | |
SetCustomPaperSizeinPixelsOrPoints | Sets the paper size in screen pixels or printer points. | |
PrintHtmlBackgrounds | Boolean | Indicates whether to print background colors and images from HTML. Default value is true. |
RequestContext | RequestContexts | Request context for this render, determining isolation of certain resources such as cookies. |
Timeout | Integer | Render timeout in seconds. Default value is 60. |
Title | String | PDF Document Name and Title metadata, useful for mail-merge and automatic file naming in the IronPDF MVC and Razor extensions. |
ForcePaperSize | Boolean | Force page sizes to be exactly what is specified via IronPdf.ChromePdfRenderOptions.PaperSize by resizing the page after generating a PDF from HTML. Helps correct small errors in page size when rendering HTML to PDF. |
WaitFor | WaitFor | A wrapper object that holds configuration for wait-for mechanism for users to wait for certain events before rendering. By default, it will wait for nothing. |
Ready to explore more capabilities? Visit our tutorial page: Convert PDFs
Frequently Asked Questions
What are the primary rendering options available in IronPDF?
IronPDF offers options like setting margins, headers, footers, paper size, and enabling JavaScript execution to control PDF generation and formatting from HTML.
How can I use IronPDF to create a PDF with no margins?
You can set the `MarginTop`, `MarginBottom`, `MarginLeft`, and `MarginRight` properties of the `ChromePdfRenderer` to zero to generate a PDF without margins using IronPDF.
Is it possible to generate a PDF with a custom paper size using IronPDF?
Yes, IronPDF allows you to specify custom paper sizes in millimeters, inches, centimeters, or pixels using the `SetCustomPaperSize` methods available in the `RenderingOptions`.
Can IronPDF render HTML content that includes JavaScript?
Certainly, IronPDF’s `EnableJavaScript` property in the `RenderingOptions` allows for JavaScript to execute before rendering the HTML to PDF.
How do advanced rendering options benefit professional document generation in IronPDF?
Advanced options like adding headers and footers, enabling JavaScript for dynamic content, configuring custom CSS, and setting precise margins enhance the customization for professional document outputs.
What is the function of the `PaperFit` option in IronPDF?
`PaperFit` in IronPDF’s `ChromePdfRenderer` controls content layout styles on PDF pages, providing options like responsive CSS3 layouts and continuous feed style setups.
How can IronPDF assist with rendering interactive PDF forms?
IronPDF automatically converts HTML form elements into interactive PDF forms when the `CreatePdfFormsFromHtml` property is set to true.
What is the significance of enabling `PrintHtmlBackgrounds` in IronPDF?
Enabling `PrintHtmlBackgrounds` ensures that all background colors and images defined in HTML are printed in the PDF, maintaining the visual integrity of the document.
Can IronPDF merge or split PDFs after applying rendering options?
Yes, IronPDF supports merging and splitting of PDF documents, which can be useful when combining or breaking down complex documents after rendering.
How does IronPDF handle dynamic web content rendering?
IronPDF handles dynamic web content by enabling JavaScript execution and allowing delays for JavaScript execution using the `WaitFor` class to ensure all interactive elements are fully rendered.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.