Attaching drawings to an OCG #2807
-
Hi all I have the following task at hand: I want to optionally display all the drawings (vector graphics and svg-like objects) present in a PDF. For this I would like to attach the drawings on the page to an OCG (Optional Content Group). I am aware that this is possible when you create a drawing/shape yourself (for example: With this in mind: How can I toggle the visibility of exisiting drawings using an OCG/OCMD? Is it even possible utilizing PyMuPDF or is another approach (perhaps low-level) required? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Congratulations for a clearly described problem situation. Doesn't happen often ... 😒. Your analysis is correct. No way to make a drawing OCG-eligible afterwards.
The code for finding that bbox is here. |
Beta Was this translation helpful? Give feedback.
-
This one of your reported results: page.get_contents() # Output = [16, 17, 18]
page.clean_contents(sanitize=True)
page.get_contents() # Output =[16, 17, 18] should not occur. I remember we have had an issue in PyMuPDF several versions ago - so maybe you still are on it. Otherwise - hacking content streams is of course possible if you are willing to follow the white rabbit ... In [4]: print(doc.xref_object(page.xref))
<<
/Type /Page
/MediaBox [ 0 0 500 400 ]
/Rotate 0
/Resources <<
/Properties << % references to OCGs in the contents:
/MC0 6 0 R % this one controls the gray rectangle
/MC1 4 0 R
/MC2 5 0 R
>>
/Font <<
/helv 7 0 R
>>
>>
/Parent 2 0 R
/Contents 8 0 R
>> This is the start of the contents stream: In [5]: cont=page.read_contents().decode()
In [6]: len(cont)
Out[6]: 2437
In [7]: print(cont[:500])
q
[3 1] 0 d
/OC /MC0 BDC
45 95 360 260 re
h
.8 .8 .8 rg
0 0 1 RG
B
EMC
Q
q
/OC /MC1 BDC
BT
/helv 12 Tf
1 0 0 1 50 337.1 Tm
[(The false killer whale \(Pseudorca crassidens\) is a species of)] TJ
1 0 0 1 50 319.375 Tm The rectangle's OCG wraps the drawings command in strings In [8]: print(doc.xref_object(6))
<<
/Type /OCG
/Name (graphic)
/Intent [ /View ]
/Usage <<
/CreatorInfo <<
/Creator (PyMuPDF)
/Subtype /Artwork
>>
>>
>> If you are confident to locate the commands for some vector graphics, you could put similar |
Beta Was this translation helpful? Give feedback.
Congratulations for a clearly described problem situation. Doesn't happen often ... 😒.
Your analysis is correct. No way to make a drawing OCG-eligible afterwards.
What I can think of:
The code for finding that bbox is here.