Skip to content

Conversation

svd-sea
Copy link

@svd-sea svd-sea commented Jun 23, 2025

This is a proposal for the implementation of optional content groups (PDF layers: https://helpx.adobe.com/acrobat/using/pdf-layers.html).

The configuration can be created by PDFDocumentProxy.getOptionalContentConfig and will then be used by PDFPageProxy.render.

pdf.js reference -> mozilla/pdf.js#269 (comment)

@wojtekmaj
Copy link
Owner

Hey! Excellent idea. Couple of questions:

  • Would it make sense to add it to Document and pass as childContext so that all pages could consume it, much like scale and rotate props, or it only makes sense to be passed to each page individually?
  • Can we somehow unit test it?

…cument components, removing them from page props
@svd-sea
Copy link
Author

svd-sea commented Jun 27, 2025

Hi wojtekmaj,

good point, since optional content groups are created by the PDFDocumentProxy and affect all pages, it is much better to move this property to Document.

I have added positive and negative render tests for the page with optionalContentConfig and tests for the document.

@wojtekmaj wojtekmaj force-pushed the main branch 3 times, most recently from a5dac89 to 30d9456 Compare June 27, 2025 11:45
@wojtekmaj
Copy link
Owner

This is a work of art 🤩 I have one final concern that I think we should address to make this feature really useful. How we could trigger layer visibility change? Currently, even if you set the value, without forcing the entire document to reload by e.g. changing component keys, the layer visibility will remain unchanged.

If this can be easily solved (I looked through PDF.js source quickly and didn't find it), then cool, if not - perhaps we should think about a wrapper exposing some methods from optionalContentConfig and calling additional methods instead.

@svd-sea
Copy link
Author

svd-sea commented Jul 1, 2025

Hmm pdf.js uses this function in their viewer, but they change the configuration directly and trigger the new rendering via their own event bus. So this solution is not suitable for our situation.

Any idea how to best approach building a wrapper for this? My current idea would be a RefObject, wrapping the configurations functionality. Initialized in the document while loading the document (similar to the link service). which is then passed to the page and can be used by the page via onStateChange/onVisibilityChange callback and from outside via a Document ref. This way we would not even have the need to load and pass the configuration from outside, as it could be loaded with the document.

@wojtekmaj
Copy link
Owner

wojtekmaj commented Jul 9, 2025

Check out #2004. I think we can somehow make it work :)

@wojtekmaj
Copy link
Owner

Hmmm, doesn't look like this changes much, just adding EventBus doesn't magically cause the PDF to rerender, but it's not surprising. I guess another approach would be to expose optionalContentConfig using our own wrapper that gets the entire config and allows to set it, in a more React-y way.

svd-sea added 3 commits July 9, 2025 14:56
…cument and Page components, replacing optionalContentConfig usage. Update tests accordingly.
…essages. Improve readability in OptionalContentService and related components.
@svd-sea
Copy link
Author

svd-sea commented Jul 10, 2025

Okay, I have created a first design for the OptionalContentConfig wrapper (OptionalContentService). The rerender is now triggered by the Canvas Component, which is attached to a listener and the page test also covers the rerendering after layer visibility change.

What do you think? :)

svd-sea added 3 commits July 21, 2025 16:03
# Conflicts:
#	packages/react-pdf/src/Document.spec.tsx
#	packages/react-pdf/src/Document.tsx
#	packages/react-pdf/src/shared/types.ts
# Conflicts:
#	packages/react-pdf/src/Document.spec.tsx
#	packages/react-pdf/src/Page.spec.tsx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants