Is there a way to make a PDF of only the hidden text layer of a DjVu file?

I know how to use

djvused myfile.djvu -e 'output-txt' > myfile.dsed

to output a DjVu script that includes the x,y coordinates of each word of each page, but how would I convert that to a PS or PDF? I'm sure someone has a script.

The dsed file begins with S-expressions like this (click here for more info):

select; remove-txt

# ------------------------- 

select "myfile_0000.djvu"
(page 108 107 1176 1725
 (column 941 1694 1176 1725
  (region 941 1694 1176 1725
   (para 941 1694 1176 1725
    (line 941 1694 1176 1725
     (word 941 1694 999 1723 "another")
     (word 1024 1696 1176 1725 "word")))))
 (column 108 107 805 1519
  (region 108 107 805 1519
   (para 226 1491 701 1519
    (line 226 1491 701 1519
     (word 226 1491 701 1519 "example")))
   (para 350 1370 534 1396




Updated after OP input:

In order to typeset the OCR layer of a DJVU file into a PDF file, that is print the words at the location they appear in the DJVU file, one option is to programatically generate an Inkscape or Scribus file. I will sketch the procedure. It is not difficult to write a script in your language of choice (Python, Ruby, Racket, etc.).

  1. For every page of the DJVU file export the OCR layer as HTML or as an S-expression. I will assume the latter in the following.

  2. Extract the page size from (page x1 y1 x2 y2.... Page width = x2-x1, Page height = y2-y1.

  3. For each word calculate the width, height and coordinates of the lower left corner of the text frame that will contain it. That is turn (word x1 y1 x2 y2 "another") into a text frame with lower left corner at (x1, Page height - y1), width x2 - x1 and height y2 - y1.

  4. Select suitable font and font size for the text frame. Use the height of the word rectangles as a guidance. Make sure that the text is not stretched to fit the text frame.

  5. Once you are finished adding all the text frames to the page export the file to PDF. Notice that the page size is much larger than letter or A4. If you intend to print the document make sure to scale it in the print dialog to fit your paper size. In order to save trees print to a PDF file first.

Original answer:

According to man djvused, the command 'output-txt' will generate a djvused script, which is intended for consumption by djvused.

For your purposes it is better to use (according to the man page)

djvused myfile.djvu -e 'print-pure-txt' > myfile.txt

Then you can produce a PDF file using a program that can print to PDF.

Depending on your operating system and version you may need to install a PDF printer. You can also open it in a recent version of Chrome or Firefox and print to PDF.

  • Yes, but the position of the text in the PDF document would not be as it was in the DjVu one. – Geremia Aug 16 '16 at 22:28
  • Essentially, I need a script to parse dsed files and make a PDF with the text at the correct coordinates. – Geremia Aug 16 '16 at 22:29
  • That was not clear in the question. See the updated answer. Hope that helps. – Marduk Aug 18 '16 at 21:13

The easiest way to do this is to convert to the DjVu to PDF (making sure the OCR layer is maintained in the PDF). Then run:

gs -q -o - -dFILTERIMAGE -sDEVICE=pdfwrite -f "${input_pdf}" | pdftk - output - uncompress | sed "s/^3 Tr$/0 Tr/g" | pdftk - output "${output_pdf}" compress

where the user specifies ${input_pdf} and ${output_pdf}. gs removes (-dFILTERIMAGE) all the images, and sed makes it so the PDF renders the hidden OCR text as visible (by changing PDF's 3 Tr or "hidden text render" command to 0 Tr or "default text render"). The last pdftk command isn't strictly necessary, but the first one is, else sed wouldn't be able to change the PDF Tr command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.