#1095819 pdftk-java: (dataloss) PDF layers are flattened; and made visible even with false default visibility

#1095819#5
Date:
2025-02-12 11:51:03 UTC
From:
To:
Some PDFs contain OCG layers, where some content is assigned to a
layer that users can toggle the visibility of. For LaTeX users the
ocgx2 package provides this functionality.

PDFtk loses the layering information. All layers are flattened into
one, even layers with a default visibility is set to false.

Instead of attaching a sample PDF, I will reference a PDF from Debian:

  /usr/share/texmf/doc/context/presentations/examples/present-punk-001.pdf

That file is included with the /context/ package, which texlive-full
depends on. If you open it in evince, layers are detected and evince
gives a layers tab in the sidebar. In that tab you can see these
layers:

  step:1
  step:2
  step:3
  step:4

They are all invisible by default. Clicking next to each step makes a
layer visible. You can see the effect if the 2nd page is in view.

Running this command ruins the presentation:

  $ pdftk /usr/share/texmf/doc/context/presentations/examples/present-punk-001.pdf cat 1-r1 output /tmp/flattened.pdf

Loading /tmp/flattened.pdf shows the layers are gone. A CLI way to verify this:

===8<----------------------------------------
$ gdalinfo /usr/share/texmf/doc/context/presentations/examples/present-punk-001.pdf -mdd LAYERS | grep -iE "(layer.*name|^Files)"
Files: /usr/share/texmf/doc/context/presentations/examples/present-punk-001.pdf
  LAYER_00_NAME=step:1
  LAYER_01_NAME=step:2
  LAYER_02_NAME=step:3
  LAYER_03_NAME=step:4

$ gdalinfo /tmp/flattened.pdf -mdd LAYERS | grep -iE "(layer.*name|^Files)"
Files: /tmp/flattened.pdf
===8<----------------------------------------

FWIW, if you want to find other files on your system which contain OCG
layers, this command may be useful:

===8<----------------------------------------
  $ find /usr/share -iname \*.pdf -exec sh -c 'for f; do gdalinfo "$f" -mdd LAYERS; done | grep -iE "(layer.*name|^Files)"' _ {} +
===8<----------------------------------------

# This is a BUG, not a feature request:

Superficially this bug report might be perceived as a wishlist
proposal to support layers. However, I’m exposing a *bug* here. There
is data loss and it is invisible to users. Users are exposed to the
risk of deleting a source PDF without realising the output PDF was
unwittingly/surreptitiously altered in this way.

Adding seamless support for OCG layers would of course be ideal but
short of that, in the very least, there should be a LIMITATIONS
section added to the man page informing users of the treatment of OCG
layers. And when a PDF with layers is fed to pdftk as input, there
should be a loud warning printed to the screen about the lossy
output. FWIW, the following JavaScript may be useful for detection of
layers (if not gdalinfo):

https://web.archive.org/web/20230330010541/https://acrobatusers.com/tutorials/how-do-i-check-layers-comments-and-forms-collection-pdf-files/