Openpdf: PdfTextExtractor.getTextFromPage seems to add spaces between words

Created on 16 Nov 2018  路  5Comments  路  Source: LibrePDF/OpenPDF

I have a pdf with text content 'my text with simple spaces between the words'. If I read the content of the same pdf with PdfTextExtractor.getTextFromPage(), I get 'my text with simple spaces between the words' (two spaces between the words).

JUnit Test that shows this behaviour:

`

public void testPdfTextExtractor() throws Exception {

    String contentText = "my text with simple spaces between the words";
    File myPdf = new File("example.pdf");
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(myPdf));

    document.open();
    Chunk chunk = new Chunk(contentText);
    document.add(chunk);
    document.close();
    byte[] fileContent = Files.readAllBytes(myPdf.toPath());

    PdfReader pdfReader = new PdfReader(fileContent);
    PdfTextExtractor pdfTextExtractor = new PdfTextExtractor(pdfReader, false);
    String actualText = pdfTextExtractor.getTextFromPage(1);
    assertEquals(contentText, actualText);

    // gives the following failure:
    // org.junit.ComparisonFailure:
    // Expected :my text with simple spaces between the words
    // Actual   :my  text  with  simple  spaces  between  the  words
}

`

bug

Most helpful comment

I looked at my git repo, and I see that I made a fix to this, which has never been pushed. The modified version will no longer add spaces to any text object that contains spaces.

I'm afraid it will need some work, as I haven't rebased against master in quite a while. I will try to put in a little time in on this in the next month, and get it posted.

The extra spaces will not exist in some files, anyway (probably mostly older ones).

All 5 comments

I believe this is because the extractor adds spaces because they are not dependably present between words in all PDF files. The extra space could be made conditional on an identified word not already ending in an "extracted space", but I'm not sure that that level of consistency makes complete sense. The extractor certainly does have to add spaces not present in the source, since some files don't have whitespace.

I'll assign it to myself as the originator of the rewritten extraction code, but I'm not sure that it's a high priority, given how much variability can be expected from extracted text. My main design points in the extraction were some accuracy in region determination, and ability of the resulting text to be fed into a full-text indexer.

Hello, I am using the pdfTextExtractor and I am experiencing the same as there are two spaces between spaces. Is the issue being worked on o it is not a priority? Thank you in advance.

I looked at my git repo, and I see that I made a fix to this, which has never been pushed. The modified version will no longer add spaces to any text object that contains spaces.

I'm afraid it will need some work, as I haven't rebased against master in quite a while. I will try to put in a little time in on this in the next month, and get it posted.

The extra spaces will not exist in some files, anyway (probably mostly older ones).

Hi,
How can i get text height my pdf pages. Not all page size. Only text height. When i use iText, this code do this.

PdfReaderContentParser parser = new PdfReaderContentParser(pdfReader); TextMarginFinder finder = parser.processContent(pageNumber, new TextMarginFinder()); try { return finder.getLly(); } catch (Exception e) { return 0; }

@daviddurand any ETA for this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bberto picture bberto  路  7Comments

jjkarppi picture jjkarppi  路  7Comments

literakl picture literakl  路  6Comments

bbottema picture bbottema  路  5Comments

michaelhugi picture michaelhugi  路  6Comments