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. 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.