Getting a Line Number From an Annotation #1197
-
Hey guys, I'm currently trying to make a change log app to help me organize the changes I need to make after receiving a report. To make my job easier, I'd also like to include the line number of the sentences I need to change/fix.
Thanks :) |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Not sure I understand: >>> doc=fitz.open()
>>> page=doc.new_page()
>>> a=page.add_text_annot((100,100), "line1\nline2")
>>> a
'Text' annotation on page 0 of <new PDF, doc# 1>
>>> a.info
{'content': 'line1\nline2', 'name': 'Note', 'title': '', 'creationDate': '', 'modDate': '', 'subject': '', 'id': 'fitz-A0'}
>>> a.info["content"]
'line1\nline2'
>>> print(a.info["content"])
line1
line2
>>> If the author did so, ok. If not, then there are none and the string may be arbitrarily long. |
Beta Was this translation helpful? Give feedback.
-
Ah, that's better. lines = []
for b in page.get_text("dict")["blocks"]:
if b["type"] != 0: continue
for l in b["lines"]:
lines.append(l)
# now sort the lines - they may be in arbitrary order!
lines.sort(key=lambda l: (l["bbox"][3], l["bbox"][0])) # ascending vertical, then horizontal
# for some annot do this:
for i, line in enumerate(lines):
if annot.rect in fitz.Rect(line["bbox"]):
print(annot, "contained in line %i" % i)
found = True
break
if not found:
print("annot not found") |
Beta Was this translation helpful? Give feedback.
-
almost correct: "otherwise, it's lines are added to the list. |
Beta Was this translation helpful? Give feedback.
-
I see. Thank you for your help. :) |
Beta Was this translation helpful? Give feedback.
Ah, that's better.
First, you need to get the bboxes of all lines on the page.
Second, take the bbox (annot.rect) of your annotation and look in which line's bbox it is contained. Some snippet like this: