How to Convert JPG to Black and White PDF in Java

This brief tutorial has all the information on how to convert JPG to Black and White PDF in Java. The complete description is provided like environment configuration, programming approach, and runnable sample code to develop a JPG to Black and White PDF converter in Java. Moreover, different options are also shared that can be used to customize this conversion process in any of the operating systems that support Java.

Steps to Change JPG to Black and White PDF using Java

  1. Establish the environment to add Aspose.Imaging for Java from the repository
  2. Load the source JPG image into the Image class object for conversion to Black and White PDF
  3. Perform the casting of this loaded image to RasterCachedImage class for accessing the necessary features
  4. Check if the image is cached or not and cache it if not done already
  5. Binarize the loaded image using the binarizeFixed() method with a predefined fixed threshold
  6. Save the resultant image as a PDF file using the PdfOptions object for customization

The aforementioned steps guide to establish the environment and then present a detailed procedure for writing a complete application to change JPG to Black and White PDF in Java. It explains how to load the source image, and transform it into an object of RasterCachedImage class for accessing the desired methods for binarization and caching. The same class contains the save method for converting the image to the target PDF.

Code to Transform JPG to Black and White PDF in Java

This code demonstrates the process to transform JPG to Black and White PDF in Java where the Image class is used for loading the source JPG file. Note that you can check if the image can be loaded or not using the canLoad() method and also use the LoadOptions class object to set a variety of properties for loading an image like setting the data recovery mode, and setting the progress event handler. Similarly, once the image is loaded, you can resize the image, rotate it, or even modify it by setting its background, etc. if required.

This tutorial has guided us to create a Black and White PDF from an image in Java. If you want to learn the process to resize an image, refer to the article on how to resize image using Java.

 English