Embedded fonts using JavaScript

To extract embedded fonts in a document.

1async function objIsEmbeddedFont(indirectObj) {
2 if (await indirectObj.isFree()) {
3 return false;
4 }
5
6 if (!(await indirectObj.isDict()) && !(await indirectObj.isStream())) {
7 return false;
8 }
9
10 const typeObj = await indirectObj.findObj('Type');
11 if (!typeObj || !(await typeObj.isName())) {
12 return false;
13 }
14
15 const typeName = await typeObj.getName();
16 if (typeName !== 'Font') {
17 return false;
18 }
19
20 const subtypeObj = await indirectObj.findObj('Subtype');
21 if (!!subtypeObj && await subtypeObj.isName()) {
22 const subtypeName = await subtypeObj.getName();
23 if (subtypeName === 'CIDFontType0') {
24 return false;
25 }
26 }
27
28 const font = await PDFNet.Font.createFromObj(indirectObj);
29 return await font.isEmbedded();
30
31}
32
33WebViewer(...)
34 .then(instance => {
35 const { docViewer } = instance.Core;
36 docViewer.addEventListener('documentLoaded', () => {
37 const doc = await docViewer.getDocument().getPDFDoc();
38 const sdfdoc = await doc.getSDFDoc();
39 const xRefSize = await sdfdoc.xRefSize();
40 for (let i = 0; i < xRefSize; ++i) {
41 const indirectObj = await sdfdoc.getObj(i);
42 if (await objIsEmbeddedFont(indirectObj)) {
43 // perform document processing
44 }
45 }
46 });
47 });

About embedded fonts

PDF documents access fonts from one of two places: the host machine rendering the PDF document or from within the PDF document itself. When a font is used in a PDF document which is not available on the machine loading that document and it's not embedded within the document the viewer will usually load a different font. When that font is contained within the PDF document itself we call that an embedded font. When a PDF contains an embedded font that font can still be rendered even if it is not defined on the host machine.

All font information in a PDF is stored in the SDF layer as an SDF object and exists as either a dictionary or a stream. When a font exists a dictionary that means that means that it is defined exclusively within the PDF document but if it is defined as a stream that means it exists as a file which may or may not exist within the PDF document itself. It is possible for either type to be embedded.

Apryse Docs Image

It's also possible to programatically embed fonts within a PDF with the Apryse SDK. You can find an example in the ElementBuilder sample code.

The samples below demonstrates how to iterate over all embedded fonts found within a PDF document. Note that because this requires several low-level operations additional care must be taken for to check for possible null pointers. Also note that fonts with the subtype CIDFontType0 are not not counted as embedded fonts because they are necessarily referenced by a parent font within the same document.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales