HOCR without image processing

The current (java) mechanism for overlaying HOCR text with documents is to use the Document.CallBackGetHocr interface with Document.convert method. This requires decoding of each image which can be pretty CPU/Memory intensive.

If we already have the HOCR for the document, is it possible just to overlay the text without using the callback/image decoding? I simply want to add the text at the required positions.

@snewby,

You can retrieve the rectangular position of the image, delete this image, and then add text at this rectangle position. Please refer to these help topics: Retrieve the rectangular position of an image, Add Text with TextParagraph and Delete Images from a PDF File

Those are for the .NET API but I think I’ve found the java equivalent. Seems like there are more factors to consider here (transparent text, text positioning adjustment relative to the image x/y, font scaling to fit the bounding box, etc.). I thought maybe there was a way to simply provide the HOCR without the image decoding but maybe not?

@snewby,

Kindly send us the source PDF and expected output PDF documents. We will investigate and share our findings with you.

Sure, here’s the example documents (before and after HOCR)

source.pdf (24.8 KB)
expected.pdf (77.4 KB)

@snewby,

We have logged a feature request under the ticket ID PDFJAVA-37343 in our issue tracking system to add HOCR formatted text in a PDF document. You might also share the sample HOCR formatted samples which you require to add in the PDF document. We have linked your post to this ticket and will keep you informed regarding any available updates.

Thanks! I’m attaching the HOCR html file we used for this document.

hocr.zip (2.5 KB)

@snewby,

Thank you. We have logged the sample HOCR document under the same ticket ID PDFJAVA-37343 in our issue tracking.

@snewby,

In reference to the linked ticket ID PDFJAVA-37343, please use the following code:
Java

final String myDir = "C:/path/";
Document doc = new Document(myDir + "source.pdf");
doc.convert(new Document.CallBackGetHocr()
{
    @Override
    public String invoke(java.awt.image.BufferedImage bi)
    {
        //Ignore the argument "bi" and use the existing hocr.html file

       try
       {
           int len;
           char[] chr = new char[4096];
           final StringBuffer buffer = new StringBuffer();
           final FileReader reader = new FileReader(myDir + "hocr.html");
           try
           {
               while ((len = reader.read(chr)) > 0)
               {
                   buffer.append(chr, 0, len);
               }
               } finally
               {
                   reader.close();
                }
            return buffer.toString();
        } catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                } catch (java.lang.Exception exc)
                {
                    exc.printStackTrace();
                }
                return null;
            }
        });
doc.save(myDir + "converted.hocr_out.pdf");

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan