No way to copy fields for printing
What were you trying to do?
I want to print only the filled forms of a document like https://acrobatusers.com/tutorials/how-do-i-print-only-form-field-data/
The easiest way would be to use a adobe pro license, since this feature seems to be only implemented in the paid version.
How did you attempt to do it?
I want to solve it with the pdf-lib only way:
- extract the form from the PDF
- add it to a new empty PDF object
- add the input and print it
const pdf = await this.loadPdf(templateEntry.fileId);
const doc = await PDFDocument.load(pdf);
const form = doc.getForm();
const newDoc = await PDFDocument.create();
const page = newDoc.addPage();
const newForm = newDoc.getForm();
for (const field of form.getFields()) {
for (const widget of field.acroField.getWidgets()) {
if (field.constructor.name === 'PDFTextField') {
const newField = newForm.createTextField(field.getName());
newField.setText(claim.values[field.getName()]);
newField.addToPage(page, {
...widget.getRectangle(),
borderWidth: 0,
}
newField.setFontSize(12); //static value :(
}
}
// print new page
newForm.flatten();
return newDoc.save();
This works when the fields are defined without any specify styling. As seen above I am able to set the fontsize, but I found no way to get it from the defined field to copy it like the coordinates. Besides other information like text align used fonts or other inputs like pdf button it can get a bit compicated.
Another approach would be to extract all elements from a PDF except the elements that belong to the form, but I found option to list and therefore remove them.
What actually happened?
There is no way to copy a field 1:1 to a new document.
What did you expect to happen?
Being able to copy it.
How can we reproduce the issue?
see the code above
Version
1.17.1
What environment are you running pdf-lib in?
Browser
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
No response