IRONSOFTWAREHOME
Set Custom Margins
import {PdfDocument} from "@ironsoftware/ironpdf";

(async () => {
    const options = {
        margin: {
            top: 40,
            left: 20,
            right: 20,
            bottom: 40,
        },
    };
    // Render HTML file to PDF
    const pdf = await PdfDocument.fromHtml("my-content.html", { renderOptions: options });

    // Save the PDF
    await pdf.saveAs("my-content.pdf");
})();
npm i @ironsoftware/ironpdf
Set Custom Margins

Set Custom Margins

Customizing margins is a straightforward task with IronPDF.

Begin by setting up the rendering options and specifying the margins for the PDF content. In this example, we have defined a top margin of 40 units, a left margin of 20 units, a right margin of 20 units, and a bottom margin of 40 units. These margin values control the spacing between the content and the edges of each page within the PDF.

Utilize the HtmlToPdf method to convert an HTML file into a PDF document using IronPDF's extensive HTML-to-PDF capabilities. The rendering options, which include the customized margins, should be provided within the HtmlToPdf object.

Once the HTML content has been successfully rendered into a PDF document, proceed to export the resulting PDF, taking full advantage of IronPDF's powerful document conversion features.

Here's an example code snippet demonstrating how to set margins:

Explanation of the code:

  1. Import IronPDF: Ensure you have included the necessary using IronPdf directives to access IronPDF's features.
  2. Initialize HtmlToPdf: Create an object of HtmlToPdf to handle the HTML to PDF conversion.
  3. Configure Margins: Define a HtmlToPdf object setting the margins for the PDF pages. The margins are important for layout and help define where content will be placed relative to the edge of the paper.
  4. Set Print Options: Assign the HtmlToPdf (with margin specifications) to the renderer.PrintOptions.
  5. HTML Content: Define your HTML content in a string. This is what will be converted into the PDF format.
  6. Render HTML to PDF: Call RenderHtmlAsPdf on the renderer, passing in the HTML content. This method returns a PdfDocument object.
  7. Save PDF: Save the resulting PdfDocument to a file using SaveAs, providing the path where you want to store the PDF.

By following these steps, you can customize the PDF document margins and create well-formatted PDFs using IronPDF.

Explore IronPDF Custom Margin Code Example on GitHub
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
Node.js Module Download for PDF
Install with npm

Version: 2026.8

  1. Download and install Node.js 12+.
  2. Execute the above command in the terminal.

Licenses from $999