You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Timm Friedholz <ti...@gmx.de> on 2015/10/26 22:46:51 UTC

Merging pages of two PDF documents

Hello,

I have some PDF documents in which the glyph-Unicode character mapping is destroyed so that it's not possible to search and copy the text.  In an attempt to remove this restriction I've converted the PDFs to TIFF images and performed OCR on them using tesseract.  Tesseract exports the recognized text as PDF files in which the text is positioned transparently on top of the images such that the text is searchable and selectable.

The problem is that the images in the PDF that tesseract outputs are gray-scaled, large and high contrast versions of the original PDFs and I would like to have the quality and file size of the original PDFs instead.  Thus my idea is to copy  the text objects of the OCR output to the original PDFs.  To avoid interference with the old text, I've converted the original PDFs to vector paths using Ghostscript:  gs -o out.pdf -dNoOutputFonts -sDEVICE=pdfwrite in.pdf

Now the problem is that I'm not sure how to approach this programmatically.  Can I simply iterate over the pages and copy the text objects from each page of one document to the corresponding page of the other document?  Which operators do I need to copy if I parse it token by token?  Should I actually do it as directly via the PDFStreamParser class or are there abstraction in PDFBox that will make this easier?

The code for the PDF export by tesseract is here:

https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L1

Here is the line that specifies the text objects:

https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L317

Thanks.

Timm
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Merging pages of two PDF documents

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.10.2015 um 17:53 schrieb Tilman Hausherr:
>
> To see this better, use PDFDebugger.
> https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/debugger-app/ 
>
meant
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/debugger-app/2.0.0-SNAPSHOT/


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Merging pages of two PDF documents

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.10.2015 um 10:12 schrieb Timm Friedholz:
>> On 27.10.2015, at 06:51, Maruan Sahyoun <sa...@fileaffairs.de> wrote:
>>
>> Hi,
>>
>>> Am 26.10.2015 um 22:46 schrieb Timm Friedholz <ti...@gmx.de>:
>>>
>>> Hello,
>>>
>>> I have some PDF documents in which the glyph-Unicode character mapping is destroyed so that it's not possible to search and copy the text.  In an attempt to remove this restriction I've converted the PDFs to TIFF images and performed OCR on them using tesseract.  Tesseract exports the recognized text as PDF files in which the text is positioned transparently on top of the images such that the text is searchable and selectable.
>>>
>>> The problem is that the images in the PDF that tesseract outputs are gray-scaled, large and high contrast versions of the original PDFs and I would like to have the quality and file size of the original PDFs instead.  Thus my idea is to copy  the text objects of the OCR output to the original PDFs.  To avoid interference with the old text, I've converted the original PDFs to vector paths using Ghostscript:  gs -o out.pdf -dNoOutputFonts -sDEVICE=pdfwrite in.pdf
>>>
>>> Now the problem is that I'm not sure how to approach this programmatically.  Can I simply iterate over the pages and copy the text objects from each page of one document to the corresponding page of the other document?  Which operators do I need to copy if I parse it token by token?  Should I actually do it as directly via the PDFStreamParser class or are there abstraction in PDFBox that will make this easier?
>> the easiest might be to
>> a) remove the images from the OCR'ed document
>> b) overlay the pages from the OCR'ed document over the original PDF using org.apache.pdfbox.multipdf.Overlay
>>
>> BR
>> Maruan
> Hello Maruan,
>
> Thanks for your reply. How would I go about removing the images exactly? I think this is the line that defines the images in tesseract's PDF renderer:
>
> https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L755 <https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L755>
>
> Would I be able to access the image objects if I run the PDFStreamParser on the contents per page as it’s done in some of the examples, or are they stored somewhere else in the PDF file? Which operators mark the beginning and end of it?
>
> Timm

The easiest would be to remove the invoke calls from the content stream. 
You can still remove the actual images later (so that you project goes 
forward) This looks somewhat like this:

/Im1 Do

To see this better, use PDFDebugger.
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/debugger-app/

If possible, upload such a PDF somewhere.

The line you mention is where the object is defined, but the name of the 
object is defined in the resources dictionary. Just removing it will 
create just a mess.

You can get the token list, and rewrite the tokens with 
ContentStreamWriter.writeTokens

Tilman

Re: Merging pages of two PDF documents

Posted by Timm Friedholz <ti...@gmx.de>.
> On 27.10.2015, at 06:51, Maruan Sahyoun <sa...@fileaffairs.de> wrote:
> 
> Hi,
> 
>> Am 26.10.2015 um 22:46 schrieb Timm Friedholz <ti...@gmx.de>:
>> 
>> Hello,
>> 
>> I have some PDF documents in which the glyph-Unicode character mapping is destroyed so that it's not possible to search and copy the text.  In an attempt to remove this restriction I've converted the PDFs to TIFF images and performed OCR on them using tesseract.  Tesseract exports the recognized text as PDF files in which the text is positioned transparently on top of the images such that the text is searchable and selectable.
>> 
>> The problem is that the images in the PDF that tesseract outputs are gray-scaled, large and high contrast versions of the original PDFs and I would like to have the quality and file size of the original PDFs instead.  Thus my idea is to copy  the text objects of the OCR output to the original PDFs.  To avoid interference with the old text, I've converted the original PDFs to vector paths using Ghostscript:  gs -o out.pdf -dNoOutputFonts -sDEVICE=pdfwrite in.pdf
>> 
>> Now the problem is that I'm not sure how to approach this programmatically.  Can I simply iterate over the pages and copy the text objects from each page of one document to the corresponding page of the other document?  Which operators do I need to copy if I parse it token by token?  Should I actually do it as directly via the PDFStreamParser class or are there abstraction in PDFBox that will make this easier?
> 
> the easiest might be to
> a) remove the images from the OCR'ed document
> b) overlay the pages from the OCR'ed document over the original PDF using org.apache.pdfbox.multipdf.Overlay
> 
> BR
> Maruan

Hello Maruan,

Thanks for your reply. How would I go about removing the images exactly? I think this is the line that defines the images in tesseract's PDF renderer:

https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L755 <https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L755>

Would I be able to access the image objects if I run the PDFStreamParser on the contents per page as it’s done in some of the examples, or are they stored somewhere else in the PDF file? Which operators mark the beginning and end of it?

Timm

Re: Merging pages of two PDF documents

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 26.10.2015 um 22:46 schrieb Timm Friedholz <ti...@gmx.de>:
> 
> Hello,
> 
> I have some PDF documents in which the glyph-Unicode character mapping is destroyed so that it's not possible to search and copy the text.  In an attempt to remove this restriction I've converted the PDFs to TIFF images and performed OCR on them using tesseract.  Tesseract exports the recognized text as PDF files in which the text is positioned transparently on top of the images such that the text is searchable and selectable.
> 
> The problem is that the images in the PDF that tesseract outputs are gray-scaled, large and high contrast versions of the original PDFs and I would like to have the quality and file size of the original PDFs instead.  Thus my idea is to copy  the text objects of the OCR output to the original PDFs.  To avoid interference with the old text, I've converted the original PDFs to vector paths using Ghostscript:  gs -o out.pdf -dNoOutputFonts -sDEVICE=pdfwrite in.pdf
> 
> Now the problem is that I'm not sure how to approach this programmatically.  Can I simply iterate over the pages and copy the text objects from each page of one document to the corresponding page of the other document?  Which operators do I need to copy if I parse it token by token?  Should I actually do it as directly via the PDFStreamParser class or are there abstraction in PDFBox that will make this easier?

the easiest might be to
a) remove the images from the OCR'ed document
b) overlay the pages from the OCR'ed document over the original PDF using org.apache.pdfbox.multipdf.Overlay

BR
Maruan


> 
> The code for the PDF export by tesseract is here:
> 
> https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L1
> 
> Here is the line that specifies the text objects:
> 
> https://github.com/tesseract-ocr/tesseract/blob/dd8c12997385cf7f5961093bcd44f0396b08f96f/api/pdfrenderer.cpp#L317
> 
> Thanks.
> 
> Timm
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org