Set to Grayscale
Converting a colored PDF document to grayscale can offer several advantages, such as removing the consideration of color and focusing solely on color intensity.
The feature to generate grayscale PDFs is only available when converting HTML to PDF with IronPDF's HTML to PDF Conversion Feature. You can apply a rendering option that includes a grayScale
property, which can be set to true
or false
. Setting this property to true
will generate a grayscale PDF instead of a colored one.
Below is a sample code snippet demonstrating how to convert an HTML file to a grayscale PDF using IronPDF in C#:
using IronPdf;
class Program
{
static void Main()
{
// Initialize a new HTML to PDF Renderer
var Renderer = new HtmlToPdf();
// Create a render options object
var Options = new PdfPrintOptions()
{
// Set the grayscale option to true to convert the PDF to grayscale
GrayScale = true
};
// Assign the render options to the renderer
Renderer.PrintOptions = Options;
// HTML content or file to be converted
string htmlContent = "<h1>Hello World</h1><p>This is a test PDF document in grayscale.</p>";
// Generate a PDF from HTML content
PdfDocument PDF = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the generated PDF to a file
PDF.SaveAs("output.pdf");
// Indicate the completion of the process
System.Console.WriteLine("PDF document created successfully in grayscale!");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Initialize a new HTML to PDF Renderer
var Renderer = new HtmlToPdf();
// Create a render options object
var Options = new PdfPrintOptions()
{
// Set the grayscale option to true to convert the PDF to grayscale
GrayScale = true
};
// Assign the render options to the renderer
Renderer.PrintOptions = Options;
// HTML content or file to be converted
string htmlContent = "<h1>Hello World</h1><p>This is a test PDF document in grayscale.</p>";
// Generate a PDF from HTML content
PdfDocument PDF = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the generated PDF to a file
PDF.SaveAs("output.pdf");
// Indicate the completion of the process
System.Console.WriteLine("PDF document created successfully in grayscale!");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Initialize a new HTML to PDF Renderer
Dim Renderer = New HtmlToPdf()
' Create a render options object
Dim Options = New PdfPrintOptions() With {.GrayScale = True}
' Assign the render options to the renderer
Renderer.PrintOptions = Options
' HTML content or file to be converted
Dim htmlContent As String = "<h1>Hello World</h1><p>This is a test PDF document in grayscale.</p>"
' Generate a PDF from HTML content
Dim PDF As PdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the generated PDF to a file
PDF.SaveAs("output.pdf")
' Indicate the completion of the process
System.Console.WriteLine("PDF document created successfully in grayscale!")
End Sub
End Class
Explanation of the Code
- Using IronPdf: Include the IronPdf library to access PDF rendering functionalities.
- HtmlToPdf Renderer Initialization: Create an instance of the
HtmlToPdf
class, which facilitates the HTML to PDF conversion process. - Setting Render Options: Define the rendering options through the
PdfPrintOptions
class. The key property here isGrayScale
, which, when set totrue
, ensures that the resulting PDF is rendered in grayscale. - Rendering HTML to PDF: Use
RenderHtmlAsPdf
to convert HTML content into a PDF document. - Saving the PDF: The resulting grayscale PDF is saved to the specified file path "output.pdf".
- Console Notification: Display a message on the console to inform the user that the PDF has been created successfully.
This code provides a clear example of how to convert an HTML document into a grayscale PDF using IronPDF's HTML to PDF conversion tools.