Closed Sjoerd82 closed 3 years ago
Having the same issue wit the new form-filling API as well
https://github.com/Hopding/pdf-lib/pull/983 might help here. If not, please fill out the new bug report form and we can pick things up there.
You can also set form fields font size to Auto with JavaScript
pdfDoc.addJavaScript("customJS", "for(var i = 0; i < this.numFields; i++) { this.getField(this.getNthFieldName(i)).textSize = 'Auto' }" )
@Hopding I don't think #983 covers it since it sets the fontsize based on the field size. However, Acrobat has a way to set the font size to Auto
, which will set the font size based on the content.
One work around is to avoid the assertPositive by calling
// @ts-expect-error
textField.acroField.setFontSize('Auto')
I'm having some trouble using the workaround provided by @zacwellmer.
It seems that the font size set to auto that way is not propagated to the PDF and it ends up still being overwritten with a numeric font size 😕
I've tried marking the fields as dirty after setting the size but that didn't seem to make a difference. I even tried setting the DA string with the Auto value in there following the suggestions from this comment but the problem persisted.
@Hopding Could you reopen this issue?
@Dude29
I'll start by apologizing for my English, but I imagine that what could solve your problem with dynamically sized text would be the solution I found, I defined textField.acroField.setFontSize(0)
, the text is being changed based on the number of characters, However, if the text is too short and the textField is large, the text will be too large and the opposite also happens, this solution solved my problem, I hope it helps
@Dude29
Issue We have had an issue where we have PDFs with multiple of the same named field in a form all set to auto font size, some fields have less room, some have more, so the filled size should vary, but pdf-lib was running the auto size logic once and setting all widgets of the field to the same font size.
Solution
Comment out the call to updateDefaultAppearance in src/api/form/appearances.ts
around line number 487:
if (widgetColor || widgetFontSize !== undefined) {
updateDefaultAppearance(widget, textColor, font, fontSize);
} else {
// updateDefaultAppearance(textField.acroField, textColor, font, fontSize);
}
This prevents the source textField from having its font size set, allowing all display widgets to get autosize logic, this works for me, but could break something for you, good luck.
I'm having the same issue as @Dude29, and unfortunately @Filipe-QuickFlip 's answer doesn't solve it.
Setting textField.acroField.setFontSize(0)
leads to the size of the font to change at PDF creation time, but it is set to a number still, not to "Auto". The only way I've managed to achieve a setting of "Auto" so far has been following @khanhubgit 's suggestion.
@Hopding is there a way to achieve this without runtime javascript? guidance would be much appreciated, or if you see this as a bug I'm happy to open a bug report as well.
In recent versions of pdf-lib we now have the form-filling API, no longer do we need to work directly with those AcroForms. As mentioned in #48:
However, at this point we may seem have lost the ability to set the font size to Auto? When I now use setText(), it reverts the size from auto (as specified in the PDF) to 12. Preventing the text to scale down. Is there a solution for this?