Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Font size of PDFForm field being redefined #1196

Answered by bcholmes
bcholmes asked this question in Q&A
Discussion options

Hey!

I have various PDF files that I created in Adobe products. I've defined some form fields in Acrobat. I'm then grabbing a PDF and loading it into pdf-lib in my app and filling various fields.

In one of my PDF files, I've included some text fields that are tall (in height), defined as multiline fields and given a fixed font size. My app does not fill those fields (the intention is that the user that receives the PDF can fill those fields manually). But after going through the pdf-lib processing, the font size for those multiline fields is really big (like, ~100). I've tried to use PDFTextField.setFontSize() to force the fields back to a reasonable size, but that's failed to improve the output.

I can see that there's some code in layoutMultilineText in api/text/layout.ts which is used by api/form/appearances.ts: this code appears to be trying to change the font size based on the size of the text in the field (in my case, there's no text in the field). But I don't want that. Is there a way to prevent that code from being executed on my fields? I don't really understand appearances, so I'm not sure if there's a way to use a "leave everything the way you found it" appearance.

You must be logged in to vote

New idea: if I manually run the updateAppearances() method on the text fields, I can pass in an alternative implementation of the appearanace provider (one that does not behave the same w.r.t. multiline fields), then we should be able to mark those fields as clean, and therefore the updateFieldAppearances() method shouldn't try to re-update them (with the default appearance provider).

Replies: 2 comments · 4 replies

Comment options

Some findings after scouring through some of the code. I can't be sure that the appearance.ts file is a culprit, but I've done some looking in to the calls, and here's the basic call hierarchy.

Overall, the PDFDocument.updateFieldAppearances() call is dependant on a flag on the save call; if that flag is set to false, then the code that I question is not invoked. My concern is that setting that flag to false will prevent more straight-forward text fields from adjusting their text sizes.

But it's testable, and if my concern proves true, I might have some options to emulate the save behaviour with some changes (although privacy might thwart me).

You must be logged in to vote
1 reply
@bcholmes
Comment options

Yeah, that's even worse than I expected. It appears that if I pass in a updateFieldAppearances flag of false, then the text fields don't really show up at all (at least in macOS's Preview app). If I put my cursor into a text field, then the text appears, but otherwise it's not visible.

Comment options

New idea: if I manually run the updateAppearances() method on the text fields, I can pass in an alternative implementation of the appearanace provider (one that does not behave the same w.r.t. multiline fields), then we should be able to mark those fields as clean, and therefore the updateFieldAppearances() method shouldn't try to re-update them (with the default appearance provider).

You must be logged in to vote
3 replies
@bcholmes
Comment options

That... seems to have worked. The code's a bit more cut-and-paste-y than I'd prefer, but this'll do for now.

@Trekiros
Comment options

That... seems to have worked. The code's a bit more cut-and-paste-y than I'd prefer, but this'll do for now.

Hi,
Do you happen to still have a code sample with this solution on hand? I have the same issue but haven't been able to reproduce your solution.
Thank you

@bcholmes
Comment options

Here's the code that I plonked into my codebase: https://github.com/bcholmes/StarTrek2d20/blob/sta-complete/WebTools/src/helpers/pdfTextFieldAppearance.ts

In my PDF-filling code, I'd run a piece of code like this:

import { staTextFieldAppearanceProvider } from "../helpers/pdfTextFieldAppearance";

form.getFields().forEach(f => {
    if (f instanceof PDFTextField) {
         let textField = f as PDFTextField;
         if (textField.isMultiline() && (textField.getText() == null || textField.getText().length === 0)) {
             textField.updateAppearances(this.formFont, staTextFieldAppearanceProvider(8));
         }
     }
 });

That'd force the font size to be 8 for the field in question.

Answer selected by bcholmes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.