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.
यह अंतर महत्वपूर्ण है क्योंकि फॉन्ट डिस्क्रिप्टर (font descriptor) पढ़ने से अंडरलाइन (underline) का पता नहीं चलेगा। आपको पेज पर रॉ ड्राइंग ऑपरेटर्स (raw drawing operators), या संक्षेप में कहें तो ज्योमेट्री-लेवल आउटपुट (geometry-level output) में उन छोटे क्षैतिज रेखा खंडों (horizontal line segments) को देखना होगा जो सही निकटता पर बेसलाइन के समानांतर चलते हैं। जब आपका एक्सट्रैक्शन इंजन टेक्स्ट रन के नीचे ऐसे किसी खंड को पहचान लेता है, तो आप उस रन को अंडरलाइन के रूप में टैग करते हैं। इसे एक अलग डिटेक्शन लेयर के रूप में मानने से आपका डेटा मॉडल सटीक रहता है: बोल्ड (bold) और इटैलिक (italic) फॉन्ट के आंतरिक गुण हैं, जबकि अंडरलाइन दस्तावेज़ द्वारा रेंडर किया गया एक बाहरी सजावट (extrinsic decoration) है।
एक कार्यशील पाइपलाइन
एक स्वच्छ एक्सट्रैक्शन सिस्टम कार्यों को अलग-अलग लेयर्स में विभाजित करता है। सबसे पहले, एक ज्योमेट्री वर्कर (geometry worker) पेज का विश्लेषण करता है। यह आपके fontStyleMap को तैयार करने के लिए page.commonObjs को क्वेरी करता है, सिंथेटिक इटैलिक्स (synthetic italics) को पकड़ने के लिए प्रत्येक टेक्स्ट आइटम के ट्रांसफॉर्म एरे (transform array) का निरीक्षण करता है, और अंडरलाइन का पता लगाने के लिए पास के वेक्टर पाथ (vector paths) को स्कैन करता है। इसका आउटपुट एक स्वच्छ इंटरमीडिएट स्ट्रक्चर होता है जिसे textMeta कहा जा सकता है, जहाँ प्रत्येक टेक्स्ट रन में तीन सरल बूलियन (booleans) होते हैं: bold, italic, और underline।
इसके बाद, एक टेक्स्ट रीबिल्डर (text rebuilder) उस स्ट्रक्चर का उपयोग करता है और मार्क-अप आउटपुट जारी करता है। यहाँ नेस्टिंग ऑर्डर (nesting order) महत्वपूर्ण है। सही पदानुक्रम (hierarchy) में अंडरलाइन को सबसे बाहर, फिर इटैलिक को, और फिर बोल्ड को सबसे अंदर रखा जाता है। इसका अर्थ है कि पूरी तरह से स्टाइल किया गया रन <u><i><b>text</b></i></u> बन जाता है। यह क्रमिंग अमान्य HTML ओवरलैप को रोकती है और ब्राउज़र एवं डॉक्यूमेंट कन्वर्टर्स में रेंडरिंग को सुसंगत बनाए रखती है। यह टाइपोग्राफिक लॉजिक को भी दर्शाता है: सजावट (decoration) सिमेंटिक एम्फेसिस (semantic emphasis) को लपेटती है, और सिमेंटिक एम्फेसिस स्ट्रक्चरल वेट (structural weight) को लपेटती है।
इस दृष्टिकोण की शक्ति यह है कि यह केवल उसी का उपयोग करता है जो PDF पहले से ही अपने बारे में जानता है। इसमें कोई OCR शामिल नहीं है, कोई क्लाउड विजन सर्विस नहीं है, और कोई मशीन लर्निंग मॉडल रास्टराइज़्ड पिक्सेल (rasterized pixels) से स्टाइल का अनुमान नहीं लगा रहा है। आप फ़ाइल की अपनी सिमेंटिक लेयर को पढ़ रहे हैं, जो उस ज्योमेट्री और मेटाडेटा के माध्यम से प्रकट होती है जिसे क्रिएटर एप्लिकेशन ने पहले ही कैलकुलेट कर लिया है। इसका परिणाम तेज़, नियतात्मक (deterministic) और PDF जनरेटर्स के अराजक परिदृश्य में सटीक होता है।
वास्तविक निष्कर्ष
फॉन्ट नामों के विरुद्ध Regex का उपयोग करना एक जाल है। यह एक शॉर्टकट जैसा लगता है क्योंकि यह उस एक फ़ाइल पर काम करता है जिसका आपने परीक्षण किया है, लेकिन दूसरे एक्सपोर्ट के मामूली दबाव में यह विफल हो जाता है। वास्तविक जानकारी पहले से ही PDF के अंदर, डिस्क्रिप्टर्स, मैट्रिसेस और वेक्टर पाथ में मौजूद है। अपने पाइपलाइन को इन्हीं तथ्यों के इर्द-गिर्द बनाएं। फॉन्ट ऑब्जेक्ट से पूछें कि क्या वह बोल्ड है। शीयर (shear) के लिए ट्रांसफॉर्म मैट्रिक्स की जाँच करें। अंडरलाइन के लिए खींचे गए सेगमेंट को देखें। यदि आप दस्तावेज़ के सतही लेबल को स्क्रैप करने के बजाय उसके अपने इंजीनियरिंग डेटा को क्वेरी करते हैं, तो आपको ऐसे स्टाइल मिलते हैं जो एक PDF जनरेटर से दूसरे तक बने रहते हैं।
