JAVA: How to Convert PDF from Color to Grayscale

This article takes Java code as an example to introduce how to convert a color PDF file into a grayscale (black and white) PDF file, that is, convert the color image or text in the PDF document into gray by calling the PdfGrayConverter.toGrayPdf() method. It can reduce the size of the document by adjusting the document without color effect. The following is the program running environment and code samples.

Configure the program environment

  • IntelliJ IDEA
  • Spire.PDF for Java 5.3.1
  • PDF document

About how to import jar files:

1. Download from the Maven repository and configure Pom.xml as follows:

            <url>https: //</url> 
    </repositories > 
        <groupId> e-iceblue </groupId> 

2. Manually add the jar

Download the jar package locally, then unzip it, and find Spire.Pdf.jar in the lib folder. Then open the following interface in IDEA, and add the jar file in the local path to the Java program.


Convert color PDF to grayscale

This conversion requires only the following two steps:

  • Use the PdfGrayConverter class to load PDF files.
  • Call the PdfGrayConverter.toGrayPdf() method to convert the PDF to grayscale.


import com.spire.pdf.conversion.* ; 

public  class ToGrayPDF {
     public  static  void main(String[] args) {
         // Create a PdfGrayConverter instance and load the PDF document 
        PdfGrayConverter converter = new PdfGrayConverter("Booklet.pdf" ); 

        / / Convert color PDF to grayscale 
        converter.toGrayPdf("ToGray.pdf" ); 

Conversion result:

Translated from

Read More:

Leave a Reply

Your email address will not be published. Required fields are marked *