Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer


How to Set Custom Margins

  1. Download the IronPDF C# library for margin configuration from NuGet
  2. Instantiate the ChromePdfRenderer class to render a PDF file
  3. Modify the margin values in Chrome Renderer's RenderingOptions for customization
  4. Adjust margins specifically for headers and footers
  5. Render the HTML to PDF and save the document

Set Custom Margin Example

To set custom margins, first, instantiate the ChromePdfRenderer class. With ChromePdfRenderer, you can access the RenderingOptions object, from which you can set the specific margins in millimeters for the top, bottom, left, and right, as shown below:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-set-margins.cs
// This code snippet demonstrates how to configure the margin options
// for a PDF rendering operation using a ChromePdfRenderer.

// Ensure the appropriate namespace and library references are included 
// for ChromePdfRenderer to be accessible. You might need to add a 
// directive for that specific library.

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Ensure the renderer is correctly instantiated before accessing its properties.
// The following lines set the margin dimensions for the PDF rendering.

renderer.RenderingOptions.MarginTop = 40;    // Sets the top margin to 40 units.
renderer.RenderingOptions.MarginLeft = 20;   // Sets the left margin to 20 units.
renderer.RenderingOptions.MarginRight = 20;  // Sets the right margin to 20 units.
renderer.RenderingOptions.MarginBottom = 40; // Sets the bottom margin to 40 units.

// Ensure that the renderer is used within the appropriate context, such as:
// rendering a PDF from HTML content, a document, etc.
// Example: renderer.RenderHtmlAsPdf("...html content...").SaveAs("output.pdf");
' This code snippet demonstrates how to configure the margin options

' for a PDF rendering operation using a ChromePdfRenderer.



' Ensure the appropriate namespace and library references are included 

' for ChromePdfRenderer to be accessible. You might need to add a 

' directive for that specific library.



Dim renderer As New ChromePdfRenderer()



' Ensure the renderer is correctly instantiated before accessing its properties.

' The following lines set the margin dimensions for the PDF rendering.



renderer.RenderingOptions.MarginTop = 40 ' Sets the top margin to 40 units.

renderer.RenderingOptions.MarginLeft = 20 ' Sets the left margin to 20 units.

renderer.RenderingOptions.MarginRight = 20 ' Sets the right margin to 20 units.

renderer.RenderingOptions.MarginBottom = 40 ' Sets the bottom margin to 40 units.



' Ensure that the renderer is used within the appropriate context, such as:

' rendering a PDF from HTML content, a document, etc.

' Example: renderer.RenderHtmlAsPdf("...html content...").SaveAs("output.pdf");
$vbLabelText   $csharpLabel

Note this adds to the margins that are set in the style section of the HTML. For instance, in the example below, the margins are initially set as 50 mm in the HTML, but setting the margins for each side in RenderingOptions adds another 30 mm to the margins, making them 80 mm:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-set-margins-with-css.cs
// This string holds the HTML content with embedded CSS style 
// that defines a margin of 50mm on all sides of the page.
const string htmlWithStyle = @"
<!DOCTYPE html>
<html>
    <head>
        <style>
            body { margin: 50mm; } /* Sets a margin of 50mm for all sides */
        </style>
    </head>
<body>
    <h1>Hello World!</h1>
</body>
</html>";

// Initialize a ChromePdfRenderer to create a PDF from HTML.
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Configure the renderer's options to set custom margins for the PDF.
// These margins are in pixels.
renderer.RenderingOptions.MarginTop = 30;    // Set the top margin to 30 pixels
renderer.RenderingOptions.MarginLeft = 30;   // Set the left margin to 30 pixels
renderer.RenderingOptions.MarginRight = 30;  // Set the right margin to 30 pixels
renderer.RenderingOptions.MarginBottom = 30; // Set the bottom margin to 30 pixels

// Render the HTML content to a PDF document using the defined margins.
PdfDocument pdf = renderer.RenderHtmlAsPdf(htmlWithStyle);

// Save the generated PDF document to a file named "PdfWithCustomMargins.pdf".
pdf.SaveAs("PdfWithCustomMargins.pdf");
' This string holds the HTML content with embedded CSS style 

' that defines a margin of 50mm on all sides of the page.

Const htmlWithStyle As String = "

<!DOCTYPE html>

<html>

    <head>

        <style>

            body { margin: 50mm; } /* Sets a margin of 50mm for all sides */

        </style>

    </head>

<body>

    <h1>Hello World!</h1>

</body>

</html>"



' Initialize a ChromePdfRenderer to create a PDF from HTML.

Dim renderer As New ChromePdfRenderer()



' Configure the renderer's options to set custom margins for the PDF.

' These margins are in pixels.

renderer.RenderingOptions.MarginTop = 30 ' Set the top margin to 30 pixels

renderer.RenderingOptions.MarginLeft = 30 ' Set the left margin to 30 pixels

renderer.RenderingOptions.MarginRight = 30 ' Set the right margin to 30 pixels

renderer.RenderingOptions.MarginBottom = 30 ' Set the bottom margin to 30 pixels



' Render the HTML content to a PDF document using the defined margins.

Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(htmlWithStyle)



' Save the generated PDF document to a file named "PdfWithCustomMargins.pdf".

pdf.SaveAs("PdfWithCustomMargins.pdf")
$vbLabelText   $csharpLabel

The resulting PDF is shown below:

By default, the margins set in RenderingOptions do not apply to headers and footers in the document. To set the same custom margins of the document in the headers and footers, configure the UseMarginsOnHeaderAndFooter property in RenderingOptions:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-use-margins-header-footer.cs
// Assuming 'renderer' is an instance of a document rendering class.
// This line sets the rendering options related to the use of margins in headers and footers.

renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.All;

// Explanation:
// - 'renderer' is an instance responsible for managing the rendering of documents.
//    Make sure that this instance is appropriately initialized in your program before executing this code.
// - 'RenderingOptions' is presumably either a property or a field contained within the 'renderer' object. 
//    It likely holds various configurations influencing how documents are rendered.
// - 'UseMarginsOnHeaderAndFooter' is a setting within 'RenderingOptions' that dictates how margins should be applied to the headers and footers.
// - 'UseMargins.All' appears to be an enumerated type value indicating that all margins should be used for rendering headers and footers.

// Note:
// Ensure that the 'renderer' object is defined and properly instantiated within your application context.
// Verify that 'UseMargins' is a valid enumeration available in the scope of execution.
// This code snippet assumes such instances and contexts are already established prior to this line running.
' Assuming 'renderer' is an instance of a document rendering class.

' This line sets the rendering options related to the use of margins in headers and footers.



renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = UseMargins.All



' Explanation:

' - 'renderer' is an instance responsible for managing the rendering of documents.

'    Make sure that this instance is appropriately initialized in your program before executing this code.

' - 'RenderingOptions' is presumably either a property or a field contained within the 'renderer' object. 

'    It likely holds various configurations influencing how documents are rendered.

' - 'UseMarginsOnHeaderAndFooter' is a setting within 'RenderingOptions' that dictates how margins should be applied to the headers and footers.

' - 'UseMargins.All' appears to be an enumerated type value indicating that all margins should be used for rendering headers and footers.



' Note:

' Ensure that the 'renderer' object is defined and properly instantiated within your application context.

' Verify that 'UseMargins' is a valid enumeration available in the scope of execution.

' This code snippet assumes such instances and contexts are already established prior to this line running.
$vbLabelText   $csharpLabel

It is possible to specify which margins to set in the header and footer. For more detailed configuration, refer to our comprehensive API Reference. Some examples of specifying which margins to set are shown below:

:path=/static-assets/pdf/content-code-examples/how-to/custom-margins-use-specific-margins-header-footer.cs
// The following code snippets configure the rendering options for headers and footers 
// in a given document. 
// Specifically, the options control which margins (if any) are applied when rendering headers and footers.

namespace DocumentRendering
{
    // An enum representing the margin application options for headers and footers.
    public enum UseMargins
    {
        None,           // No margins are applied
        Left,           // Only the left margin is applied
        Right,          // Only the right margin is applied
        Top,            // Only the top margin is applied
        Bottom,         // Only the bottom margin is applied
        LeftAndRight,   // Both left and right margins are applied
        TopAndBottom,   // Both top and bottom margins are applied
        All             // All margins are applied
    }

    // Represents the rendering options, including how margins are applied to headers and footers.
    public class RenderingOptions
    {
        // Property to set which margins should be used on headers and footers.
        public UseMargins UseMarginsOnHeaderAndFooter { get; set; }
    }

    // Represents a document renderer that utilizes specific rendering options.
    public class Renderer
    {
        // Instance of rendering options associated with the renderer.
        public RenderingOptions RenderingOptions { get; private set; }

        // Constructor to initialize rendering options.
        public Renderer()
        {
            RenderingOptions = new RenderingOptions();
        }
    }
}

// Initialize a new Renderer instance
DocumentRendering.Renderer renderer = new DocumentRendering.Renderer();

// Configure the renderer to use only the left margin for headers and footers.
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = DocumentRendering.UseMargins.Left;

// Configure the renderer to use both the left and right margins for headers and footers.
// This setting will override the previous setting.
renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = DocumentRendering.UseMargins.LeftAndRight;
' The following code snippets configure the rendering options for headers and footers 

' in a given document. 

' Specifically, the options control which margins (if any) are applied when rendering headers and footers.



Namespace DocumentRendering

	' An enum representing the margin application options for headers and footers.

	Public Enum UseMargins

		None ' No margins are applied

		Left ' Only the left margin is applied

		Right ' Only the right margin is applied

		Top ' Only the top margin is applied

		Bottom ' Only the bottom margin is applied

		LeftAndRight ' Both left and right margins are applied

		TopAndBottom ' Both top and bottom margins are applied

		All ' All margins are applied

	End Enum



	' Represents the rendering options, including how margins are applied to headers and footers.

	Public Class RenderingOptions

		' Property to set which margins should be used on headers and footers.

		Public Property UseMarginsOnHeaderAndFooter() As UseMargins

	End Class



	' Represents a document renderer that utilizes specific rendering options.

	Public Class Renderer

		' Instance of rendering options associated with the renderer.

		Private privateRenderingOptions As RenderingOptions

		Public Property RenderingOptions() As RenderingOptions

			Get

				Return privateRenderingOptions

			End Get

			Private Set(ByVal value As RenderingOptions)

				privateRenderingOptions = value

			End Set

		End Property



		' Constructor to initialize rendering options.

		Public Sub New()

			RenderingOptions = New RenderingOptions()

		End Sub

	End Class

End Namespace



' Initialize a new Renderer instance

Private renderer As New DocumentRendering.Renderer()



' Configure the renderer to use only the left margin for headers and footers.

renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = DocumentRendering.UseMargins.Left



' Configure the renderer to use both the left and right margins for headers and footers.

' This setting will override the previous setting.

renderer.RenderingOptions.UseMarginsOnHeaderAndFooter = DocumentRendering.UseMargins.LeftAndRight
$vbLabelText   $csharpLabel

Frequently Asked Questions

How can I set custom margins in a PDF?

To set custom margins with IronPDF, instantiate the ChromePdfRenderer class, access the RenderingOptions object, and specify the margins in millimeters for each side (top, bottom, left, right).

What are RenderingOptions?

RenderingOptions is an object in IronPDF that allows you to customize various settings for rendering PDFs, such as setting custom margins, enabling margins on headers and footers, and more.

Can I set different margins for headers and footers?

Yes, you can set different margins for headers and footers by enabling the UseMarginsOnHeaderAndFooter property in IronPDF's RenderingOptions. This allows you to specify distinct margins for the header and footer.

How do CSS margins interact with RenderingOptions?

CSS margins set within HTML will add to the margins specified in IronPDF's RenderingOptions. For example, if HTML specifies a 50 mm margin and RenderingOptions adds another 30 mm, the total margin will be 80 mm.

Where can I download the C# library?

You can download the IronPDF C# library for margin configuration from NuGet at https://nuget.org/packages/IronPdf/.

Is there a video tutorial available for setting custom margins?

Yes, a video tutorial is available on YouTube to help you learn how to set custom margins in IronPDF.

Can I specify margins in units other than millimeters?

IronPDF's RenderingOptions primarily uses millimeters for specifying margins. Other units are not directly supported in the API.

What is the default behavior of margins in headers and footers?

By default, the margins set in IronPDF's RenderingOptions do not apply to headers and footers. You need to enable UseMarginsOnHeaderAndFooter to apply the same margins to these sections.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.