How to set Custom Margins in PDFs C#

In this tutorial, you will learn how to set custom margins in PDF documents using the IronPDF library in a C#.NET application. Begin by ensuring that the IronPDF NuGet package is installed in your project. Open your Program.cs file, import the IronPDF namespace, and set your license key. Prepare an HTML string with inline CSS to establish initial margins, such as setting all margins to 50 mm. Instantiate the Chrome PDF Renderer, and adjust the rendering options to set custom margins for each side—top, left, right, and bottom—to 30 mm. This adds to the initial margins, resulting in a total of 80 mm per side. Render the HTML to a PDF using the RenderHtmlAsPdf method and save the document with the SaveAs method. To customize margins for headers and footers, use the UseMarginsOnHeaderAndFooter property in rendering options. Run the application to generate a PDF with combined margin settings. Explore more advanced functionalities of IronPDF for versatile document generation. Sign up for a trial to experience the software's capabilities firsthand, and subscribe for more tutorials from Iron Software.

using System;
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Initialize IronPDF license key
        var license = new IronPdf.LicenseKey("YOUR_LICENSE_KEY_HERE");

        // HTML content with initial margins set via inline CSS
        string htmlContent = @"
            <html>
            <head>
                <style>
                    body { margin: 50mm; }
                </style>
            </head>
            <body>
                <h1>Hello, IronPDF!</h1>
                <p>This is a sample PDF document with custom margins.</p>
            </body>
            </html>";

        // Instantiate the ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Customize the PDF rendering options including margins
        var renderOptions = new PdfDocumentRenderOptions()
        {
            MarginTop = 30,   // Additional margin of 30 mm on top
            MarginBottom = 30, // Additional margin of 30 mm on bottom
            MarginLeft = 30,  // Additional margin of 30 mm on left
            MarginRight = 30,  // Additional margin of 30 mm on right
            // Apply custom margins for headers and footers
            UseMarginsOnHeaderAndFooter = true
        };

        renderer.RenderingOptions = renderOptions;

        // Render the HTML content as a PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the PDF document to a file
        pdf.SaveAs("CustomMarginsPDF.pdf");

        Console.WriteLine("PDF document created successfully with custom margins.");
    }
}
using System;
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        // Initialize IronPDF license key
        var license = new IronPdf.LicenseKey("YOUR_LICENSE_KEY_HERE");

        // HTML content with initial margins set via inline CSS
        string htmlContent = @"
            <html>
            <head>
                <style>
                    body { margin: 50mm; }
                </style>
            </head>
            <body>
                <h1>Hello, IronPDF!</h1>
                <p>This is a sample PDF document with custom margins.</p>
            </body>
            </html>";

        // Instantiate the ChromePdfRenderer
        var renderer = new ChromePdfRenderer();

        // Customize the PDF rendering options including margins
        var renderOptions = new PdfDocumentRenderOptions()
        {
            MarginTop = 30,   // Additional margin of 30 mm on top
            MarginBottom = 30, // Additional margin of 30 mm on bottom
            MarginLeft = 30,  // Additional margin of 30 mm on left
            MarginRight = 30,  // Additional margin of 30 mm on right
            // Apply custom margins for headers and footers
            UseMarginsOnHeaderAndFooter = true
        };

        renderer.RenderingOptions = renderOptions;

        // Render the HTML content as a PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the PDF document to a file
        pdf.SaveAs("CustomMarginsPDF.pdf");

        Console.WriteLine("PDF document created successfully with custom margins.");
    }
}
Imports System
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Initialize IronPDF license key
		Dim license = New IronPdf.LicenseKey("YOUR_LICENSE_KEY_HERE")

		' HTML content with initial margins set via inline CSS
		Dim htmlContent As String = "
            <html>
            <head>
                <style>
                    body { margin: 50mm; }
                </style>
            </head>
            <body>
                <h1>Hello, IronPDF!</h1>
                <p>This is a sample PDF document with custom margins.</p>
            </body>
            </html>"

		' Instantiate the ChromePdfRenderer
		Dim renderer = New ChromePdfRenderer()

		' Customize the PDF rendering options including margins
		Dim renderOptions = New PdfDocumentRenderOptions() With {
			.MarginTop = 30,
			.MarginBottom = 30,
			.MarginLeft = 30,
			.MarginRight = 30,
			.UseMarginsOnHeaderAndFooter = True
		}

		renderer.RenderingOptions = renderOptions

		' Render the HTML content as a PDF
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

		' Save the PDF document to a file
		pdf.SaveAs("CustomMarginsPDF.pdf")

		Console.WriteLine("PDF document created successfully with custom margins.")
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Set Custom Margins

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Apply Custom PDF Watermarks
NEXT >
How to use Custom Logging in C#