If you have ever tried to pull bold or italic text out of a PDF, you probably started with a regex. It feels like the obvious move. Search the font name for the word “Bold,” flag the text, and move on. That strategy works just long enough to give you false confidence. Then someone exports the same document from a different version of Acrobat, or from LibreOffice, or from a print-to-PDF driver, and every assumption you coded falls apart.

Why Font Names Lie

PDF.js will hand you strings like ABCDEF+TimesNewRomanPS-BoldMT. The first six characters are a random prefix injected during export, and they change every time the file is regenerated. Betting your parser on that prefix is betting on noise. Other exporters are even less helpful. Some emit bare identifiers like Font12 or F1. These labels carry no semantic meaning at all; they are internal resource tags that happened to be nearby when the file was written.

Because the Portable Document Format was never designed to make downstream text extraction easy, font names are simply references to embedded or subsetted resources. They were never intended to act as a stable API for style detection. When you write a regex that looks for the substring Bold or Italic, you are scraping a label that the creator application was free to format however it pleased. You are not reading the actual typographic properties. You are reading a file-naming convention, and conventions are not contracts.

Read the Descriptor, Not the Label

The ground truth lives elsewhere. In PDF.js, every page object exposes commonObjs, a map that holds the real font descriptors needed to render the page. When the library parses a page, it populates this map with genuine font objects. Those objects expose boolean properties for bold and italic. Those booleans are not inferred from the name string. They originate from the font descriptor embedded inside the PDF, derived from glyph metrics, OS/2 table flags, and the symbolic descriptors that the typesetter wrote into the document.

That means you can stop guessing. Before you walk the text items on a page, create a fontStyleMap by iterating over page.commonObjs. For each font ID, store an object that records the actual .bold and .italic properties provided by the font object. Later, when you process each text item, look up its font reference in your map and read the precomputed flags. You suddenly have a deterministic answer that does not change between exports.

You can keep a fallback. If the descriptor is somehow absent or incomplete, clean the font name strip the prefix, drop the random tags, and run a conservative regex against what remains. But this should be your last resort, not your primary logic. The difference in reliability is dramatic. Where name-based parsing fractures across exporters, descriptor-based parsing holds steady because it asks the file what it actually contains.

When the Font Lies Too: Synthetic Styles

Even the descriptor can miss a trick. Some PDF creators do not bother embedding a separate italic typeface. Instead, they take the upright roman font and slant it with a transform matrix. This is common in files generated by design tools or older word processors that favor file size over typographic purity.

Every text item in PDF.js carries a transform array, a six-element affine matrix that maps the glyph coordinate system into the page coordinate system. The third element of that array controls horizontal shear. When that value is non-zero, the text is being mechanically slanted by the renderer. If you only trust the font descriptor, you will classify this text as upright roman. If you inspect the matrix, you catch the synthetic italic and mark it correctly. The same logic applies to synthetic bold created by overprinting, though that is harder to detect from geometry alone. For slanted text, the shear value is your smoking gun.

Underlines Are Drawn, Not Declared

Bold and italic are font properties. Underline is not. In PDF, an underline is a vector path. The renderer issues a drawing command for a thin horizontal segment positioned near the text baseline. It is a graphical element that happens to sit underneath glyphs, not a character attribute stored in a cmap.

This distinction matters because no amount of font descriptor reading will reveal an underline. You need to look at the raw drawing operators on the page, or at the geometry-level output, for short horizontal line segments that run parallel to the baseline at the correct proximity. When your extraction engine spots such a segment underneath a text run, you tag that run as underlined. Treating this as a separate detection layer keeps your data model honest: bold and italic are intrinsic to the font, while underline is extrinsic decoration rendered by the document.

A Working Pipeline

A clean extraction system separates concerns into distinct layers. First, a geometry worker walks the page. It queries page.commonObjs to assemble your fontStyleMap, inspects each text item’s transform array to catch synthetic italics, and scans nearby vector paths to spot underlines. Its output is a clean intermediate structure call it textMeta where every text run carries three simple booleans: bold, italic, and underline.

Next, a text rebuilder consumes that structure and emits marked-up output. Nesting order is important here. The correct hierarchy places underline outermost, then italic, then bold innermost. That means a fully styled run becomes <u><i><b>text</b></i></u>. This ordering prevents invalid HTML overlaps and keeps rendering consistent across browsers and document converters. It also mirrors the typographic logic: decoration wraps semantic emphasis, and semantic emphasis wraps structural weight.

The power of this approach is that it uses only what the PDF already knows about itself. There is no OCR involved, no cloud vision service, and no machine learning model guessing at styles from rasterized pixels. You are reading the file’s own semantic layer, exposed through the geometry and metadata that the creator application already calculated. The result is fast, deterministic, and accurate across the chaotic landscape of PDF generators.

The Real Takeaway

Regex against font names is a trap. It feels like a shortcut because it works on the one file you tested, but it collapses under the mild pressure of a second export. The real information is already inside the PDF, sitting in descriptors, matrices, and vector paths. Build your pipeline around those facts. Ask the font object whether it is bold. Check the transform matrix for shear. Look at the drawn segments for underlines. If you query the document’s own engineering instead of scraping its surface labels, you get styles that survive from one PDF generator to the next.