-
|
I'm new to this package, and I've figured out how to toggle layers on/off: import pymupdfd
doc = pymupdf.open("file.pdf")
ogcs = doc.get_ocgs()
lyr_xref = ogcs[0]
doc.set_layer(-1, on=[lyr_xref], basestate="OFF") # turn off all but the first layerBut how do I delete them, or copy a specific layer to a new file? |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 6 replies
-
|
This is no issue, but a Discussions item or even better a post on our Forum. |
Beta Was this translation helpful? Give feedback.
-
|
Cool, thank you for moving this to the right place. I also created a topic in the forum: https://forum.mupdf.com/t/how-do-i-remove-layers-or-copy-a-layer-to-a-new-file/183 |
Beta Was this translation helpful? Give feedback.
-
|
Why do you even want to delete a layer? There is no associated file size reduction after all. Whether or not a PDF takes note of an OCG (Optional Content Group - and that exactly is what the term "layer" means) exclusively depends on whether or not its xref is present in the PDF catalog array There is no way to "copy a layer" between PDFs: there seems to be a general misconception here: |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the suggestions! I'll check them out. For context: my use case is wrangling PDF exports from CAD files -- which preserve CAD layers on the OCG objects/layers -- and needing to convert back to CAD. The reason I want to delete layers is because I will need to convert PDF to DXF/DWG, and the solutions I've tested so far don't respect the on/off settings. Essentially I'm trying to isolate layers for downstream use. For example, this file: building001-0_floor1.pdf How would you create a new PDF only containing the layer named |
Beta Was this translation helpful? Give feedback.
-
|
Other users visiting this page may want to look at this post in the Forum. |
Beta Was this translation helpful? Give feedback.
-
|
@JorjMcKie Thank you for the quick responses! Now I appreciate that "deleting" the OCG isn't the way to go because removing the xref doesn't remove the data being referenced. (Correct?) Perhaps I'm asking the question in the wrong way. In Adobe Acrobat my desired result can be achieved by turning off the unwanted layers then doing
Which results in this file: building001-0_floor1_discarded.pdf This removes the underlying vector graphics and text in the
Is there a way to do this in |
Beta Was this translation helpful? Give feedback.
-
Even if we were able to delete undesired OCGs (and in fact I found a way to do something equivalent) the effect would be that those PDF objects that referenced a now deleted OCG become unconditionally visible! Please also consider that the referencing objects (the ones I don't want to see anymore) may be referencing more than one OCG at the same time, because it is possible to combine two or more OCGs with arbitrary logical expressions like "OR" / "NOT" / "AND" ... the full boolean algebra is available here. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for the detailed feedback and explaining to me how this works!
Would you be able to share that method?
Here's a comparison of your
The bottom left result is clearly a glitch in InkScape's internal conversion of the PDF to SVG, however, my point is that it appears if we can remove the OFF data entirely and preserve the ON data then we can avoid downstream problems like this. As I mentioned above, Acrobat does this with the
Thus my question above:
I've tried variations of |
Beta Was this translation helpful? Give feedback.





A clear NO to your question. Our output is made correctly and authoritative PDF viewers accept the created PDF and display it as they should.
"Downstream problems" should be addressed by those who are causing them in the first pace. The problem clearly is in InkScape and not on our side. They are the ones who owe their users a problem fix. And I encourage you to submit a bug report to them.
The Adobe feature "Discard hidden layer content and flatten visible layers" is not for free. It's just a lucky coincidence that this optimization feature avoids a situation where InkScape fails.