How to Set Custom Margins
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.
How to Set Custom Margins
- Download the C# library to set custom margins
- Instantiate the ChromePdfRenderer class to render PDF file
- Modify the margin values in Chrome Renderer's RenderingOptions
- Set custom margins in headers and footers
- Render the HTML to PDF and save the document
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLSet 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
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")
The resulting PDF is shown below:
Set Custom Margin in Header/Footer
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
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 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