Giter VIP home page Giter VIP logo

Comments (4)

JorjMcKie avatar JorjMcKie commented on June 23, 2024 1

For whatever reason, some of your text_instances rectangles have a width of 0 - this causes the division by zero exception.
So you can check / ignore such rectangles yourself.
But of course the code must also be immunized against such nonsense-input.

from pymupdf.

Flint-company avatar Flint-company commented on June 23, 2024

For whatever reason, some of your text_instances rectangles have a width of 0 - this causes the division by zero exception. So you can check / ignore such rectangles yourself. But of course the code must also be immunized against such nonsense-input.

Thanks !
This leads to another question...
The search_for method returns very odd placements (Rect) of words searched for (ie not at all the place where the word is located in the pdf...).
Is there something I can do to actually search for words and return the right spots or is it too difficult ?

from pymupdf.

JorjMcKie avatar JorjMcKie commented on June 23, 2024

The success of text searching is dependent on that text is indeed stored in a searchable way. That may not be the case at all! Even if a page looks utterly harmless.

For a demo look at these two files, file1, file2. Both look exactly equal - and in fact, every character is positioned at the exact same place in both of them.
But ...

doc1=pymupdf.open("textmaker.pdf")
doc2=pymupdf.open("textmaker2.pdf")
page1 = doc1[0]
page2 = doc2[0]
needle = "alignment."  # some text to search for
page1.search_for(needle)
[Rect(205.07913208007812, 158.3671875, 257.65399169921875, 172.76171875),
 Rect(91.0791244506836, 158.3671875, 143.65399169921875, 172.76171875)]
page2.search_for(needle)
[]

You will never find any word in file2, however, single characters are no problem.
The solution of this riddle: for demo purposes, characters have been deliberately written on the page in some random sequence.

Text searching contains no precaution to extract single characters and sort them in "natural reading sequence" (what is that anyway?) before finally searching for something. The performance of such an approach would make it unattractive.
So when searching, you implicitly trust that a page has been created in some reasonable way, that the PDF creator has been too lazy to make it difficult.
But there are exceptions: e.g. whenever you use redaction annotations to replace text, then the new text will be an example for out-of-sequence words / characters. This is inevitable with PDF.
We have to live with it.

from pymupdf.

Flint-company avatar Flint-company commented on June 23, 2024

Ok, I got it, very clear. Thanks !

from pymupdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.