Set to Grayscale
When rendering an HTML page to PDF with IronPDF, output options for the ChromePdfRenderer
class can be changed to create a custom document that fits your needs. These options include paper size, DPI, headers and footers, and other Chromium-specific browser setup options. IronPDF also has the option to set your PDF color to grayscale.
This example shows you how to render your PDF document to grayscale.
Converting your document to grayscale makes the colors wash down to black and white. This makes printing on black and white printers easy. The grayscale option in the ChromePdfRenderer
class outputs a black and white PDF document. However, the default value is false. If you want the document to render in grayscale, you would have to manually put in the grayscale settings.
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Create an instance of ChromePdfRenderer
var Renderer = new ChromePdfRenderer();
// Set the rendering to grayscale
Renderer.RenderingOptions.SetPaperSize(PaperSize.A4);
Renderer.RenderingOptions.RenderInGrayscale = true; // This option enables grayscale rendering
// Render an HTML file to PDF
var PdfDocument = Renderer.RenderHtmlAsPdf("<html><body><h1>Hello, World!</h1></body></html>");
// Save the PDF document to a specific path
PdfDocument.SaveAs("GrayscalePDF.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Create an instance of ChromePdfRenderer
var Renderer = new ChromePdfRenderer();
// Set the rendering to grayscale
Renderer.RenderingOptions.SetPaperSize(PaperSize.A4);
Renderer.RenderingOptions.RenderInGrayscale = true; // This option enables grayscale rendering
// Render an HTML file to PDF
var PdfDocument = Renderer.RenderHtmlAsPdf("<html><body><h1>Hello, World!</h1></body></html>");
// Save the PDF document to a specific path
PdfDocument.SaveAs("GrayscalePDF.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create an instance of ChromePdfRenderer
Dim Renderer = New ChromePdfRenderer()
' Set the rendering to grayscale
Renderer.RenderingOptions.SetPaperSize(PaperSize.A4)
Renderer.RenderingOptions.RenderInGrayscale = True ' This option enables grayscale rendering
' Render an HTML file to PDF
Dim PdfDocument = Renderer.RenderHtmlAsPdf("<html><body><h1>Hello, World!</h1></body></html>")
' Save the PDF document to a specific path
PdfDocument.SaveAs("GrayscalePDF.pdf")
End Sub
End Class
This code snippet demonstrates how to render an HTML document to a grayscale PDF using the IronPDF library.
ChromePdfRenderer Instance: We begin by creating an instance of
ChromePdfRenderer
. This class provides methods for converting HTML to PDFs.Set Rendering Options: The
RenderingOptions
property of the renderer is used to specify how we want the PDF to be rendered. TheSetPaperSize
method sets the document to A4 size. TheRenderInGrayscale
property is set totrue
to enable grayscale rendering. By default, PDFs are not rendered in grayscale unless specified.Render HTML: We call the
RenderHtmlAsPdf
method on theRenderer
object, passing in a simple HTML string. This method converts the HTML content to a PDF format.- Save the PDF: Finally, the
SaveAs
method is used to save the rendered PDF to a file named "GrayscalePDF.pdf" on the disk.
To learn more about the capabilities of IronPDF and other Iron Software libraries such as IronBarcode and IronOCR, you can visit the IronPDF Website and explore other resources and documentation available on Iron Software's Official Site.