14

We are doing research in information extraction, and we would like to use iText.

We are on the process of exploring iText. According to the literature we have reviewed, iText is the best tool to use. Is it possible to extract text from pdf per line in iText? I have read a question post here in stackoverflow related to mine but it just read text not to extract it. Can anyone help me with my problem? Thank you.

1
  • 3
    I'm not completely clear on what you are doing. Reading text and extracting text are generally the same thing. iText won't save the text to a file for you but once you have the text you should be able to do that fairly easily. iText does a really great job of extracting text as long as it is actually text (not outlines or bitmaps). When searching this site also look for iTextSharp which is the .Net port of iText. It has more questions/answers and the code is almost completely the same for C#.
    – Chris Haas
    Commented Jan 11, 2012 at 19:01

4 Answers 4

21

Like Theodore said you can extract text from a pdf and like Chris pointed out

as long as it is actually text (not outlines or bitmaps)

Best thing to do is buy Bruno Lowagie's book Itext in action. In the second edition chapter 15 covers extracting text.

But you can look at his site for examples. http://itextpdf.com/examples/iia.php?id=279

And you can parse it to create a plain txt file. Here is a code example:

/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */

package part4.chapter15;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

public class ExtractPageContent {

    /** The original PDF that will be parsed. */
    public static final String PREFACE = "resources/pdfs/preface.pdf";
    /** The resulting text file. */
    public static final String RESULT = "results/part4/chapter15/preface.txt";

    /**
     * Parses a PDF to a plain text file.
     * @param pdf the original PDF
     * @param txt the resulting text
     * @throws IOException
     */
    public void parsePdf(String pdf, String txt) throws IOException {
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        PrintWriter out = new PrintWriter(new FileOutputStream(txt));
        TextExtractionStrategy strategy;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            out.println(strategy.getResultantText());
        }
        reader.close();
        out.flush();
        out.close();
    }

    /**
     * Main method.
     * @param    args    no arguments needed
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        new ExtractPageContent().parsePdf(PREFACE, RESULT);
    }
}

Notice the license

This example only works with the AGPL version of iText.

If you look at the other examples it will show how to leave out parts of the text or how to extract parts of the pdf.

Hope it helps.

1
  • The program shown works like a charm, without a single modification! Thanks to Bruno and thanks to you for pointing it out.
    – gsl
    Commented Aug 18, 2020 at 16:10
4

iText allows you to do that, but there is no guarantee about the granularity of the text blocks, those depend on the actual pdf renderers used in producing your documents.

It's quite possible that each word or even letter has its own text block. Nor do these need to be in lexical order, for reliable results you may have to reorder text blocks based on their coordinates. Also you may have to calculate if you need to insert spaces between textblocks.

3

In newer versions of itext:

public static void main(String[] args) throws Exception {
    try (var document = new PdfDocument(new PdfReader("your.pdf"))) {
        var strategy = new SimpleTextExtractionStrategy();
        for (int i = 1; i <= document.getNumberOfPages(); i++) {
            String text = PdfTextExtractor.getTextFromPage(document.getPage(i), strategy);
            System.out.println(text);
        }
    }
}
1
  • 2
    I upvoted but there's a small mistake: it should either be int i = 0 and getPage(i+1) or i <= document.getNumberOfPages() I wanted to edit but got an exception from StackOverflow saying too many edits try again later... so please edit your reply and correct it. Cheers! Commented Jul 4, 2024 at 15:32
0

Q. Is it possible to extract text from pdf per line in iText?

A. PDF is based on a charting system (X,Y co-ordinates) Thus all PDF writers and reader's are based on "area by area". Such that the smallest single unit is in effect each page not the lines of text that can be in any angle or order.

Thus to be forced to work "Line-wise" there would need to be named functions or switches. Certainly iText could do that as per other answers. But for simplicity I propose in basic cases try PDFtoText first. That way you can pick from newly numbered lines.

So for the first 31 lines of this page.
pdftotext -layout -enc UTF-8 pdftext.pdf|find /v /n "uhhlfdhh" pdftext.txt

---------- PDFTEXT.TXT
[1]PDF text extraction using iText
[2]Asked 12 years, 7 months ago      Modified today       Viewed 54k times
[3]
[4]
[5]
[6]        We are doing research in information extraction, and we would like to use iText.
[7]
[8]        We are on the process of exploring iText. According to the literature we have reviewed, iText is
[9] 14
[10]        the best tool to use. Is it possible to extract text from pdf per line in iText? I have read a
[11]        question post here in stackoverflow related to mine but it just read text not to extract it. Can
[12]        anyone help me with my problem? Thank you.
[13]
[14]
[15]         itext   Edit tags
[16]
[17]
[18]
[19]        Share Edit Follow Close Flag                edited Jan 11, 2012 at 21:12            asked Jan 11, 2012 at 14:40
[20]                                                            Kieran                                  rogelie
[21]                                                            3,756    4    29    39                  141       1   1   3
[22]
[23]
[24]
[25]
[26]        3        I'm not completely clear on what you are doing. Reading text and extracting text are generally the
[27]                 same thing. iText won't save the text to a file for you but once you have the text you should be able
[28]                 to do that fairly easily. iText does a really great job of extracting text as long as it is actually text (not
[29]                 outlines or bitmaps). When searching this site also look for iTextSharp which is the .Net port of
[30]                 iText. It has more questions/answers and the code is almost completely the same for C#.
[31]                 – Chris Haas Jan 11, 2012 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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