When you define a font to be used by a field it automatically gets fully
embedded in the PDF.
JS has no access to the list of available fonts in the computer.
Sorry, but no, you are wrong.
This is indeed what I would have thought, but it is not correct.
If I set "textFont = "Garamond"", the form text gets displayed in Garamond, independently of the fact that the Garamond font is included in the PDF.
It might not be visible in the Fonts list, but it does get embedded. I just
tested it on a file I'm working on... I added a text field and set its text
font to Garamond and saved it under a new name. The new file has almost
exactly the same audit space usage stats as the old one, except the Fonts
usage jumped from 500kb to 1.3mb.
Thanks again for your answer.
If you create a form with Acrobat and set its font to Garamond, then indeed the font gets embedded.
But this was not my question...
If you do it in Acrobat (no matter if you do it manually or using a script) then the font will be embedded in the file the next time you save it.
If you use JS (or an external library) to set the font name to a font that is not available on the computer then my guess is that it will default to some default font (Helvetica or something like that) when the file is opened, or it will simply not work at all.
Any file that is installed locally has an "internal name". You can easily find it out by setting a field to have this font and then checking the value of its textFont property in the console. You can't get a list of all the available fonts and their internal names using a script, though, as I've said. It might be possible with a plugin, though.
Sorry, but this isn't helpful...
I am NOT asking:
- how to embed a font (this I know),
- what happens if textFont is set to a font that is not availabe (see my original post: "Acrobat uses a replacement font instead"),
- how to get the internal name of a locally installed font (this I know),
- how to get a list of all available fonts (I don't care).
What I am asking is "how to use an embedded font to set the textFont of a form".
That is, supposing that:
- a font (say Garamond) is NOT installed on the computer on which the PDF file is opened (or at least that I cannot suppose that it is), and
- that font IS embedded in the PDF file,
JS also can't know which fonts are embedded in the file, if that's what you're asking.
No, that's not what I'm asking. I know what fonts are embedded in the file (given that I am the creator of the file).
See the first sentence of my original post: "Is there a way to use one of the fonts that are embedded in the PDF as the textFont of a form?".
You cannot use a font that's embedded in the document but not currently in use with a form field. Even if you could somehow, most fonts that are embedded only include a subset of the glyphs that the font contains, so it would be of limited use for a form field.
What do you mean by "access a font"?
Re: You cannot use a font that's embedded in the document but not currently in use with a form field.
Okay, so that's a clear "no, it's not possible".
Re: Even if you could somehow, most fonts that are embedded only include a subset of the glyphs that the font contains, so it would be of limited use for a form field.
That's not a good reason, I can control the glyphs I include, and I can validate what the user enters to exclude for example anything else than ASCII.
Re: What do you mean by "access a font"?
Something like this.getField ("FieldName").textFont = this.getFontByPostscriptName ("Garamond").
No such method, or anything similar to that, exists in Acrobat JS.
It turns out that what I wanted to do is indeed possible.
The font simply has to be embedded in full (i.e. not a subset).
Then one can refer to that font with its Postscript name, as any of the fonts installed on the computer:
this.getField ("FieldName").textFont = "Garamond", for example.
See this example file.
It was George_Johnson who came closest to the correct answer, when he wrote:
"You cannot use a font that's embedded in the document but not currently in use with a form field."
Of course you can refer to a font using its PS name, if you know it. What we were saying is that you can't get the PS name of a font based on its display name, or on the fact it's embedded in the file or anything like that. Only if it's used in a form field or an annotation can you find out its internal PS name using a script.
Please don't be so assertive.
You wrote that "JS has no access to the list of available fonts in the computer", which turned out to be plain wrong.
Then you wrote that "If you use JS (or an external library) to set the font name to a font that is not available on the computer then my guess is that it will default to some default font (Helvetica or something like that) when the file is opened, or it will simply not work at all", which turns out to be plain wrong.
Then you wrote, when I said that I was looking for something similar to "this.getField("FieldName").textFont = this.getFontByPostscriptName ("Garamond")", that "No such method, or anything similar to that, exists in Acrobat JS", which also turns out to be plain wrong. A correct answer would have been "You can simply use "this.getField("FieldName").textFont = "Garamond"" if the font named "Garamond" is embedded in full in the PDF."
Now you write that "Of course you can refer to a font using its PS name, if you know it", although it was clear from the very beginning that I knew the Postscript name of the font I wanted to use. And it is again wrong: it is not possible to refer to an (embedded) font using its Postscript name, even if you know it, unless that font has been embedded in a special way (namely in full).
I stand behind all the things I wrote before, but it seems we keep misunderstanding each other, so let's drop it.