Challenges with Extracting and Reapplying Specific Layers from Multi-layer PDFs using PyMuPDF #4091
Replies: 2 comments 4 replies
-
Well thought approaches! I see no way to achieve what you want. Drawing with the The only thing that may come close to your intention is setting (combinations of) OCGs permanently active and then save the PDF in that state. |
Beta Was this translation helpful? Give feedback.
-
I have tried following code snippet to extract drawing elements from a PDF page:
This code outputs objects with details like this:
These entries have the type clip, and their structure includes paths (e.g., l for line, c for cubic Bézier curves), scissor rectangles, and other attributes. My goal is to determine if these are clipping paths.
Any guidance or examples on how to apply these paths during a redraw process would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I’m working with a multi-layer PDF file, and I need to extract and reapply content from a specific layer (OCG) into a new PDF. Here's what I want to achieve:
I have tried three different approaches to achieve this, but I am encountering issues in each of them. Here’s an overview of the approaches and the problems I’m facing:
Approach 1: Copy Desired Objects and Paste into a New PDF with a Layer
Problem:
I attempted to copy only the desired objects (vectors) from the specified layer and paste them into a new PDF with a new layer. However, I am unable to selectively copy vector objects.
Approach 2: Copy All Content and Remove Unwanted Objects, Then Paste into a Layer
Problem:
In this approach, I tried copying all content from the original PDF using show_pdf_page and then removing unwanted objects by using methods like add_redact_annot() and apply_redactions(). While this approach successfully copied and pasted everything into a new file, it merged all the layer data into the root of the new PDF. When I tried removing unwanted layers, the method also deleted objects I wanted to keep, since some objects are present in overlapping areas across different layers. This caused the unwanted removal of content that was supposed to remain.
Approach 3: Copy Drawings and Redraw on a New Layer
Problem:
In the third approach, I copied the drawings (vectors) and redrew them on a new layer. While I successfully extracted and redrew the vector graphics from the target layer, this method also ended up redrawing areas that had been clipped out in the original file. I couldn’t figure out how to identify or preserve the clipping path, and thus, the redrawn content included areas that were supposed to be clipped. As a result, I ended up with content that didn’t match the original layout.
Questions:
Thank you for any guidance or suggestions!
Beta Was this translation helpful? Give feedback.
All reactions