Question / Comment: underline, strike-out and highlights that are not annotations
Hello
In this pdf is it possible to identify the texts that are underlined, strikethrough, and highlight?

Using page.annots() doesn't work, because they are not annotations, the PDF was saved with theses.
Is there a way to get those texts?
(I am using PyMuPDF 1.17.0)
No, unfortunately not. Not with reasonable effort at least. If you like adventures, you can write a program and try doing this - the data is there and accessible via PyMuPDF.
Let's take an example: the "highlights". They are probably, but not necessarily coded as rectangle drawings. In the PDF mini-language (somewhat comparable to PostScript) those are coded like
...
x0 y0 x1 y1 re % rectangle
1 1 0 rg % RGB fill color yellow
...
"re" means rectangle, the coordinates are preceeding the operator and (usually) counted bottom-up: PDF point (0,0) is bottom-left, not top-left. Following comlications may exists:
- you can also draw a rectangle by painting its 4 borders as simple lines
- arbitrary many matrices (and other commands) may precede the instruction, which might change the coordinate system (scaling, rotation, ...). You would have to keep track of all of them.
- the above 2 commands with their parameters may contain any number of spaces and / or linebreaks.
And so on. So would have to write something like a PDF interpreter (like every PDF viewer is one) to achieve something reliable / stable ...
If yu feel like it, look at the PDF manual "Appendix A Operator Summary", page 985.
The mentioned instructions are (usually - again not always!) found as a bytes object via page.readContents().
Here are the first 550 bytes of the /Contents object:
>>> doc=fitz.open("styled_text.pdf")
>>> page=doc[0]
>>> cont=page.readContents().decode() # make a string of it
>>> len(cont)
7793
>>> print(cont[:550])
0.1 w % set width
q 0 0 595.2 841.8 re % push to stack, define rectangle
W* n % set clipping path (even-odd rule), end path with no-op
q 0 0 0 rg % push to stack, set color RGB black
BT % begin text
56.8 773.9 Td /F1 12 Tf[<01>5<020304>-5<05>1<06>]TJ
ET % end text
Q % unstack
q 0 0 0 rg % etc, etc, etc
BT
127.7 773.9 Td /F2 12 Tf[<0102>8<03>-2<04>-2<0506>2<0708>-5<09>2<0506>2<08>-5<0A>1<07>]TJ
ET
Q
q 0 0 0 rg
BT
198.6 773.9 Td /F1 12 Tf[<01>5<020304>-5<05>1<06>2<0708090A0B>1<0306>-5<0C>2<090B>1<0A>]TJ
ET
Q
q 1 0 0 1 198.6 773.9 cm
0.7 w 0 0 0 RG
0 -1.1 m 91 -1.1 l S
Q
q 0 0 0 rg
BT
305 773.9 Td /F2 12 Tf[<010203>-2<04>-2<0506>2<0708>2<09>2<0506>-5<08>2<0A>-6<070B010C0D>10<03>-2<06>-5<08>2<010D>1<0C>]TJ
ET
Q
I should have mentioned that you can get a /Contents string which is syntactically cleaned and standardized: use page.cleanContents() before page.readContents(). The above output will then look like this:
.1 w
q
0 0 595.2 841.8 re
W*
n
q
0 0 0 rg
BT
56.8 773.9 Td
/F1 12 Tf
[<01>5<020304>-5<05>1<06>] TJ
ET
Q
q
0 0 0 rg
BT
127.7 773.9 Td
/F2 12 Tf
[<0102>8<03>-2<04>-2<0506>2<0708>-5(\t)2<0506>2(\b)-5(\n)1<07>] TJ
ET
Q
q
0 0 0 rg
BT
198.6 773.9 Td
/F1 12 Tf
[<01>5<020304>-5<05>1<06>2<0708090A0B>1<0306>-5(\f)2<090B>1(\n)] TJ
ET
Q
q
1 0 0 1 198.6 773.9 cm % matrix, repositions to Point(198.6, 842 - 773.9)(*)
.7 w % change line width to 0.7 points
0 0 0 RG % set stroke color to black
0 -1.1 m % goto this point (relative to position established by last matrix)
91 -1.1 l % draw line from there to (91, -1.1), should pertain to your text "Normal underlined"
S
Q
q
0 0 0 rg
BT
305 773.9 Td
/F2 12 Tf
[<010203>-2<04>-2<0506>2<0708>2(\t)2<0506>-5(\b)2(\n)-6<070B010C0D>10<03>-2<06>-5(\b)2<010D>1(\f)] TJ
ET
Q
This is easier to interpret, because every command is guarantied to be on a new line, multiple white spaces are replaced by one space, numbers are in a standard format, etc.
(*) If you want to invest the time, confirm whether
Point(198.6, 842 - 773.9)really is the start of "Normal underlined" in line 1. So, in this simple case, we have correlated some text to some "highlighting" element.
Hm... that's interesting! I was doing some experimentation. Here's what I was able to achieve:
First, to make things easier, I created this (much simpler) pdf:

Then:
page.cleanContents()
content = page.readContents().decode()
From this, I got (the \n are there because I simply prettyprinted this):
'.1 w\n'
'q\n'
'0 0 595.2 841.8 re\n'
'W*\n'
'n\n'
'q\n'
'0 0 0 rg\n'
'BT\n' % "xxx" text
'56.8 773.9 Td\n' % left = 56 | bottom = 773.9 (this would be bottom, right?)
'/F1 12 Tf\n'
'<01010102> Tj\n'
'ET\n'
'Q\n'
'q\n'
'0 0 0 rg\n'
'BT\n' % "underline underline" text
'77.8 773.9 Td\n' % left = 77.8 | bottom = 773.9
'/F1 12 Tf\n'
'[<03040506>1<0708>2(\\t)2<0406>-6<0203040506>1<0708>2(\\t)2<04>-7<06>] TJ\n'
'ET\n'
'Q\n'
'q\n'
'1 0 0 1 77.8 773.9 cm\n' % starts at left = 77.8 | bottom = 773.9
'.7 w\n'
'0 0 0 RG\n'
'0 -1.1 m\n'
'93.6 -1.1 l\n' % Relative to matrix position. Starts at left:77.8 and goes to left:(77.8+93.6= 171.4)
'S\n'
'Q\n'
'Q\n'
I think it is pretty clear that the underline for the underline underline text comes from this piece of code (i double checked this by creating a pdf with just one underlined word):
'q\n'
'1 0 0 1 77.8 773.9 cm\n' % starts at left = 77.8 | bottom = 773.9
'.7 w\n'
'0 0 0 RG\n'
'0 -1.1 m\n'
'93.6 -1.1 l\n' % Relative to matrix position. Starts at left:77.8 and goes to left:(77.8+93.6= 171.4)
'S\n'
'Q\n'
I guess we can assume that all text from left:77.8 bottom:773.9 to left:171.4 bottom:773.9 position are underlined. What do you think?
I was able to match this underline code easily with a regex:
underline_regex = r"q\n(.+) cm\n(.+) w\n(.+) RG\n(.+) m\n(.+) l\nS\nQ"
matches = re.finditer(underline_regex, content)
[<re.Match object; span=(203, 265), match='q\n1 0 0 1 77.8 773.9 cm\n.7 w\n0 0 0 RG\n0 -1.1 >]
Although I do realize that this approach might not be the best:
- there might be more than one way to code the underline, and my regex might fail in some cases.
- since my regex basically only considers the operator's order, it could match things that are not underlines. I pretend to work on improving this incrementally by testing it exhaustively.
But maybe this will do the trick for me right now (at least for underlines). With the position information from the matched code I would be able to link the underline style to the words on the page.
So I did:
(Pdb) pp page.getText("words")
[(56.79999923706055,
57.26800537109375,
74.80000305175781,
70.55706787109375,
'xxx', # not important
0,
0,
0),
(77.80000305175781, # x0 (aka left)
57.26800537109375, # y0 (aka top)
123.02616882324219, # x1 rigth
70.55706787109375, # y1 bottom
'underline',
0,
0,
1),
(126.08799743652344, # left
57.26800537109375,
171.39817810058594,
70.55706787109375, # bottom
'underline',
0,
0,
2)]
Since both underline words are in range left:77.8 to left:171.4 i would say they are underlined.
But why didn't bottom:773.9 value from the pdf content matched with pymu's y1 (or bottom)?
I guess that would solve it, for me. Thoughts?
Thanks for the awesome support.
I see you are a fearless adventurer ...
I would probably do a similar approach as you do. Go through the contetns from front to back, interpreting the matrices as encountered (watch out for "q" / "Q" command pairs: they represent a separate stack level each). Then put lines and rectangles in Python list.
The text can be gotten from either getText("words") or getText("dict").
Then match the two information bases.
But why didn't bottom:773.9 value from the pdf content matched with pymu's y1 (or bottom)?
I have seen this too. MuPDF always reflects the full bbox of text spans / words / characters (whatever is the reporting level). The bbox is a tiny bit larger than the visible representation. So better include some allowance for "inaccuracies" ... Especially also, because you do not in general know, how close to the text bottom an underline was drawn, etc.
Closing this for now. Please re-open whenever there are more questions.