Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need way to change font size for text inserted into existing field #727

Closed
duffyd opened this issue Dec 31, 2020 · 11 comments
Closed

Need way to change font size for text inserted into existing field #727

duffyd opened this issue Dec 31, 2020 · 11 comments

Comments

@duffyd
Copy link
Contributor

duffyd commented Dec 31, 2020

  • We're using the PDF-LIB for filling PDFs (https://pdf-lib.js.org/docs/api/classes/pdftextfield). It is likely not relevant but we also embed a font for characters outside of the winansi set (Latin)
  • On Nov 23rd, someone asked the author (Hopding) if there is a way to change the font size used. He said, "There are not currently any APIs for changing the font size of an existing field. But such an API could certainly be built. Happy to provide some pointers if anybody is interested in making a PR for it."
  • This issue is the formal request for pointers :)
@btecu
Copy link
Contributor

btecu commented Dec 31, 2020

Look at the code that retrieves the font size:

// Examples:
// `/Helv 12 Tf` -> ['Helv', '12']
// `/HeBo 8.00 Tf` -> ['HeBo', '8.00']
const tfRegex = /\/([^\0\t\n\f\r\ ]+)[\0\t\n\f\r\ ]+(\d*\.\d+|\d+)[\0\t\n\f\r\ ]+Tf/;
const getDefaultFontSize = (field: {
getDefaultAppearance(): string | undefined;
}) => {
const da = field.getDefaultAppearance() ?? '';
const daMatch = findLastMatch(da, tfRegex) ?? [];
const defaultFontSize = Number(daMatch[2]);
return isFinite(defaultFontSize) ? defaultFontSize : undefined;
};

You just have to reverse the process. The DA (default appearance) is a string (or HEX) containing the font name, size and a few possible other details.

@duffyd
Copy link
Contributor Author

duffyd commented Jan 1, 2021

@btecu thanks for your reply! So just to clarify, should I be setting the DA property (ref here) on the PDF field to a string representing what I want it to look like before doing a getTextField?

@btecu
Copy link
Contributor

btecu commented Jan 1, 2021

Yes, you can use getDefaultAppearance, modify the string to reflect the font size that you want and then use setDefaultAppearance:

setDefaultAppearance(appearance: string) {
this.dict.set(PDFName.of('DA'), PDFString.of(appearance));
}

@duffyd
Copy link
Contributor Author

duffyd commented Jan 1, 2021

@btecu: Great, thanks again!!

@Hopding
Copy link
Owner

Hopding commented Jan 2, 2021

@duffyd Just want to mention that what @btecu said is accurate.

Are you planning to make a PR that introduces an API for changing a text field's font size? If so, note that the DA property is optional and may not have a font size already defined in it. So you'll need to handle the case of a text field having a DA with a font size defined and the case where it does not have a font size defined (and the case of there being no DA property at all).

If you're not planning to make a PR, then I'll go ahead and close this issue.

@duffyd
Copy link
Contributor Author

duffyd commented Jan 2, 2021

Hi @Hopding, I'm happy to do a PR but just concerned whether you require some tests as well? Just I've never done testcases/etc in JS before. JS coding is fine, though.

@Hopding
Copy link
Owner

Hopding commented Jan 5, 2021

@duffyd There will need to be tests, but we can work through those once you finish the code. I can help you on the tests if necessary. They're not too hard to write. Note that some of them will need to be integration tests, which are just run-of-the-mill scripts (e.g. test8.ts). But again, I can help you with those.

Please be sure to read https://github.com/Hopding/pdf-lib/blob/master/CONTRIBUTING.md before you get started.

@duffyd
Copy link
Contributor Author

duffyd commented Jan 15, 2021

@Hopding I've finished writing the code and have also successfully got an integration test running. How should I create a PR? Fork project into my own GitHub repo and then create a PR or create a fork in your repo?

@duffyd
Copy link
Contributor Author

duffyd commented Jan 23, 2021

@Hopding just reiterating my comment in my PR - all requested changes have been implemented and are in my fork awaiting approval.

@Hopding
Copy link
Owner

Hopding commented Jan 27, 2021

Support for this added in v1.16.0

@yaakovyitzchak
Copy link

BH
Hi i have a form text field in about 4 different pages of the pdf and the above method only changes the font size on some but not in all cases?

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

No branches or pull requests

4 participants