IronPDF How-Tos Set Custom Margins How to Set Custom Margins Jordi Bardia Updated:July 28, 2025 When working with PDFs or any other document type, there is often the need to specify the margins 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 making PDFs with NuGet now: Install IronPDF with NuGet PM > Install-Package IronPdf Copy the code new IronPdf.ChromePdfRenderer { RenderingOptions = { MarginTop = 10, MarginBottom = 10, MarginLeft = 10, MarginRight = 10, UseMarginsOnHeaderAndFooter = true } } .RenderHtmlAsPdf("<h1>Hello with margins!</h1>") .SaveAs("custom‑margins.pdf"); Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial Get started with IronPDF Start using IronPDF in your project today with a free trial. First Step: Start for Free How to Set Custom Margins Download the IronPDF C# library for margin configuration from NuGet Instantiate the ChromePdfRenderer class to render a PDF file Modify the margin values in Chrome Renderer's RenderingOptions for customization Adjust margins specifically for headers and footers Render the HTML to PDF and save the document 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 $vbLabelText $csharpLabel 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") $vbLabelText $csharpLabel 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 $vbLabelText $csharpLabel It is possible to specify which margins to set in the header and footer. For more detailed configuration, refer to 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 $vbLabelText $csharpLabel Frequently Asked Questions How can I set custom margins in a PDF document using C#? You can set custom margins in a PDF document using the IronPDF library in C#. Instantiate the ChromePdfRenderer class and use the RenderingOptions to specify margins in millimeters for the top, bottom, left, and right sides. What steps are necessary to set custom margins in IronPDF? To set custom margins in IronPDF, first download the IronPDF library from NuGet, instantiate the ChromePdfRenderer, modify the margin values in the RenderingOptions, and then render and save the PDF. Can I apply different margins to the header and footer of a PDF? Yes, you can apply different margins to the header and footer of a PDF by setting the UseMarginsOnHeaderAndFooter property in IronPDF's RenderingOptions. How do CSS margins affect the margins set in IronPDF? CSS margins specified in the HTML add to the margins set in IronPDF's RenderingOptions. For example, if the HTML has a 50 mm margin and IronPDF adds another 30 mm, the total margin will be 80 mm. Is there a way to learn how to set margins using a video? Yes, there is a video tutorial available on YouTube that provides guidance on setting custom margins using IronPDF. Can margins be specified in units other than millimeters in IronPDF? No, IronPDF primarily uses millimeters for specifying margins. Other units are not directly supported in the API. What is the default margin behavior for headers and footers in IronPDF? By default, margins specified in IronPDF's RenderingOptions do not apply to headers and footers. To apply the same margins, enable UseMarginsOnHeaderAndFooter. Where can I find additional examples for setting custom margins in IronPDF? For additional examples on setting custom margins in IronPDF, refer to the comprehensive API Reference and code examples provided in the article. Jordi Bardia Chat with engineering team now 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? Free NuGet Download Total downloads: 15,030,178 View Licenses