How to Render PDFs with Custom Paper Size
A custom paper size refers to a non-standard paper size that is defined by the user rather than being a standard size like A4 or letter size (8.5 x 11 inches). Custom paper sizes are often used when printing documents that require a unique or specific layout, such as posters, banners, or specialty documents.
Discover the extensive range of paper sizes available with IronPDF, offering a wide selection to suit your needs!
How to Render PDFs with Custom Paper Size
- Download IronPDF from NuGet for setting custom paper sizes in PDFs
- Instantiate the ChromePdfRenderer class in C#
- Access the RenderingOptions on the new object
- Invoke one of the
SetCustomPaperSize
methods based on the measurement unit - Render and export the PDF document
Start using IronPDF in your project today with a free trial.
Use Standard Paper Size Example
First, create an instance of the ChromePdfRenderer
class. Then, use the RenderingOptions
property of the newly created object to modify the PaperSize
. Set it to one of the predefined values from the PdfPaperSize
enum to specify the desired paper size. We offer over 100 predefined standard paper sizes for your convenience.
Code
Below is an example of how to set a standard paper size:
:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-standard-paper-size.cs
using IronPdf;
using IronPdf.Rendering;
// Instantiate a ChromePdfRenderer to create a PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Configure the renderer with the desired options
// Set the paper size to A4 for the PDF document
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
// Render simple HTML content to a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Standard Paper Size</h1>");
// Save the rendered PDF document to the specified file path
pdf.SaveAs("standardPaperSize.pdf");
Imports IronPdf
Imports IronPdf.Rendering
' Instantiate a ChromePdfRenderer to create a PDF
Private renderer As New ChromePdfRenderer()
' Configure the renderer with the desired options
' Set the paper size to A4 for the PDF document
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
' Render simple HTML content to a PDF document
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Standard Paper Size</h1>")
' Save the rendered PDF document to the specified file path
pdf.SaveAs("standardPaperSize.pdf")
Related Properties
PaperSize
: Set an output paper size for PDF pages with predefined sizes such as letter, A3, A4, etc.ForcePaperSize
: Forces page sizes to be exactly what is specified via IronPdf.ChromePdfRenderOptions.PaperSize by resizing the page after generating a PDF from HTML. This feature is useful for bypassing CSS rules that specify paper size.
Use Custom Paper Size Example
First, we begin by instantiating the ChromePdfRenderer
class. From the newly created object, we can access the RenderingOptions to apply a custom paper size to the newly generated PDF document. There are four methods that can be used to set the output paper size for PDF pages, each based on a different measurement unit:
SetCustomPaperSizeInCentimeters
: Dimensions are incentimeters
.SetCustomPaperSizeInInches
: Dimensions are ininches
.SetCustomPaperSizeInMillimeters
: Dimensions are inmillimeters
.SetCustomPaperSizeInPixelsOrPoints
: Dimensions are inpixels or points
.
Code
Below is an example of how to set a custom paper size in centimeters:
:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-cm.cs
using IronPdf;
// Create a new instance of the ChromePdfRenderer, which is used to render HTML to PDF.
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Set a custom paper size for the PDF using centimeters.
// The first parameter is the width (15 cm) and the second is the height (15 cm) of the page.
renderer.RenderingOptions.SetCustomPaperSizeInCentimeters(15, 15);
// Render an HTML string as a PDF document.
// Here, we're rendering a simple HTML containing a heading.
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Custom Paper Size</h1>");
// Save the rendered PDF document to a file named "customPaperSize.pdf".
pdf.SaveAs("customPaperSize.pdf");
Imports IronPdf
' Create a new instance of the ChromePdfRenderer, which is used to render HTML to PDF.
Private renderer As New ChromePdfRenderer()
' Set a custom paper size for the PDF using centimeters.
' The first parameter is the width (15 cm) and the second is the height (15 cm) of the page.
renderer.RenderingOptions.SetCustomPaperSizeInCentimeters(15, 15)
' Render an HTML string as a PDF document.
' Here, we're rendering a simple HTML containing a heading.
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Custom Paper Size</h1>")
' Save the rendered PDF document to a file named "customPaperSize.pdf".
pdf.SaveAs("customPaperSize.pdf")
Output PDF
Modify Paper Dimension Example
In an existing PDF document or a freshly rendered PDF, the size of each page can be modified using the ExtendPage
method. This method allows you to specify the target page index, the values to modify each of the four sides, and the units of measurement. The values for each side can be negative, which will reduce that particular side, or positive, which will extend that side.
Code
Below is an example of how to modify paper dimensions:
:path=/static-assets/pdf/content-code-examples/how-to/custom-paper-size-modify-paper-size.cs
using IronPdf;
using IronPdf.Editing;
// Load the PDF document from a file specified by its name.
// This will open "customPaperSize.pdf" for editing purposes.
PdfDocument pdf = PdfDocument.FromFile("customPaperSize.pdf");
// Extend the first page of the PDF by increasing the left margin by 50 millimeters.
// ExtendPage parameters: pageIndex (page to modify), left (margin to add to left side),
// right (margin to add to right side), top (margin to add to top side), bottom (margin to add to bottom side),
// unit (measurement unit for the extension, in this case, millimeters).
pdf.ExtendPage(pageIndex: 0, left: 50, right: 0, top: 0, bottom: 0, unit: MeasurementUnit.Millimeter);
// Save the modified PDF document under a new file name, ensuring the original file remains unchanged.
// The modified PDF will be saved as "extendedLeftSide.pdf".
pdf.SaveAs("extendedLeftSide.pdf");
Imports IronPdf
Imports IronPdf.Editing
' Load the PDF document from a file specified by its name.
' This will open "customPaperSize.pdf" for editing purposes.
Private pdf As PdfDocument = PdfDocument.FromFile("customPaperSize.pdf")
' Extend the first page of the PDF by increasing the left margin by 50 millimeters.
' ExtendPage parameters: pageIndex (page to modify), left (margin to add to left side),
' right (margin to add to right side), top (margin to add to top side), bottom (margin to add to bottom side),
' unit (measurement unit for the extension, in this case, millimeters).
pdf.ExtendPage(pageIndex:= 0, left:= 50, right:= 0, top:= 0, bottom:= 0, unit:= MeasurementUnit.Millimeter)
' Save the modified PDF document under a new file name, ensuring the original file remains unchanged.
' The modified PDF will be saved as "extendedLeftSide.pdf".
pdf.SaveAs("extendedLeftSide.pdf")
Output PDF
Frequently Asked Questions
What is a custom paper size in PDF rendering?
A custom paper size refers to a non-standard paper size defined by the user, which deviates from standard sizes like A4 or letter size. Using IronPDF, it is often used for unique layouts in documents such as posters or specialty documents.
How can I set a custom paper size for a document?
To set a custom paper size in IronPDF, you need to instantiate the ChromePdfRenderer class, access the RenderingOptions, and use one of the SetCustomPaperSize methods based on your preferred measurement unit.
Which measurement units are supported for custom paper sizes in rendering libraries?
IronPDF supports setting custom paper sizes using centimeters, inches, millimeters, and pixels or points as measurement units.
How do I use a standard paper size in a PDF rendering library?
To use a standard paper size with IronPDF, create an instance of the ChromePdfRenderer class and set the PaperSize property in RenderingOptions to one of the predefined values from the PdfPaperSize enum.
Can I modify the paper dimensions of an existing PDF document?
Yes, using IronPDF, you can modify the dimensions of an existing PDF page using the ExtendPage method. This allows you to adjust each side of the page by specifying the target page index, values for each side, and the units of measurement.
What is the purpose of the feature that forces specific paper sizes in PDF rendering?
The ForcePaperSize feature in IronPDF ensures that the page sizes are exactly what is specified by resizing the page after generating a PDF from HTML. It helps bypass CSS rules that might otherwise dictate paper size.
How can I download a library for setting custom paper sizes in PDFs?
You can download IronPDF from NuGet. It provides the necessary tools to set custom paper sizes in PDFs.
Is there a code example for setting a custom paper size in centimeters?
Yes, in IronPDF, you can create an instance of ChromePdfRenderer, access RenderingOptions, and use SetCustomPaperSizeInCentimeters to set the dimensions. Then render and save your PDF.
What predefined standard paper sizes are available in PDF rendering libraries?
IronPDF offers over 100 predefined standard paper sizes, including common sizes like letter, A3, and A4, accessible via the PdfPaperSize enum.