How to use \pdffontobjnum?

A font identifier is a control sequence corresponding to a font selection; it must be defined with the primitive \font (or \let to a control sequence so defined).

However, there are two predefined font identifiers. The first is \nullfont, which is not particularly useful because it has no characters. The second one is much more interesting and is \font itself.

Whenever TeX is looking for a font identifier, if it finds \font it will do as if you have used a font identifier corresponding to the current font.

In LaTeX the font identifiers are built in a standard format


so, for example, \OT1/cmr/m/n/10 refers to the default font. However, these identifiers are only built on demand when a \selectfont command is issued and points to the desired font; thus something like

\expandafter\pdffontobjnum\csname OT1/cmr/m/it/10\endcsname

might produce a Missing font identifier error, if the font hasn't yet been selected in the document. So you need to select the font before using \pdffontobjnum anyway.

The problem of identifiers only built on demand is not the main one. Consider the following plain TeX example:

\message{The font number is \tenitnumber}



This will cause a low level fatal error:

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017) (preloaded format=pdftex)
 restricted \write18 enabled.
entering extended mode
ap} The font number is 1 [1]Assertion failed: (fo->last_char >= fo->first_char), function create_fontdictionary, file ../../../texk/web2c/pdftexdir/writefont.c, line 607.
 )Abort trap: 6

because the internal number points to an object that's apparently not created. No error if you add \it before \tenitnumber, so the font is actually used in the document. It's not sufficient to use the font in a box: indeed

\message{The font number is \tenitnumber}

\setbox0=\hbox{\tenit something}    


would fail in the same way. So you must typeset something in a font, if you want to store the object number corresponding to it.

The error doesn't happen with LuaTeX; for instance,

\edef\firstfont{\pdffeedback fontobjnum\tenit}


correctly prints 1 and pdffonts outputs

name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
SDXKYB+CMR10                         Type 1            yes yes no       5  0

you can use \font to get the current font: