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 PdfDocument.fromHtml
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 renderOptions
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:
// Import the necessary IronPDF libraries
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Create an instance of the HtmlToPdf class
HtmlToPdf renderer = new HtmlToPdf();
// Set up the rendering options with specified margins
var renderOptions = new PdfPrintOptions()
{
MarginTop = 40, // Top margin in units
MarginLeft = 20, // Left margin in units
MarginRight = 20, // Right margin in units
MarginBottom = 40 // Bottom margin in units
};
// Assign the custom rendering options to the renderer
renderer.PrintOptions = renderOptions;
// Define the HTML content to be converted
string htmlContent = "<html><body><h1>Hello, IronPDF!</h1></body></html>";
// Convert the HTML content to a PDF document
PdfDocument pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Export the PDF document to a file
pdfDocument.SaveAs("output.pdf");
}
}
// Import the necessary IronPDF libraries
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Create an instance of the HtmlToPdf class
HtmlToPdf renderer = new HtmlToPdf();
// Set up the rendering options with specified margins
var renderOptions = new PdfPrintOptions()
{
MarginTop = 40, // Top margin in units
MarginLeft = 20, // Left margin in units
MarginRight = 20, // Right margin in units
MarginBottom = 40 // Bottom margin in units
};
// Assign the custom rendering options to the renderer
renderer.PrintOptions = renderOptions;
// Define the HTML content to be converted
string htmlContent = "<html><body><h1>Hello, IronPDF!</h1></body></html>";
// Convert the HTML content to a PDF document
PdfDocument pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Export the PDF document to a file
pdfDocument.SaveAs("output.pdf");
}
}
' Import the necessary IronPDF libraries
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create an instance of the HtmlToPdf class
Dim renderer As New HtmlToPdf()
' Set up the rendering options with specified margins
Dim renderOptions = New PdfPrintOptions() With {
.MarginTop = 40,
.MarginLeft = 20,
.MarginRight = 20,
.MarginBottom = 40
}
' Assign the custom rendering options to the renderer
renderer.PrintOptions = renderOptions
' Define the HTML content to be converted
Dim htmlContent As String = "<html><body><h1>Hello, IronPDF!</h1></body></html>"
' Convert the HTML content to a PDF document
Dim pdfDocument As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Export the PDF document to a file
pdfDocument.SaveAs("output.pdf")
End Sub
End Class
Explanation of the code:
Import IronPDF: Ensure you have included the necessary using directives to access IronPDF's features.
Initialize HtmlToPdf: Create an object of
HtmlToPdf
to handle the HTML to PDF conversion.Configure Margins: Define a
PdfPrintOptions
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.Set Print Options: Assign the
PdfPrintOptions
(with margin specifications) to therenderer.PrintOptions
.HTML Content: Define your HTML content in a string. This is what will be converted into the PDF format.
Render HTML to PDF: Call
RenderHtmlAsPdf
on the renderer, passing in the HTML content. This method returns aPdfDocument
object.- Save PDF: Save the resulting
PdfDocument
to a file usingSaveAs
, 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.