Embedded fonts on Server/Desktop

To extract embedded fonts in a document.

1def obj_is_embedded_font(indirect_obj):
2 if indirect_obj.IsFree():
3 return False
4
5 if not indirect_obj.IsDict() and not indirect_obj.IsStream():
6 return False
7
8 type_obj = indirect_obj.FindObj("Type")
9 if type_obj is None or not type_obj.IsName():
10 return False
11
12 type_name = type_obj.GetName()
13 if type_name != "Font":
14 return False
15
16 subtype_obj = indirect_obj.FindObj("Subtype")
17 if subtype_obj is not None and subtype_obj.IsName():
18 subtype_name = subtype_obj.GetName()
19 if subtype_name =="CIDFontType0":
20 return False
21
22 font = Font(indirect_obj)
23 return font.IsEmbedded()
24
25doc = PDFDoc(filename)
26sdfdoc = doc.GetSDFDoc()
27for i in range(1, sdfdoc.XRefSize() + 1):
28 indirect_obj = sdfdoc.GetObj(i)
29 if obj_is_embedded_font(indirect_obj):
30 # perform document processing

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