Set Custom Margins

IronPDF creates new PDF documents with 25-millimeter margins (top, bottom, left, and right) by default. However, IronPDF allows developers to set custom measurements for each margin as needed to achieve specific design criteria.

Developers can define custom margins for new PDFs using ChromePdfRenderOptions. The featured code example sets custom values for all four margins: 20-millimeter left and right margins and 40-millimeter top and bottom margins.

Specify PDF margins suitable for any document type: letters, posters, postcards, etc.--or create borderless documents suitable for commercial printing by setting each margin to zero millimeters. Use ChromePdfRenderOptions objects with PDF rendering methods (renderUrlAsPdf, renderHtmlAsPdf, and renderHtmlFileAsPdf) to define additional print-specific options apart from margin sizes, such as paper size, DPI, headers and footers, and others. View more information about available customization options in the ChromePdfRenderOptions API Reference.

using IronPdf;
// The ChromePdfRenderOptions class allows for detailed customization of PDF rendering
var renderOptions = new ChromePdfRenderOptions
{
    // Set the top margin to 40mm
    MarginTop = 40,

    // Set the bottom margin to 40mm
    MarginBottom = 40,

    // Set the left margin to 20mm
    MarginLeft = 20,

    // Set the right margin to 20mm
    MarginRight = 20
};

// Create a new instance of IronPdf's ChromePdfRenderer class
var renderer = new ChromePdfRenderer();

// Apply the custom margin settings to the renderer's render options
renderer.RenderOptions = renderOptions;

// Render a PDF from an HTML string (example)
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, PDF!</h1><p>This is a PDF document with custom margins.</p>");

// Save the rendered PDF document to a file
pdf.SaveAs("output.pdf");
using IronPdf;
// The ChromePdfRenderOptions class allows for detailed customization of PDF rendering
var renderOptions = new ChromePdfRenderOptions
{
    // Set the top margin to 40mm
    MarginTop = 40,

    // Set the bottom margin to 40mm
    MarginBottom = 40,

    // Set the left margin to 20mm
    MarginLeft = 20,

    // Set the right margin to 20mm
    MarginRight = 20
};

// Create a new instance of IronPdf's ChromePdfRenderer class
var renderer = new ChromePdfRenderer();

// Apply the custom margin settings to the renderer's render options
renderer.RenderOptions = renderOptions;

// Render a PDF from an HTML string (example)
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, PDF!</h1><p>This is a PDF document with custom margins.</p>");

// Save the rendered PDF document to a file
pdf.SaveAs("output.pdf");
Imports IronPdf
' The ChromePdfRenderOptions class allows for detailed customization of PDF rendering
Private renderOptions = New ChromePdfRenderOptions With {
	.MarginTop = 40,
	.MarginBottom = 40,
	.MarginLeft = 20,
	.MarginRight = 20
}

' Create a new instance of IronPdf's ChromePdfRenderer class
Private renderer = New ChromePdfRenderer()

' Apply the custom margin settings to the renderer's render options
renderer.RenderOptions = renderOptions

' Render a PDF from an HTML string (example)
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello, PDF!</h1><p>This is a PDF document with custom margins.</p>")

' Save the rendered PDF document to a file
pdf.SaveAs("output.pdf")
$vbLabelText   $csharpLabel

In this code:

  • We instantiate ChromePdfRenderOptions to set custom margins for the PDF. The margins are set in millimeters.
  • We use an instance of ChromePdfRenderer, a class provided by IronPDF, to apply the rendering options.
  • The RenderHtmlAsPdf method is called with a simple HTML string to demonstrate rendering a PDF with the specified margins.
  • Finally, the rendered PDF is saved to a file named "output.pdf".