Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Further Reading: How to Set Custom Margins