Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.8k stars 648 forks source link

Page translateContent does not work for form fields #1348

Open enoh-barbu opened 1 year ago

enoh-barbu commented 1 year ago

What were you trying to do?

I'm trying to scale a page which contains a form with different fields, text/checkbox etc

How did you attempt to do it?

page.scale(scale, scale); page.translateContent(translateX, translateY);

What actually happened?

The form fields are not translated as the rest of the page content.

What did you expect to happen?

The form fields should have been translated as well (X/Y)

How can we reproduce the issue?

page.scale(0.8, 0.8); page.translateContent(translateX, translateY); OoPdfFormExample.pdf

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

enoh-barbu commented 1 year ago

I have implemented a workaround solution

pdfDoc.getForm().getFields().forEach((field) => {
                page.node.Annots()?.asArray().forEach((annot) => {
                    if ((annot as PDFRef).objectNumber === field.acroField.ref.objectNumber) {
                        const widgets = field.acroField.getWidgets();
                        widgets.forEach((widget) => {
                            const rect = widget.getRectangle();
                            rect.x += translateX;
                            rect.y += translateY;
                            widget.setRectangle(rect);
                        });
                    }
                });
            });