Join us for an engaging webinar, as we unravel the potential of AI for revolutionizing document management.
Watch Now
Enable your employees to remain productive throughout the document management process.
Read More
Learn how SmartZone uses a regular expression engine integrated into the recognition engine to achieve the best possible accuracy on data that can be defined by a regular expression.
Docubee is an intelligent contact automation platform built to help your team success
I am trying to perform OCR on a PDF created from a scanned document. I need to rasterize the PDF page before importing the page into the recognition engine. When rasterizing the PDF page I want to set the bit depth of the generated page to be equal to the bit depth of the embedded image so I may use better compression methods for 1-bit and 8-bit images.
ImGearPDFPage.DIB.BitDepth will always return 24 for the bit depth of a PDF. Is there a way to detect the bit depth based on the PDF’s embedded content?
To do this:
ImGearPDFPage.GetContent()
ImGearPDEImage
ImGearPage
The code below demonstrates how to do detect the bit depth of a PDF page for all pages in a PDF document, perform OCR, and save the output while using compression.
private static void Recognize(ImGearRecognition engine, string sourceFile, ImGearPDFDocument doc) { using (ImGearPDFDocument outDoc = new ImGearPDFDocument()) { // Import pages foreach (ImGearPDFPage pdfPage in doc.Pages) { int highestBitDepth = 0; ImGearPDEContent pdeContent = pdfPage.GetContent(); int contentLength = pdeContent.ElementCount; for (int i = 0; i < contentLength; i++) { ImGearPDEElement el = pdeContent.GetElement(i); if (el is ImGearPDEImage) { //create an imGearPage from the embedded image and find its bit depth int bitDepth = (el as ImGearPDEImage).ToImGearPage().DIB.BitDepth; if (bitDepth > highestBitDepth) { highestBitDepth = bitDepth; } } } if(highestBitDepth == 0) { //if no images found in document or the images are embedded deeper in containers we set to a default bitDepth of 24 to be safe highestBitDepth = 24; } ImGearRasterPage rasterPage = pdfPage.Rasterize(highestBitDepth, 200, 200); using (ImGearRecPage recogPage = engine.ImportPage(rasterPage)) { recogPage.Image.Preprocess(); recogPage.Recognize(); ImGearRecPDFOutputOptions options = new ImGearRecPDFOutputOptions() { VisibleImage = true, VisibleText = false, OptimizeForPdfa = true, ImageCompression = ImGearCompressions.AUTO, UseUnicodeText = false }; recogPage.CreatePDFPage(outDoc, options); } } outDoc.SaveCompressed(sourceFile + ".result.pdf"); } }
For the compression type, I would recommend setting it to AUTO. AUTO will set the compression type depending on the image’s bit depth. The compression types that AUTO uses for each bit depth are:
Disclaimer: This may not work for all PDF documents due to some PDF’s structure. If you’re unfamiliar with how PDF content is structured, we have an explanation in our documentation. The above implementation of this only checks one layer into the PDF, so if there were containers that had images embedded in them, then it will not detect them. However, this should work for documents created by scanners, as the scanned image should be embedded in the first PDF layer. If you have more complex documents, you could write a recursive function that goes through the layers of the PDF to find the images. The above code will set the bit depth to 24 if it wasn’t able to detect any images in the first layer, just to be on the safe side.
Disclaimer: This may not work for all PDF documents due to some PDF’s structure. If you’re unfamiliar with how PDF content is structured, we have an explanation in our documentation. The above implementation of this only checks one layer into the PDF, so if there were containers that had images embedded in them, then it will not detect them.
However, this should work for documents created by scanners, as the scanned image should be embedded in the first PDF layer. If you have more complex documents, you could write a recursive function that goes through the layers of the PDF to find the images.
The above code will set the bit depth to 24 if it wasn’t able to detect any images in the first layer, just to be on the safe side.