Set Custom Margins
IronPDF allows you to edit margins to any value using RenderingOptions
. The HTML to PDF output options for ChromePdfRenderer
include specifications such as paper size, DPI, headers and footers, and other Chromium-specific browser setup options. You can also set custom margins with this feature.
This example shows how to set custom margins on your PDF document using IronPDF, a versatile tool for generating and editing PDFs in .NET applications. For more information about IronPDF and its capabilities, visit the IronPDF official website.
The margins can be measured and set in millimeters or inches. For example, the bottom PDF paper margin is in millimeters and can be set to zero to create a document for borderless and commercial printing applications.
However, the default value is 25
. For the desired effect, you can customize the top, bottom, left, and right borders individually.
using IronPdf;
using System;
class PdfMarginExample
{
static void Main()
{
// Initialize ChromePdfRenderer - a rendering engine for PDFs in IronPDF
var Renderer = new ChromePdfRenderer();
// Set up custom rendering options including margins
var pdfOptions = new PdfDocumentOptions
{
// Set the PDF paper size
PaperSize = PdfPaperSize.A4,
// Set custom margins in millimeters
// Each can be set individually. Here is an example of setting each one to 10 mm.
MarginTop = 10,
MarginBottom = 10,
MarginLeft = 10,
MarginRight = 10,
// Optionally, you can adjust headers, footers, DPI, etc.
// Additional settings could be provided here
};
// Assigning the options to the renderer
Renderer.RenderingOptions = pdfOptions;
// Render the HTML to PDF and save to a file
// Example HTML content to convert
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a sample PDF with custom margins.</p>";
// Convert HTML to PDF
var pdf = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("CustomMargins.pdf");
Console.WriteLine("PDF generated with custom margins successfully.");
}
}
using IronPdf;
using System;
class PdfMarginExample
{
static void Main()
{
// Initialize ChromePdfRenderer - a rendering engine for PDFs in IronPDF
var Renderer = new ChromePdfRenderer();
// Set up custom rendering options including margins
var pdfOptions = new PdfDocumentOptions
{
// Set the PDF paper size
PaperSize = PdfPaperSize.A4,
// Set custom margins in millimeters
// Each can be set individually. Here is an example of setting each one to 10 mm.
MarginTop = 10,
MarginBottom = 10,
MarginLeft = 10,
MarginRight = 10,
// Optionally, you can adjust headers, footers, DPI, etc.
// Additional settings could be provided here
};
// Assigning the options to the renderer
Renderer.RenderingOptions = pdfOptions;
// Render the HTML to PDF and save to a file
// Example HTML content to convert
var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a sample PDF with custom margins.</p>";
// Convert HTML to PDF
var pdf = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF to a file
pdf.SaveAs("CustomMargins.pdf");
Console.WriteLine("PDF generated with custom margins successfully.");
}
}
Imports IronPdf
Imports System
Friend Class PdfMarginExample
Shared Sub Main()
' Initialize ChromePdfRenderer - a rendering engine for PDFs in IronPDF
Dim Renderer = New ChromePdfRenderer()
' Set up custom rendering options including margins
Dim pdfOptions = New PdfDocumentOptions With {
.PaperSize = PdfPaperSize.A4,
.MarginTop = 10,
.MarginBottom = 10,
.MarginLeft = 10,
.MarginRight = 10
}
' Assigning the options to the renderer
Renderer.RenderingOptions = pdfOptions
' Render the HTML to PDF and save to a file
' Example HTML content to convert
Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a sample PDF with custom margins.</p>"
' Convert HTML to PDF
Dim pdf = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF to a file
pdf.SaveAs("CustomMargins.pdf")
Console.WriteLine("PDF generated with custom margins successfully.")
End Sub
End Class
Explanation
ChromePdfRenderer: A powerful renderer that uses Chromium to process and convert HTML into a PDF format. It provides detailed control over the resulting PDF's layout and appearance.
PdfDocumentOptions: Configures various aspects of the PDF generation process, including margin settings. Here, margins are set in millimeters.
MarginTop, MarginBottom, MarginLeft, MarginRight: These properties define the size of the margins around the content of the PDF. In the example, they are each set to 10 millimeters for uniform margins.
RenderHtmlAsPdf(): Converts HTML content to a PDF document.
- SaveAs(): Saves the generated PDF to a specified file path.
This example demonstrates the ease of customizing PDF document margins using IronPDF in C#.