How to Set Custom Margins in IronPDF C#
IronPDF enables developers to set custom margins when rendering PDFs from HTML through simple configuration of the ChromePdfRenderer's RenderingOptions, allowing precise control over top, bottom, left, and right margins in millimeters.
Set custom margins when rendering PDFs from HTML with simple configuration.
-
1Install IronPDF with NuGet Package Manager
PM > Install-Package IronPdf
-
2Copy and run this code snippet.
new IronPdf.ChromePdfRenderer { RenderingOptions = { MarginTop = 10, MarginBottom = 10, MarginLeft = 10, MarginRight = 10, UseMarginsOnHeaderAndFooter = IronPdf.UseMargins.All } } .RenderHtmlAsPdf("<h1>Hello with margins!</h1>") .SaveAs("custom-margins.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 Steps):
- Download IronPDF from NuGet
- Instantiate the
ChromePdfRendererclass - Set margin values in
RenderingOptions - Configure header and footer margin behavior
- Render HTML to PDF and save
How Do I Set Custom PDF Margins in IronPDF?
To set custom margins, instantiate the ChromePdfRenderer class. Access the RenderingOptions object to set specific margins in millimeters for top, bottom, left, and right:
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 40;Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.MarginTop = 40
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20
renderer.RenderingOptions.MarginBottom = 40Output
The heading and paragraph sit inside the 40mm top and bottom and 20mm left and right margins set on the renderer.
The margin settings work with IronPDF's Chrome rendering engine, which provides accurate HTML to PDF conversion. For advanced PDF generation settings, see the guide on rendering options.
How Do Margins Interact with CSS Styles?
Margins add to those set in the HTML style section. In the example below, HTML margins are 50mm, and RenderingOptions adds 30mm, resulting in 80mm total margins:
const string htmlWithStyle = @"
<!DOCTYPE html>
<html>
<head>
<style>
body {margin: 50mm 50mm 50mm 50mm;}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 30;
renderer.RenderingOptions.MarginLeft = 30;
renderer.RenderingOptions.MarginRight = 30;
renderer.RenderingOptions.MarginBottom = 30;
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlWithStyle);
pdf.SaveAs("PdfWithCustomMargins.pdf");Const htmlWithStyle As String = "
<!DOCTYPE html>
<html>
<head>
<style>
body {margin: 50mm 50mm 50mm 50mm;}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.MarginTop = 30
renderer.RenderingOptions.MarginLeft = 30
renderer.RenderingOptions.MarginRight = 30
renderer.RenderingOptions.MarginBottom = 30
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlWithStyle)
pdf.SaveAs("PdfWithCustomMargins.pdf")Output
The 50mm CSS body margins and the 30mm RenderingOptions margins combine, so the content is inset 80mm on every side.
When working with CSS in HTML documents, IronPDF supports responsive CSS and media queries, allowing professional layouts. This margin behavior matters when converting HTML files to PDF or working with HTML strings.
How Do I Control Whether CSS @page or C# Margins Win?
The example above adds CSS body margins to your RenderingOptions margins. CSS @page rules are separate: by default IronPDF leaves their margins untouched. When you need the @page margins and your RenderingOptions margins to follow a defined precedence, set RenderingOptions.CssPageRulePolicy:
| CssPageRulePolicy | Behavior |
|---|---|
| Default | Legacy behavior; IronPDF does not touch CSS @page margins. This is the default. |
| RenderingOptionsWin | The C# RenderingOptions.Margin* values win; margin declarations inside @page are stripped and replaced. |
| CssPageWin | The CSS @page margins win; IronPDF reads and applies them so the body and the header/footer band agree. |
using IronPdf;
using IronPdf.Rendering;
const string htmlWithAtPageRule = @"
<!DOCTYPE html>
<html>
<head>
<style>
@page { margin: 25mm 20mm 25mm 20mm; }
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Let the HTML's own @page rule dictate margins; C# only sets size/orientation and header/footer
renderer.RenderingOptions.CssPageRulePolicy = CssPageRulePolicy.CssPageWin;
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlWithAtPageRule);
pdf.SaveAs("PdfWithCssPageMargins.pdf");
Output
Under CssPageWin the CSS @page rule wins, giving 25mm top and bottom and 20mm left and right margins.
CssPageRulePolicy controls page margins only. It does not set page size or orientation, which always come from RenderingOptions.PaperSize and PaperOrientation. Under CssPageWin, only the sides your CSS actually declares are applied (if @page omits margin-bottom, no footer space is reserved), and header/footer auto-sizing is disabled, so give any header/footer a concrete MaxHeight. If an @page rule uses margin-box at-rules such as @top-center, its margins may not be fully reconciled and a warning is logged.What Are Common Margin Values for Different Standards?
Documents often require specific margins to meet various standards. MLA and APA formats require 1-inch margins, while dissertations may need 1.5-inch margins.
Common margin standards and millimeter equivalents:
- MLA/APA Format: 1 inch (25.4mm) all sides
- Dissertation Format: 1.5 inches (38.1mm) all sides
- Business Letters: 1 inch sides, 2 inches (50.8mm) top/bottom
- European A4 Standard: 20mm all sides
- Narrow Margins: 0.5 inches (12.7mm) all sides
When implementing these standards, IronPDF supports custom paper sizes, allowing documents that meet specific regional or organizational requirements.
How Can I Apply Custom Margins to Headers and Footers?
By default, margins set in RenderingOptions don't apply to headers and footers. To apply document margins to headers and footers, configure the UseMarginsOnHeaderAndFooter property:
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.All;renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.AllOutput
The header and footer line up with the 25mm document margins instead of spanning the full page width.
For header and footer implementation with dynamic content and styling, see the guide on adding headers and footers.
How Do I Apply Margins Selectively to Headers and Footers?
Specify which margins apply to headers and footers. See the API Reference for detailed configuration. Examples:
// Use only the left margin from the document.
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.Left;
// Use only the left and right margins from the document.
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.LeftAndRight;' Use only the left margin from the document.
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.Left
' Use only the left and right margins from the document.
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.LeftAndRightOutput
With UseMargins.LeftAndRight, only the left and right document margins are applied to the header and footer.
When Should I Use Different Header/Footer Margin Configurations?
Selective margin configurations for headers and footers work well for documents with asymmetric layouts, such as books with alternating page margins or headers extending to page edges while body content maintains margins.
Common use cases:
- Book Publishing: Alternating inner/outer margins for binding
- Letterhead Documents: Full-width headers with constrained body
- Forms and Templates: Different margins for fields versus instructions
- Multi-column Layouts: Full-width headers with column margins
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.
Advanced Margin Considerations
Working with Page Breaks and Margins
Custom margins interact with page breaks. IronPDF handles content flow across pages while respecting margin settings, ensuring consistent formatting in multi-page documents.
Margin Units and Precision
IronPDF uses millimeters for margins. Convert from other units:
- Inches to mm: multiply by 25.4
- Points to mm: multiply by 0.352778
- Pixels to mm: varies by DPI (at 96 DPI, multiply by 0.264583)
IronPDF supports decimal values for sub-millimeter precision when needed.
Performance Optimization with Margins
For large documents with custom margins:
- Set margins once at
rendererlevel - Use consistent values across similar documents
- Combine margin settings with other rendering options
Integration with Other IronPDF Features
Custom margins work with other IronPDF capabilities:
- Watermarks and stamps respect boundaries
- PDF compression maintains integrity
- Digital signatures position relative to margins
Conclusion
Setting custom margins in IronPDF provides precise control over PDF layout and formatting. Whether creating business documents, academic papers, or custom reports, IronPDF's margin system ensures PDFs meet exact specifications. Explore the documentation to discover how IronPDF can streamline your PDF generation workflow.
Frequently Asked Questions
How do I set custom margins for PDFs in C#?
With IronPDF, you can set custom margins by configuring the ChromePdfRenderer's RenderingOptions. Simply set the MarginTop, MarginBottom, MarginLeft, and MarginRight properties in millimeters. For example: new ChromePdfRenderer { RenderingOptions = { MarginTop = 10, MarginBottom = 10, MarginLeft = 10, MarginRight = 10 } }.
What units are used for margin measurements?
IronPDF uses millimeters (mm) as the unit for all margin settings in the RenderingOptions. This provides precise control over PDF layout and ensures consistency across different document standards.
How do IronPDF margins interact with CSS margins in HTML?
IronPDF margins are additive to CSS margins defined in your HTML. If your HTML has 50mm margins and you set 30mm in RenderingOptions, the final PDF will have 80mm total margins. This allows flexible control over spacing while preserving your HTML styling.
Can I apply margins to headers and footers?
Yes, IronPDF provides the UseMarginsOnHeaderAndFooter property in RenderingOptions. Setting this to IronPdf.UseMargins.All applies your custom margins to headers and footers, ensuring consistent spacing throughout your PDF document.
What are the recommended margin values for academic formats?
For academic documents using IronPDF, use 25.4mm (1 inch) for MLA/APA format, 38.1mm (1.5 inches) for dissertations, or 20mm for European A4 standard. These values can be easily set through the MarginTop, MarginBottom, MarginLeft, and MarginRight properties.
Does the Chrome rendering engine affect margin accuracy?
IronPDF's Chrome rendering engine ensures highly accurate margin rendering that matches modern web standards. This provides consistent results when converting HTML to PDF, maintaining precise spacing as defined in your RenderingOptions.
Can I set different margins for each side of the PDF?
Absolutely. IronPDF allows independent control of all four margins through separate properties: MarginTop, MarginBottom, MarginLeft, and MarginRight. This enables asymmetric layouts like business letters with larger top/bottom margins.
How do I implement narrow margins for maximum content area?
For narrow margins in IronPDF, set all margin values to 12.7mm (0.5 inches) in the RenderingOptions. This maximizes the printable area while maintaining professional appearance: RenderingOptions = { MarginTop = 12.7, MarginBottom = 12.7, MarginLeft = 12.7, MarginRight = 12.7 }.
Why are my CSS @page margins ignored, and how do I make them take precedence?
By default IronPDF adds CSS @page margins to your RenderingOptions margins. To choose which wins, set RenderingOptions.CssPageRulePolicy: Default keeps the legacy additive behavior, RenderingOptionsWin strips @page margins and uses your C# margins, and CssPageWin applies the @page margins. It controls margins only, not page size or orientation, and under CssPageWin you should give any header or footer a fixed MaxHeight.

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.