# hyperref: how to format text in a forms text-field

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)

• See the JavaScript™ for Acrobat® API Reference, p. 444 and surroundings for an overview of JavaScript in PDF formatting stuff. Feb 26, 2014 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, 2014 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. Sep 27, 2014 at 8: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. Sep 27, 2014 at 9: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, 2014 at 1:01

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'];
},
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'];
},
value = 500,
charsize = 10pt]
{number 2}

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

\end{Form}

\end{document}


This example was adapted from a German community.

• Thanks for the link. Do you have a solution for using TeX-fonts (embedded into the pdf-file) instead of system fonds? Jan 8, 2012 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. Jan 8, 2012 at 11:47
• How can I change this font-format global, meaning not in each textfield?
– cis
Feb 9, 2016 at 21:25
• @cis Try something like \renewcommand*{\DefaultOptionsofText}{format = { var f = this.getField(event.targetName); f.textFont = 'Verdana';}} Aug 27, 2018 at 3: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. Aug 27, 2018 at 3:27