14

I would like to pre-format the text in a form text-field, e.g. make it bold, italic or use a specific font. The following code generates a simple text-field:

\documentclass{scrreprt}
\usepackage{hyperref}
\begin{document}
\textbf{The Text Field}
\begin{Form}
\TextField[]{\ }
\end{Form}
\end{document}

Is it possible to pre-format the text in the text-field? The hyperref package manual (10/2011) speaks of an optional parameter format and the use of JavaScript to format the field. Can this be used to format the text in the field? And if yes, how is it done? (I am not proficient in JavaScript)

doncherry
  • 54,637
Rainer
  • 358
  • See the JavaScript™ for Acrobat® API Reference, p. 444 and surroundings for an overview of JavaScript in PDF formatting stuff. – doncherry Feb 26 '14 at 14:53
  • Stephan Lehmke: which fonts? Normally, pdfTeX does not embed complete fonts. Instead, it embeds subsets which contain the characters needed to display the content. Not only does this mean there is no guarantee a particular character is included in a subsetted font. These fonts also have weird (possibly unpredictable?) names. You can tell pdfTeX to embed complete fonts although this will obviously increase the file size. Is your question how to do that and how to use those fonts to format the text fields? – cfr Sep 24 '14 at 21:24
  • @cfr Exactly. Obviously, a font would need to be completely embedded to be used in a form field. Answers in Adobe forums tell that much. I know how to get pdftex to completely embed a font. Alas, so far no success to actually get that font to be used in a form field. Maybe an encoding problem? So the question really is how to use an embedded font in a Text Field reliably. – Stephan Lehmke Sep 27 '14 at 08:57
  • btw, the application I'm thinking about is producing "advertisement sheets" as preformatted PDF where only the "price tag" needs to be typed in before printing. – Stephan Lehmke Sep 27 '14 at 09:00
  • How exactly are you getting the font embedded and which font(s)? I doubt that Acrobat could use metafont or virtual fonts, for example. – cfr Sep 28 '14 at 01:01
  • Given what the Acrobat reference says about finding the value to give as the name of the font, have you tried selecting one using their UI as described and then entering the name returned in the .tex source? – cfr Sep 28 '14 at 01:06
  • @cfr If you have a workable solution please provide an answer with an example which I can try. – Stephan Lehmke Sep 29 '14 at 05:35
  • @StephanLehmke I didn't say it worked. I just asked if you'd tried it. I don't have Acrobat so I can't try it. But given the way they describe it, it looked as if the name of the font might be something you needed to get that way. Of course, there might be other ways they don't mention. – cfr Sep 29 '14 at 20:43

1 Answers1

18

I want to provide an example that shows most of the possibilities. I hope it helps:

\documentclass{article}
\usepackage[pdfstartview=FitH]{hyperref}

\begin{document}

\begin{Form}

\TextField[name = number.1,
           format = {
               var f = this.getField('number.1');
               f.textFont = 'Verdana';
               f.strokeColor = ['T'];
               f.fillColor = ['T'];
               f.userName = 'first number'
               },
           value = 1250,
           charsize = 10pt]
          {number 1}

\TextField[name = number.2,
           format = {
               var f = this.getField('number.2');
               f.textFont = 'Verdana';
               f.strokeColor = ['T'];
               f.fillColor = ['T'];
               f.userName = 'second number'
               },
           value = 500,
           charsize = 10pt]
          {number 2}

\TextField[name = sum,
           format = {
               var f = this.getField('sum');
               f.textFont = 'Verdana';
               f.strokeColor = ['T'];
               f.fillColor = ['T'];
               f.userName = 'sum'
               },
           calculate = {
               this.getField('sum').value =
                 this.getField('number.1').value + this.getField('number.2').value;
               },
           charsize = 10pt,
           readonly = true]
          {sum}

\end{Form}

\end{document}

This example was adapted from a German community.

doncherry
  • 54,637
Marco Daniel
  • 95,681
  • 1
    Thanks for the link. Do you have a solution for using TeX-fonts (embedded into the pdf-file) instead of system fonds? – Rainer Jan 08 '12 at 11:36
  • I would ask such a question at de.comp.text.tex. Heiko Oberdiek is very active there. (Maybe he will register here). You should link to this question here. – Marco Daniel Jan 08 '12 at 11:47
  • 1
    How can I change this font-format global, meaning not in each textfield? – cis Feb 09 '16 at 21:25
  • @cis Try something like \renewcommand*{\DefaultOptionsofText}{format = { var f = this.getField(event.targetName); f.textFont = 'Verdana';}} – Fuhrmanator Aug 27 '18 at 03:21
  • @Rainer I know this is old, but you can generate a PDF with the embedded TeX font (make some text outside the form have that font), then look at the Properties of the PDF file generated. I used Segoe Marker in XeTeX and it came out as SegoeMarker when it was embedded. Doing f.textFont='SegoeMarker'; worked for me. – Fuhrmanator Aug 27 '18 at 03:27
  • @Fuhrmanator It only worked because the font is installed on your system, not because it is embedded in the document. – Patrick Bergner Feb 12 '19 at 11:05
  • @Fuhrmanator I found a working example for global setting at https://tex.stackexchange.com/a/469012/32813, example: \def\DefaultOptionsofText{format = {var me = event.target.name; var f = this.getField(me); f.textFont = 'Courier'; }} – escalator May 26 '20 at 10:46