> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/basic-operations/extraction/embedded-fonts.md).

# Embedded fonts on Server/Desktop

Learn how to extract embedded fonts in PDF documents, understand where fonts are accessed from, and how to programmatically embed fonts using the Apryse SDK. Explore examples and best practices for wo

To extract embedded fonts in a document.

{% tabs %}
{% tab title="C#" %}
{% code lineNumbers="true" %}

```csharp
bool ObjIsEmbeddedFont(Obj indirectObj) {
  if (indirectObj.IsFree()) {
    return false;
  }

  if (!indirectObj.IsDict() && !indirectObj.IsStream()) {
    return false;
  }

  Obj typeObj = indirectObj.FindObj("Type");
  if (typeObj == null || !typeObj.IsName()) {
    return false;
  }

  var typeName = typeObj.GetName();
  if (!typeName.Equals("Font")) {
    return false;
  }

  Obj subtypeObj = indirectObj.FindObj("Subtype");
  if (subtypeObj != null && subtypeObj.IsName()){
    var subtypeName = subtypeObj.GetName();
    if (subtypeName.Equals("CIDFontType0")) {
      return false;
    }
  }

  Font font = new Font(indirectObj);
  return font.IsEmbedded();
}

PDFDoc doc = new PDFDoc(filename)
SDFDoc sdfdoc = doc.GetSDFDoc();
for (int i = 1; i < sdfdoc.XRefSize(); ++i) {
  Obj indirectObj = sdfdoc.GetObj(i);
  if (ObjIsEmbeddedFont(indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="C++" %}
{% code lineNumbers="true" %}

```cpp
bool objIsEmbeddedFont(Obj indirectObj) {
  if (indirectObj.IsFree()) {
    return false;
  }

  if (!indirectObj.IsDict() && !indirectObj.IsStream()) {
    return false;
  }

  Obj typeObj = indirectObj.FindObj("Type");
  if (!typeObj || !typeObj.IsName()) {
    return false;
  }

  string typeName = typeObj.GetName();
  if (typeName.compare("Font") != 0) {
    return false;
  }

  Obj subtypeObj = indirectObj.FindObj("Subtype");
  if (subtypeObj && subtypeObj.IsName()) {
    string subtypeName = subtypeObj.GetName();
    if (subtypeName.compare("CIDFontType0") == 0) {
      return false;
    }
  }

  Font font(indirectObj);
  return font.IsEmbedded();
}

PDFDoc doc(filename);
SDFDoc &sdfdoc = doc.GetSDFDoc();
for (int i = 1; i < sdfdoc.XRefSize(); ++i) {
  Obj indirectObj = sdfdoc.GetObj(i);
  if (objIsEmbeddedFont(indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Go" %}
{% code lineNumbers="true" %}

```go
func objIsEmbeddedFont(Obj indirectObj) bool {
  if indirectObj.IsFree() {
    return false
  }

  if !indirectObj.IsDict() && !indirectObj.IsStream() {
    return false
  }

  typeObj := indirectObj.FindObj("Type")
  if !typeObj || !typeObj.IsName() {
    return false
  }

  typeName := typeObj.GetName()
  if typeName.compare("Font") != 0 {
    return false
  }

  subtypeObj := indirectObj.FindObj("Subtype")
  if subtypeObj && subtypeObj.IsName() {
    subtypeName = subtypeObj.GetName()
    if subtypeName.compare("CIDFontType0") == 0 {
      return false
    }
  }

  font := NewFont(indirectObj)
  return font.IsEmbedded()
}

doc := NewPDFDoc(filename)
sdfdoc = doc.GetSDFDoc()
i := 1
for i < sdfdoc.XRefSize() {
  indirectObj := sdfdoc.GetObj(i);
  if objIsEmbeddedFont(indirectObj) {
    // perform document processing
  }
  i = i + 1;
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
private boolean objIsEmbeddedFont(Obj indirectObj) {
  if (indirectObj.isFree()) {
    return false;
  }

  if (!indirectObj.isDict() && !indirectObj.isStream()) {
    return false;
  }

  Obj typeObj = indirectObj.findObj("Type");
  if (typeObj == null || !typeObj.isName()) {
    return false;
  }

  String typeName = typeObj.getName();
  if (!typeName.equals("Font")) {
    return false;
  }

  Obj subtypeObj = indirectObj.findObj("Subtype");
  if (subtypeObj != null && subtypeObj.isName()) {
    String subtypeName = subtypeObj.getName();
    if (subtypeName.equals("CIDFontType0")) {
      return false;
    }
  }

  Font font = new Font(indirectObj);
  return font.isEmbedded();
}

PDFDoc doc = new PDFDoc(filename);
SDFDoc sdfdoc = doc.getSDFDoc();
for(int i = 1; i < sdfdoc.xRefSize(); ++i) {
  Obj indirectObj = sdfdoc.getObj(i);
  if (objIsEmbeddedFont(indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```js
async function objIsEmbeddedFont(indirectObj) {
  if (await indirectObj.isFree()) {
    return false;
  }

  if (!(await indirectObj.isDict()) && !(await indirectObj.isStream())) {
    return false;
  }

  const typeObj = await indirectObj.findObj('Type');
  if (!typeObj || !(await typeObj.isName())) {
    return false;
  }

  const typeName = await typeObj.getName();
  if (typeName !== 'Font') {
    return false;
  }

  const subtypeObj = await indirectObj.findObj('Subtype');
  if (!!subtypeObj && await subtypeObj.isName()) {
    const subtypeName = await subtypeObj.getName();
    if (subtypeName === 'CIDFontType0') {
      return false;
    }
  }

  const font = await PDFNet.Font.createFromObj(indirectObj);
  return await font.isEmbedded();

}

async function main() {
  const doc = await PDFNet.PDFDoc.createFromFilePath(filename);
  const sdfdoc = await doc.getSDFDoc();
  const xRefSize = await sdfdoc.xRefSize();
  for (let i = 0; i < xRefSize; ++i) {
    const indirectObj = await sdfdoc.getObj(i);
    if (await objIsEmbeddedFont(indirectObj)) {
      // perform document processing
    }
  }
}
PDFNet.runWithCleanup(main);
```

{% endcode %}
{% endtab %}

{% tab title="Kotlin" %}
{% code lineNumbers="true" %}

```kotlin
fun objIsEmbeddedFont(indirectObj: Obj) : Boolean {
  if (indirectObj.isFree()) {
    return false
  }

  if (!indirectObj.isDict() && !indirectObj.isStream()) {
    return false
  }

  val typeObj = indirectObj.findObj("Type")
  if (typeObj == null || !typeObj.isName()) {
    return false
  }

  val typeName = typeObj.getName()
  if (!typeName.equals("Font")) {
    return false
  }

  val subtypeObj = indirectObj.findObj("Subtype")
  if (subtypeObj != null && subtypeObj.isName()) {
    val subtypeName = subtypeObj.getName()
    if (subtypeName.equals("CIDFontType0")) {
      return false
    }
  }

  val font = Font(indirectObj)
  return font.isEmbedded()
}

val doc = PDFDoc(filename)
val sdfdoc = doc.getSDFDoc()
for (i in 1 until sdfdoc.xRefSize()) {
  val indirectObj = sdfdoc.getObj(i);
  if (objIsEmbeddedFont(indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
BOOL ObjIsEmbeddedFont:(PTObj *) indirectObj {
  if ([indirectObj IsFree]) {
    return NO;
  }

  if (![indirectObj IsDict] && ![indirectObj IsStream]) {
    return NO;
  }

  PTObj *typeObj = [indirectObj FindObj:@"Type"];
  if (!typeObj || ![typeObj IsName]) {
    return NO;
  }

  NSString *typeName = [typeObj GetName];
  if (![typeName isEqualToString:@"Font"]) {
    return NO;
  }

  PTObj *subtypeObj = [indirectObj FindObj:@"Subtype"];
  if (subtypeObj != (id)[NSNull null] && [subtypeObj IsName]) {
    NSString *subtypeName = [subtypeObj GetName];
    if ([subtypeName isEqualToString:@"CIDFontType0"]) {
      return NO;
    }
  }

  PTFont *font = [[PTFont alloc] initWithFont_dict:indirectObj];
  return [font IsEmbedded];
}

PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: filename];
PTSDFDoc *sdfdoc = [doc GetSDFDoc];
for (int i = 1; i < [sdfdoc XRefSize]; ++i) {
  PTObj *indirectObj = [sdfdoc GetObj:i];
  if (ObjIsEmbeddedFont(indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
func objIsEmbeddedFont(indirectObj:PTObj) -> Bool {
  if (indirectObj.isFree()) {
    return false
  }

  if (!indirectObj.isDict() && !indirectObj.isStream()) {
    return false
  }

  let typeObj = indirectObj.find("Type") as PTObj?
  if (typeObj == nil || !typeObj!.isName()) {
    return false
  }

  let typeName = typeObj?.getName()
  if (typeName != "Font") {
    return false
  }

  let subtypeObj = indirectObj.find("Subtype") as PTObj?
  if (subtypeObj != nil && subtypeObj!.isName()) {
    let subtypeName = subtypeObj?.getName()
    if (subtypeName == "CIDFontType0") {
      return false
    }
  }

  let font = PTFont(font_dict: indirectObj)
  return font?.isEmbedded() ?? false
}

let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
let sdfdoc = doc.getSDFDoc()
for i in 1...(sdfdoc?.xRefSize())! - 1 {
  let indirectObj = (sdfdoc?.getObj(i))!
  if(objIsEmbeddedFont(indirectObj:indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="PHP" %}
{% code lineNumbers="true" %}

```php
function objIsEmbeddedFont($indirectObj) {
  if ($indirectObj->IsFree()) {
    return false;
  }

  if (!$indirectObj->IsDict() && !$indirectObj->IsStream()) {
    return false;
  }

  $typeObj = $indirectObj->FindObj("Type");
  if (is_null($typeObj) || !$typeObj->IsName()) {
    return false;
  }

  $typeName = $typeObj->GetName();
  if (strcmp($typeName, "Font") != 0) {
    return false;
  }

  $subtypeObj = $indirectObj->FindObj("Subtype");
  if (!is_null($subtypeObj) && $subtypeObj->IsName()) {
    $subtypeName = $subtypeObj->GetName();
    if (strcmp($subtypeName, "CIDFontType0")) {
      return false;
    }
  }

  $font = new Font($indirectObj);
  return $font->IsEmbedded();
}

$doc = new PDFDoc($filename);
$sdfdoc = $doc->GetSDFDoc();
for ($i = 1; $i < $sdfdoc->XRefSize(); $i++) {
  $indirectObj = $sdfdoc->GetObj($i);
  if(objIsEmbeddedFont($indirectObj)) {
    // perform document processing
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
def obj_is_embedded_font(indirect_obj):
  if indirect_obj.IsFree():
    return False

  if not indirect_obj.IsDict() and not indirect_obj.IsStream():
    return False

  type_obj = indirect_obj.FindObj("Type")
  if type_obj is None or not type_obj.IsName():
    return False

  type_name = type_obj.GetName()
  if type_name != "Font":
    return False

  subtype_obj = indirect_obj.FindObj("Subtype")
  if subtype_obj is not None and subtype_obj.IsName():
    subtype_name = subtype_obj.GetName()
    if subtype_name =="CIDFontType0":
      return False

  font = Font(indirect_obj)
  return font.IsEmbedded()

doc = PDFDoc(filename)
sdfdoc = doc.GetSDFDoc()
for i in range(1, sdfdoc.XRefSize() + 1):
  indirect_obj = sdfdoc.GetObj(i)
  if obj_is_embedded_font(indirect_obj):
    # perform document processing
```

{% endcode %}
{% endtab %}

{% tab title="Ruby" %}
{% code lineNumbers="true" %}

```ruby
def obj_is_embedded_font(indirect_obj)
  if indirect_obj.IsFree() then
    return false
  elsif !indirect_obj.IsDict() && !indirect_obj.IsStream() then
    return false
  end

  type_obj = indirect_obj.FindObj("Type")
  if type_obj.nil? || !type_obj.IsName() then
    return false
  end

  type_name = type_obj.GetName()
  if type_name != "Font" then
    return false
  end

  subtype_obj = indirect_obj.FindObj("Subtype")
  if !subtype_obj.nil? && subtype_obj.IsName() then
    subtype_name = subtype_obj.GetName()
    if subtype_name =="CIDFontType0" then
      return false
    end
  end

  font = Font.new(indirect_obj)
  return font.IsEmbedded()

end

doc = PDFDoc.new(filename)
sdfdoc = doc.GetSDFDoc()
for i in 1..sdfdoc.XRefSize() do
  indirect_obj = sdfdoc.GetObj(i)
  if obj_is_embedded_font(indirect_obj) then
    # perform document processing
  end
end
```

{% endcode %}
{% endtab %}

{% tab title="VB" %}
{% code lineNumbers="true" %}

```vb
Function ObjIsEmbeddedFont(indirectObj As Obj) As Boolean
  If indirectObj.IsFree() Then
    Return False
  ElseIf Not indirectObj.IsDict() And Not indirectObj.IsStream() Then
    Return False
  End if

  Dim typeObj As Obj = indirectObj.FindObj("Type")
  If typeObj Is Nothing Then
    Return False
  ElseIf Not indirectObj.FindObj("Type").IsName() Then
    Return False
  ElseIf indirectObj.FindObj("Type").GetName() <> "Font" Then
    Return False
  ElseIf indirectObj.FindObj("Subtype") IsNot Nothing And indirectObj.FindObj("Subtype").IsName()
    If indirectObj.FindObj("Subtype").GetName() = "CIDFontType0" Then
      Return False
    End If
  End If

  Dim font As Font = New Font(indirectObj)
  Console.WriteLine(font.IsEmbedded())
  Return font.IsEmbedded()
End Function

PDFDoc = New PDFDoc(filename)
Dim sdfdoc As SDFDoc = doc.GetSDFDoc()
Dim XRefSize As Integer = sdfdoc.XRefSize()
Dim i As Integer
For i = 1 To XRefSize
  Dim indirectObj As Obj = sdfdoc.GetObj(i)
  If ObjIsEmbeddedFont(indirectObj) Then
    ' perform document processing
  End If
Next i
```

{% endcode %}
{% endtab %}
{% endtabs %}

## 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.

![](https://3779731113-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fziw3GiL98Xfj63F3He8h%2Fuploads%2Fgit-blob-6bc3b982454c98f5eef6aea356b5dffde8b3afbf%2F014233a6db0bc5100093dd3c5265f5b9f97e6a91-537x260.gif?alt=media)

It's also possible to programmatically embed fonts within a PDF with the Apryse SDK. You can find an example in the [ElementBuilder sample code](/core/get-started/samples/elementbuildertest.md) sample code. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.

The samples below demonstrate 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 counted as embedded fonts because they are necessarily referenced by a parent font within the same document.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/basic-operations/extraction/embedded-fonts.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
