IRONSOFTWAREHOME
Set Custom Margins

import com.ironsoftware.ironpdf.PdfDocument;  
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;  
import java.io.IOException;  
import java.nio.file.Paths;

// Set Margins (in millimeters)  
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
renderOptions.setMarginTop(40);  
renderOptions.setMarginLeft(20);  
renderOptions.setMarginRight(20);  
renderOptions.setMarginBottom(40);  
  
PdfDocument.renderHtmlFileAsPdf("assets/wikipedia.html", renderOptions).saveAs(Paths.get("assets/MyContent.pdf"));
mvn install
Set Custom Margins

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.

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".
Explore ChromePdfRenderOptions API for Custom PDF Margins
View on GitHub

Ready to Get Started?

Version:2026.8just released

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
Java Maven Library for PDF
Install with Maven

Version: 2026.8

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>ironpdf</artifactId>
   <version>2026.8.2</version>
</dependency>
https://central.sonatype.com/artifact/com.ironsoftware/ironpdf/2026.8.2
or
Java PDF JAR
Download JAR

Version: 2026.8

Manually install into your project