Skip to footer content

How to set Custom Margins in PDFs C#

Unlock the full potential of your PDF documents by learning to set custom margins with IronPDF in C#.NET, and elevate your document generation skills to a professional level by watching this comprehensive tutorial!

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.

Related Videos