Convert PDF to Grayscale in Java

Almost any document or image format (including images) can be converted to black-and-white or grayscale images. In this article, we will discuss how to convert color PDF documents to grayscale JPG and PNG image formats in Java.

Converted PDF to Grayscale

Java API to Convert Documents into Grayscale

GroupDocs.Conversion provides Java API for converting documents between a multitude of supported file formats and image types. Conversion results can be customized using multiple advanced options. I will use this GroupDocs.Conversion for Java API to convert the PDF documents to grayscale JPG and PNG images.

Download or Configure

You may download the JAR file from the downloads section, or just get the repository and dependency configurations for the pox.xml of your maven-based Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>22.3</version> 
</dependency>

Convert PDF to Grayscale JPG Image in Java

Starting with the conversion of color PDF documents and transforming them to image formats. The following steps show how to convert the PDF to grayscale JPG in Java.

  • Load the PDF document using Converter class.
  • Prepare the image conversion options.
  • Set the conversion file format as JPG.
  • Set the grayscale option to true.
  • Convert to image using appropriate convert() method with options.

The following Java source code converts the PDF document to a grayscale JPG image.

Furthermore, there are many other options to customize the height, width, horizontal and vertical flip, document rotations, and properties like brightness, gamma, and contrast. Additionally, you can apply the watermarks with different settings to the output image.

Convert PDF to Grayscale PNG Image in Java

Likewise, the color PDF document can also be converted to other image formats in grayscale. The following steps show how to convert a PDF file to grayscale PNG in Java.

  • Load the PDF document using Converter class.
  • Prepare conversion options using ImageConvertOptions.
  • Set the conversion file format as PNG.
  • Set the grayscale option to true.
  • Convert to PNG image using convert() method.

The following Java source code converts the PDF document to a grayscale PNG image.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

Conclusion

To sum up we learned how PDF documents can be converted to PNG or JPG Image formats in Java. Try building your own conversion application, you may learn more about the low code and high code Java APIs from the documentation for the automation of document conversions.

The easiest way is to experience the examples from GitHub. Contact us for any query via the forum.

See Also