Custom PDF Size
This code snippet illustrates how to define a custom paper size when rendering HTML content into a PDF document.
Use the paperSize
property to customize the paper size in the rendering options. The options object is used to specify the custom paper size, setting it to a custom 5x5-inch dimension. You can adjust the height and width properties as needed to match your desired paper size.
Utilize the PdfDocument.fromHtml
method from the IronPDF library to convert the HTML content into a PDF document while applying the specified rendering options.
After rendering, the resulting PDF document is saved with the custom paper size using the saveAs
method.
// Import the necessary namespace for IronPDF
using IronPdf;
// Define a method to create a PDF with a custom paper size
public class PdfGenerator
{
// Entry point for the PDF generation process
public static void GenerateCustomSizedPdf(string htmlContent, string outputPath)
{
// Create an instance of the ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Define custom paper size in inches (Width: 5 inches, Height: 5 inches)
var customPaperSize = new PdfPaperSize(5.0, 5.0);
// Set the custom paper size in rendering options
renderer.RenderingOptions.PaperSize = customPaperSize;
// Render the HTML content to a PDF document with the specified options
PdfDocument pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the rendered PDF document to a specified file path
pdfDocument.SaveAs(outputPath);
}
}
// Import the necessary namespace for IronPDF
using IronPdf;
// Define a method to create a PDF with a custom paper size
public class PdfGenerator
{
// Entry point for the PDF generation process
public static void GenerateCustomSizedPdf(string htmlContent, string outputPath)
{
// Create an instance of the ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Define custom paper size in inches (Width: 5 inches, Height: 5 inches)
var customPaperSize = new PdfPaperSize(5.0, 5.0);
// Set the custom paper size in rendering options
renderer.RenderingOptions.PaperSize = customPaperSize;
// Render the HTML content to a PDF document with the specified options
PdfDocument pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);
// Save the rendered PDF document to a specified file path
pdfDocument.SaveAs(outputPath);
}
}
' Import the necessary namespace for IronPDF
Imports IronPdf
' Define a method to create a PDF with a custom paper size
Public Class PdfGenerator
' Entry point for the PDF generation process
Public Shared Sub GenerateCustomSizedPdf(ByVal htmlContent As String, ByVal outputPath As String)
' Create an instance of the ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Define custom paper size in inches (Width: 5 inches, Height: 5 inches)
Dim customPaperSize = New PdfPaperSize(5.0, 5.0)
' Set the custom paper size in rendering options
renderer.RenderingOptions.PaperSize = customPaperSize
' Render the HTML content to a PDF document with the specified options
Dim pdfDocument As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
' Save the rendered PDF document to a specified file path
pdfDocument.SaveAs(outputPath)
End Sub
End Class
The
GenerateCustomSizedPdf
method takes two arguments:htmlContent
(the HTML content to be rendered into PDF) andoutputPath
(the file path where the generated PDF will be saved).The
ChromePdfRenderer
is used to handle the rendering process of HTML content.A
PdfPaperSize
object is created to specify a custom paper size of 5x5 inches. This is achieved by passing the width and height values (in inches) to thePdfPaperSize
constructor.The
RenderingOptions
property of therenderer
is set to use the custom paper size.The
RenderHtmlAsPdf
method converts the given HTML content into a PDF document according to the predefined settings.- Finally, the
SaveAs
method saves the PDF document at the specifiedoutputPath
.