How to Convert JPG to Black and White PDF in C#

This tutorial provides details on how to convert JPG to Black and White PDF in C#. You will get all the necessary resources for setting the environment settings, important namespaces, classes, methods and the programming sequence to accomplish this task. The description will also be provided to customize this process of writing a JPG to Black and White PDF converter in C# with the help of a few API calls only.

Steps to Convert JPG to Black and White PDF in C#

  1. Establish the environment to add Aspose.Imaging using the NuGet package manager
  2. Load the source JPG image into the Image class object
  3. Perform casting of the loaded image to RasterCachedImage class object
  4. Cache image data if it is not cached
  5. Using the predefined fixed threshold, binarize the image
  6. Save the resultant image as PDF using the PdfOptions object

These steps describe the process to change JPG to Black and White PDF using C# in a step-by-step approach where first the environment is set by adding the required resources to the project and then the source JPG is loaded into the Image class object that is to be changed to Black and White PDF. In the next steps, type casting is done for the image to RasterCachedImage and then it is cached into memory for the sake of performance if not done earlier. Finally, we binarize the image using the predefined fixed threshold and then the final output is saved as a PDF.

Code to Transform JPG to Black and White PDF in C#

This code loads the file for converting JPG to Black and White PDF in C# using the Image class object that not only supports many other types of images but also has multiple overloaded functions like you can load image from a stream rather than the file on disk and use additional LoadOptions parameter. This LoadOptions object supports setting the progress event handler, data recovery mode, data background color and buffer size hint.

This tutorial has taught us to convert a JPG to a Black and White PDF file. If you are interested in learning the process to resize image, refer to the article on how to resize image in C#.

 English