AVAILABLE NOW: Spring 2025 Release
By Apryse | 2025 Apr 03
15 min
Tags
image
pdf conversion
ocr
Pre-processing is a critical step in improving the accuracy and quality of OCR results, and that may be why many developers are looking for the best way to convert an image to a black & white PDF file, to more easily import a better-quality file into OCR for text data extraction. With Apryse SDK, it’s easy to convert images to grayscale PDF.
We’ve included sample code showing how to complete this using C# and Java below.
using pdftron;
using pdftron.Common;
using pdftron.PDF;
using pdftron.SDF;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
try {
PDFNet.Initialize("your_license_key");
string inputPath = "path_to_input_image";
string exportGrayScalePath = "grayscale.jpg";
PDFDoc doc = new PDFDoc();
Page page = WritePage(doc, inputPath);
PDFDraw draw = new PDFDraw(300);
ObjSet hint_set = new ObjSet();
Obj gray_hint = hint_set.CreateDict();
gray_hint.PutName("ColorSpace", "Gray");
draw.Export(page, exportGrayScalePath, "JPEG", gray_hint);
doc.Close();
PDFDoc grayDoc = new PDFDoc();
WritePage(grayDoc, exportGrayScalePath);
// Save the PDF
grayDoc.Save("grayscale.pdf", SDFDoc.SaveOptions.e_linearized);
grayDoc.Close();
Console.WriteLine("Image converted to greyscale and added to PDF successfully!");
}
catch (PDFNetException e)
{
Console.WriteLine(e.Message);
}
}
static Page WritePage(PDFDoc doc, string imageFile)
{
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Image img = Image.Create(doc, imageFile);
Page page = doc.PageCreate(new Rect(0, 0, img.GetImageWidth(), img.GetImageHeight()));
writer.Begin(page);
Element element = builder.CreateImage(img, 0, 0, img.GetImageWidth(), img.GetImageHeight());
writer.WritePlacedElement(element);
writer.End();
doc.PagePushBack(page);
return page;
}
}
}
import com.pdftron.common.PDFNetException;
import com.pdftron.pdf.*;
import com.pdftron.sdf.*;
public class App {
public static void main(String[] args) {
try {
PDFNet.initialize("your_license_key");
String inputPath = "path_to_input_image";
String exportGrayScalePath = "src\\samples\\grayscale.jpg";
PDFDoc doc = new PDFDoc();
Page page = writePage(doc, inputPath);
PDFDraw draw = new PDFDraw();
draw.setDPI(300);
ObjSet hintSet = new ObjSet();
Obj grayHint = hintSet.createDict();
grayHint.putName("ColorSpace", "Gray");
draw.export(page, exportGrayScalePath, "JPEG", grayHint);
doc.close();
PDFDoc grayDoc = new PDFDoc();
writePage(grayDoc, exportGrayScalePath);
// Save the PDF
grayDoc.save("src\\samples\\grayscale.pdf", SDFDoc.SaveMode.LINEARIZED, null);
grayDoc.close();
System.out.println("Image converted to grayscale and added to PDF successfully!");
} catch (PDFNetException e) {
System.out.println(e.getMessage());
}
}
static Page writePage(PDFDoc doc, String imageFile) throws PDFNetException {
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Image img = Image.create(doc, imageFile);
Page page = doc.pageCreate(new Rect(0, 0, img.getImageWidth(), img.getImageHeight()));
writer.begin(page);
Element element = builder.createImage(img, 0, 0, img.getImageWidth(), img.getImageHeight());
writer.writePlacedElement(element);
writer.end();
doc.pagePushBack(page);
return page;
}
}
If you’re interested in trying Apryse SDK, you can start your trial right away. If you have any questions, please contact sales or check out our Discord.
Tags
image
pdf conversion
ocr
Apryse
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content