How to set Custom Margins in PDFs C#

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.

Quickstart: Implement Custom PDF Margins with IronPDF

Set custom margins when rendering PDFs from HTML with simple configuration.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    new IronPdf.ChromePdfRenderer { RenderingOptions = { MarginTop = 10, MarginBottom = 10, MarginLeft = 10, MarginRight = 10, UseMarginsOnHeaderAndFooter = true } }
        .RenderHtmlAsPdf("<h1>Hello with margins!</h1>")
        .SaveAs("custom-margins.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

Minimal Workflow (5 Steps):

  1. Download IronPDF from NuGet
  2. Instantiate the ChromePdfRenderer class
  3. Set margin values in RenderingOptions
  4. Configure header and footer margin behavior
  5. 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:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-set-margins.cs
ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
renderer.RenderingOptions.MarginBottom = 40;
$vbLabelText   $csharpLabel

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:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-set-margins-with-css.cs
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");
$vbLabelText   $csharpLabel

The resulting PDF is shown below:

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.

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:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-use-margins-header-footer.cs
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.All;
$vbLabelText   $csharpLabel

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:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-use-specific-margins-header-footer.cs
// 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;
$vbLabelText   $csharpLabel

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

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:

  1. Set margins once at renderer level
  2. Use consistent values across similar documents
  3. Combine margin settings with other rendering options

Integration with Other IronPDF Features

Custom margins work with other IronPDF capabilities:

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 true 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 }.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More
Ready to Get Started?
Nuget Downloads 17,012,929 | Version: 2025.12 just released