How to Set Custom Margins

by Jordi Bardia

When working with PDFs or any other document type, there is often the need to specify the margins, in order to adhere to differing standards. For instance, the MLA and APA formats both require 1-inch formats, while some universities may require 1.5-inch margins for dissertation papers.

IronPDF makes it easy to set custom margins when rendering a PDF from HTML—all it takes is some simple configuration.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Set Custom Margin Example

To set custom margins, first instantiate the ChromePdfRenderer class. With ChromePdfRenderer, you can access the RenderingOptions object, from which you can set the specific margins in millimeters for the top, bottom, left, and right, as shown below:

: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;
Dim renderer As New ChromePdfRenderer()

renderer.RenderingOptions.MarginTop = 40
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20
renderer.RenderingOptions.MarginBottom = 40
VB   C#

Note this adds to the margins that are set in the style section of the HTML. For instance, in the example below, the margins are initially set as 50 mm in the html, but setting the margins for each side in RenderingOptions adds another 30 mm to the margins, making them 80 mm:

: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");
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")
VB   C#

The resulting PDF is shown below:

By default, the margins set in RenderingOptions do not apply to headers and footers in the document. To set the same custom margins of the document in the headers and footers, configure the UseMarginsOnHeaderAndFooter property in RenderingOptions:

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

It is possible to specify which margins to set in the header and footer. For a full list of enums for setting margins in headers and footers, take a look at our comprehensive API Reference. Some examples of specifying which margins to set are shown below:

: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;
' 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
VB   C#
Jordi related to Set Custom Margin in Header/Footer

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 says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.