PDF text extraction from given coordinates
Asked Answered
R

3

45

I would like to extract text from a portion (using coordinates) of PDF using Ghostscript.

Can anyone help me out?

Renfroe answered 31/5, 2011 at 11:59 Comment(1)
You will have a lot of trouble doing that with coordinates. That would require finding every text cell in the document, calculating string width and wrapping, then calculation clipping windows and deciding on inclusion/exclusion. Then would come the task of ordering it visually. Very hard.Abscess
A
109

Yes, with Ghostscript, you can extract text from PDFs. But no, it is not the best tool for the job. And no, you cannot do it in "portions" (parts of single pages). What you can do: extract the text of a certain range of pages only.

First: Ghostscript's txtwrite output device (not so good)

 gs \
   -dBATCH \
   -dNOPAUSE \
   -sDEVICE=txtwrite \
   -dFirstPage=3 \
   -dLastPage=5 \
   -sOutputFile=- \
   /path/to/your/pdf

This will output all text contained on pages 3-5 to stdout. If you want output to a text file, use

   -sOutputFile=textfilename.txt

gs Update:

Recent versions of Ghostscript have seen major improvements in the txtwrite device and bug fixes. See recent Ghostscript changelogs (search for txtwrite on that page) for details.


Second: Ghostscript's ps2ascii.ps PostScript utility (better)

This one requires you to download the latest version of the file ps2ascii.ps from the Ghostscript Git source code repository. You'd have to convert your PDF to PostScript, then run this command on the PS file:

gs \
  -q \
  -dNODISPLAY \
  -P- \
  -dSAFER \
  -dDELAYBIND \
  -dWRITESYSTEMDICT \
  -dSIMPLE \
   /path/to/ps2ascii.ps \
   input.ps \
  -c quit

If the -dSIMPLE parameter is not defined, each output line contains some additional info beyond the pure text content about fonts and fontsize used.

If you replace that parameter by -dCOMPLEX, you'll get additional infos about colors and images used.

Read the comments inside the ps2ascii.ps to learn more about this utility. It's not comfortable to use, but for me it worked in most cases I needed it....

Third: XPDF's pdftotext CLI utility (more comfortable than Ghostscript)

A more comfortable way to do text extraction: use pdftotext (available for Windows as well as Linux/Unix or Mac OS X). This utility is based either on Poppler or on XPDF. This is a command you could try:

 pdftotext \
   -f 13 \
   -l 17 \
   -layout \
   -opw supersecret \
   -upw secret \
   -eol unix \
   -nopgbrk \
   /path/to/your/pdf
   - |less

This will display the page range 13 (first page) to 17 (last page), preserve the layout of a double-password protected named PDF file (using user and owner passwords secret and supersecret), with Unix EOL convention, but without inserting pagebreaks between PDF pages, piped through less...

pdftotext -h displays all available commandline options.

Of course, both tools only work for the text parts of PDFs (if they have any). Oh, and mathematical formula also won't work too well... ;-)


pdftotext Update:

Recent versions of Poppler's pdftotext have now options to extract "a portion (using coordinates) of PDF" pages, like the OP asked for. The parameters are:

  • -x <int> : top left corner's x-coordinate of crop area
  • -y <int> : top left corner's y-coordinate of crop area
  • -W <int> : crop area's width in pixels (defaults to 0)
  • -H <int> : crop area's height in pixels (defaults to 0)

Best, if used with the -layout parameter.


Fourth: MuPDF's mutool draw command can also extract text

The cross-platform, open source MuPDF application (made by the same company that also develops Ghostscript) has bundled a command line tool, mutool. To extract text from a PDF with this tool, use:

mutool draw -F txt the.pdf

will emit the extracted text to <stdout>. Use -o filename.txt to write it into a file.

Fifth: PDFLib's Text Extraction Toolkit (TET) (best of all... but it is PayWare)

TET, the Text Extraction Toolkit from the pdflib family of products can find the x-y-coordinate of text content in a PDF file (and much more). TET has a commandline interface, and it's the most powerful of all text extraction tools I'm aware of. (It can even handle ligatures...) Quote from their website:

Geometry
TET provides precise metrics for the text, such as the position on the page, glyph widths, and text direction. Specific areas on the page can be excluded or included in the text extraction, e.g. to ignore headers and footers or margins.

In my experience, while it's does not sport the most straight-forward CLI interface you can imagine: after you got used to it, it will do what it promises to do, for most PDFs you throw towards it...


And there are even more options:

  1. podofotxtextract (CLI tool) from the PoDoFo project (Open Source)
  2. calibre (normally a GUI program to handle eBooks, Open Source) has a commandline option that can extract text from PDFs
  3. AbiWord (a GUI word processor, Open Source) can import PDFs and save its files as .txt: abiword --to=txt --to-name=output.txt input.pdf
Arlettearley answered 31/5, 2011 at 14:55 Comment(7)
Which version of ghostscript is needed for using txtwrite device ?Tolentino
In what regard is the first option "not so good" and the second "better"?Hospitable
@musiphil: at the time of writing the above, the 2nd option gave more for fine control over output. Meanwhile, the txtwrite device has acquired a new feature (current GS version is 9.06): you can add as parameter -dTextFormat=0 | 1 | 2 | 3 (default is 3). See the respective Ghostscript documentation for details. I only know of these, but I haven't tested them yet intensively, so I do not (yet) have an updated personal opinion comparing the two options with the current GS version...Arlettearley
Hello, I would like to remove / delete all images from a pdf so that pdf will contain only the text. is it possible? kindly comment. ThanksSpout
@codin: Comments are not for discussing an entirely new topic. Please ask a new question, tag it as [ghostscript] + [pdf] and I'll try to answer it as best as I can. Please also state what's the purpose of your request. Saving on filesize? Remove info contained in the images? Or?Arlettearley
Hi for sleepy people like me, to output a pdf to a text file using pdftotext instead just open a windows console and navigate where your pdftotext.exe is then type: pdftotext yourpdffilepath.pdf and will create the text file.Jelsma
Tried the free options (1-4) and they all suffer from the same problem being that any character mapping other than a default mapping is left as some filler charter, be it a blank, dot or open box. Thanks but not what I need.Scilla
C
1

I'm not sure GhostScript can accept coordinates, but you can convert the PDF to a image and send it to an OCR engine either as a subimage cropped from the given coordinates or as the whole image along with the coordinates. Some OCR API accepts a rectangle parameter to narrow the region for OCR.

Look at VietOCR for a working example, which uses Tesseract as its OCR engine and GhostScript as PDF-to-image converter.

Chloric answered 3/9, 2011 at 15:34 Comment(1)
If the text is stored in the PDF file as text, rather than as an image, this would be a very bad idea.Penchant
N
1

Debenu Quick PDF Library can extract text from a defined area on a page. The SetTextExtractionArea function lets you specify the x and y coordinates and then you can also specify the width and height of the area.

  • Left = The horizontal coordinate of the left edge of the area
  • Top = The vertical coordinate of the top edge of the area
  • Width = The width of the area
  • Height = The height of the area

Then the GetPageText function can be called immediately after this to extract the text from that defined area.

Here's an example using C# (though the library is multi-platform and can be used with many different programming languages):

DPL.LoadFromFile(@"Sample.pdf", "");
DPL.SetOrigin(1); // Sets 0,0 coordinate position to top left of page, default is bottom left
DPL.SetTextExtractionArea(35, 35, 229, 30); // Left, Top, Width, Height
string ExtractedContent = DPL.GetPageText(8);
Console.WriteLine(ExtractedContent);

Using GetPageText it is also possible to return just the text located in that area or the text located in that area as well as information about the text's font such as name, color and size.

Noami answered 5/8, 2015 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.