17 Replies Latest reply on Jul 13, 2015 3:04 PM by try67

    How to use an embedded font to set the textFont of a form?

    gregheyt

      Hi,

      Is there a way to use one of the fonts that are embedded in the PDF as the textFont of a form?

      For instance, one can set this field with this.getField ("FieldName").textFont = "Helvetica".

      It is also possible to use "Garamond" there, provided that the font "Garamond" is installed on the computer on which the PDF file is opened.

      If it is not installed on that computer, then Acrobat uses a replacement font instead, even if that font is present (embedded) in the PDF file.

      In other words: given that it is possible to access many of the file resources from within JavaScript, is there a way to access its fonts?

      Many thanks,

      Gregory

       

        • 1. Re: How to use an embedded font to set the textFont of a form?
          try67 MVP & Adobe Community Professional

          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.

          • 2. Re: How to use an embedded font to set the textFont of a form?
            gregheyt Level 1

            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.

            Moreover, from my experience the font is actually not embedded in the PDF only by referring to it in JavaScript.

             

            • 3. Re: How to use an embedded font to set the textFont of a form?
              try67 MVP & Adobe Community Professional

              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.

              • 4. Re: How to use an embedded font to set the textFont of a form?
                gregheyt Level 1

                Hi,

                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...

                My question was: if you want to change/set the font programmatically, by assigning a value to textFont in JavaScript, how can/should you refer to the already embedded fonts?

                 

                Edit: The "JavaScript for Acrobat API Reference" tells me that "Beginning with Acrobat 5.0, an arbitrary font can be used when laying out a text field ... by setting the value of textFont to the PDSysFont font name, as returned by PDSysFontGetName."  Do embedded fonts have a PDSysFont font name, or does this only refer to fonts that are already installed "outside" of Acrobat?

                • 5. Re: How to use an embedded font to set the textFont of a form?
                  try67 MVP & Adobe Community Professional

                  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.

                  • 6. Re: How to use an embedded font to set the textFont of a form?
                    gregheyt Level 1

                    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,

                    what I would like to do is to access, with JavaScript, that embedded font, and to use it to set the textFont value of a form.

                    • 7. Re: How to use an embedded font to set the textFont of a form?
                      try67 MVP & Adobe Community Professional

                      JS also can't know which fonts are embedded in the file, if that's what you're asking.

                      • 8. Re: How to use an embedded font to set the textFont of a form?
                        gregheyt Level 1

                        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).

                        What I want to know is how I could access and use an embedded front, with JavaScript.

                        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?".

                        • 9. Re: How to use an embedded font to set the textFont of a form?
                          George_Johnson MVP & Adobe Community Professional

                          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.

                          • 10. Re: How to use an embedded font to set the textFont of a form?
                            try67 MVP & Adobe Community Professional

                            What do you mean by "access a font"?

                            • 11. Re: How to use an embedded font to set the textFont of a form?
                              gregheyt Level 1

                              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").

                              • 12. Re: How to use an embedded font to set the textFont of a form?
                                try67 MVP & Adobe Community Professional

                                getFontByPostscriptName

                                No such method, or anything similar to that, exists in Acrobat JS.

                                • 13. Re: How to use an embedded font to set the textFont of a form?
                                  Test Screen Name Most Valuable Participant

                                  A PDSysFont is something that can be directly accessed by plug-ins. It is the list of fonts installed in the computer, and is not connected to any document. JavaScript can't get the list of PDSysFonts but it can use the for font fields.

                                  • 14. Re: How to use an embedded font to set the textFont of a form?
                                    gregheyt Level 1

                                    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."

                                    • 15. Re: How to use an embedded font to set the textFont of a form?
                                      try67 MVP & Adobe Community Professional

                                      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.

                                      • 16. Re: How to use an embedded font to set the textFont of a form?
                                        gregheyt Level 1

                                        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).

                                        • 17. Re: How to use an embedded font to set the textFont of a form?
                                          try67 MVP & Adobe Community Professional

                                          I stand behind all the things I wrote before, but it seems we keep misunderstanding each other, so let's drop it.