←Select platform

PDFFont Structure

Summary

Contains the information about a PDF font.

Syntax
C#
VB
C++
Java
[SerializableAttribute()] 
public struct PDFFont 
<SerializableAttribute()> 
Public Structure PDFFont  
   Inherits System.ValueType 
public class PDFFont 
[SerializableAttribute()] 
public value class PDFFont : public System.ValueType  

Remarks

The PDFFont structure is used as the type for the PDFDocument.Fonts collection.

You can read the fonts of a PDF page using the PDFDocument.ParseDocumentStructure method with the PDFParseDocumentStructureOptions.Fonts as part of the options parameter. When this method returns, The PDFDocument.Fonts collection will be populated with the PDFFont objects found in the document.

The font height and width is not stored in the PDFFont structure, instead it is stored in the PDFObject.TextProperties property of the text or hyperlink object.

PDFFont matches the font properties from PDF specification and contains the following members:

Member Description
FaceName Name of the font found in the file such as "Arial", "Times New Roman" or "ArialBoldEmbeddedSubset" or any value the creator of the PDF chose when creating the document.
FontType Type of font. Typically "Type0", "Type1", "TrueType" or any other font types defined by the PDFFont.TypeXYZ constants.
Encoding Character set encoding used by this font. This could be "StandardEncoding", "WinAnsiEncoding", or any other encodings defined by the PDFFont.EncodingXYZ constants.
DescendantCID Descendant CIDFont name if this is a Type 0 font.
EmbeddingType Font embedding type, such as None, Embedded or EmbeddedSubset.

Refer to the example below for sample source code on how to parse these values.

Example

This example parses the fonts from a PDF document and shows their information.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
using Leadtools.WinForms; 
 
private static void DocumentFontsExample() 
{ 
   // Make a copy of 'leadtools.pdf' installed with LEADTOOLS 
   string pdfFile = @"C:\Users\Public\Documents\LEADTOOLS Images\leadtools.pdf"; 
 
   using (var document = new PDFDocument(pdfFile)) 
   { 
      document.ParseDocumentStructure(PDFParseDocumentStructureOptions.Fonts); 
 
      Console.WriteLine("Fonts found in the document:"); 
      if (document.Fonts != null && document.Fonts.Count > 0) 
      { 
         foreach (PDFFont font in document.Fonts) 
         { 
            string faceName = GetPDFFontFaceName(font); 
            string type = GetPDFFontTypeName(font); 
            string encoding = GetPDFFontEncodingName(font); 
 
            Console.WriteLine($"  Face name:{faceName}\n    type:{type} encoding:{encoding}"); 
         } 
      } 
   } 
} 
 
private static string GetPDFFontFaceName(PDFFont font) 
{ 
   if (string.IsNullOrEmpty(font.FaceName)) 
      return string.Empty; 
 
   string faceName = font.FaceName; 
 
   // Strip out everything between + and - 
   char[] separator = { '+', '-' }; 
   int index = faceName.IndexOfAny(separator); 
   if (index != -1) 
   { 
      faceName = faceName.Substring(index + 1); 
      index = faceName.IndexOfAny(separator); 
      if (index != -1) 
         faceName = faceName.Substring(0, index); 
   } 
 
   switch (font.EmbeddingType) 
   { 
      case PDFFontEmbeddingType.Embedded: 
         faceName += " (Embedded)"; 
         break; 
 
      case PDFFontEmbeddingType.EmbeddedSubset: 
         faceName += " (Embedded Subset)"; 
         break; 
 
      case PDFFontEmbeddingType.None: 
      default: 
         break; 
   } 
 
   return faceName; 
} 
 
private static string GetPDFFontTypeName(PDFFont font) 
{ 
   if (string.IsNullOrEmpty(font.FontType)) 
      return string.Empty; 
 
   if (string.Compare(PDFFont.TypeType0, font.FontType, true) == 0) 
   { 
      if (string.Compare(PDFFont.TypeCIDFontType2, font.DescendantCID, true) == 0) 
         return "TrueType (CID)"; 
      else 
         return "Type 2 (CID)"; 
   } 
 
   if (string.Compare(PDFFont.TypeType1, font.FontType, true) == 0) 
      return "Type 1"; 
 
   if (string.Compare(PDFFont.TypeType3, font.FontType, true) == 0) 
      return "Type 3"; 
 
   return font.FontType; 
} 
 
private static string GetPDFFontEncodingName(PDFFont font) 
{ 
   if (string.IsNullOrEmpty(font.Encoding)) 
      return "Custom"; 
 
   if (string.Compare(PDFFont.EncodingWinAnsiEncoding, font.Encoding, true) == 0) 
      return "Ansi"; 
 
   if (string.Compare(PDFFont.EncodingStandardEncoding, font.Encoding, true) == 0) 
      return "Standard"; 
 
   if (string.Compare(PDFFont.EncodingPDFDocEncoding, font.Encoding, true) == 0) 
      return "PDF"; 
 
   if (string.Compare(PDFFont.EncodingMacExpertEncoding, font.Encoding, true) == 0) 
      return "MAC Expert"; 
 
   if (string.Compare(PDFFont.EncodingMacRomanEncoding, font.Encoding, true) == 0) 
      return "MAC Roman"; 
 
   return font.Encoding; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Controls 
Imports Leadtools.Pdf 
Imports Leadtools.Svg 
Imports Leadtools.WinForms 
 
Private Shared Sub DocumentFontsExample() 
   ' Make a copy of 'leadtools.pdf' installed with LEADTOOLS 
   Dim pdfFile As String = "C:\Users\Public\Documents\LEADTOOLS Images\leadtools.pdf" 
 
   Using document As New PDFDocument(pdfFile) 
      document.ParseDocumentStructure(PDFParseDocumentStructureOptions.Fonts) 
 
      Console.WriteLine("Fonts found in the document:") 
      If Not IsNothing(document.Fonts) AndAlso document.Fonts.Count > 0 Then 
         For Each font As PDFFont In document.Fonts 
            Dim faceName As String = GetPDFFontFaceName(font) 
            Dim type As String = GetPDFFontTypeName(font) 
            Dim encoding As String = GetPDFFontEncodingName(font) 
 
            Console.WriteLine($"  Face name:{faceName}\n    type:{type} encoding:{encoding}") 
         Next 
      End If 
   End Using 
End Sub 
 
Private Shared Function GetPDFFontFaceName(font As PDFFont) As String 
   If String.IsNullOrEmpty(font.FaceName) Then 
      Return String.Empty 
   End If 
 
   Dim faceName As String = font.FaceName 
 
   ' Strip out everything between + and - 
   Dim separator() As Char = {"+"c, "-"c} 
   Dim index As Integer = faceName.IndexOfAny(separator) 
   If index <> -1 Then 
      faceName = faceName.Substring(index + 1) 
      index = faceName.IndexOfAny(separator) 
      If (index <> -1) Then 
         faceName = faceName.Substring(0, index) 
      End If 
   End If 
 
   Select Case font.EmbeddingType 
      Case PDFFontEmbeddingType.Embedded 
         faceName += " (Embedded)" 
 
      Case PDFFontEmbeddingType.EmbeddedSubset 
         faceName += " (Embedded Subset)" 
 
      Case PDFFontEmbeddingType.None 
      Case Else 
   End Select 
 
   Return faceName 
End Function 
 
Private Shared Function GetPDFFontTypeName(font As PDFFont) As String 
   If String.IsNullOrEmpty(font.FontType) Then 
      Return String.Empty 
   End If 
 
   If String.Compare(PDFFont.TypeType0, font.FontType, True) = 0 Then 
      If String.Compare(PDFFont.TypeCIDFontType2, font.DescendantCID, True) = 0 Then 
         Return "TrueType (CID)" 
      Else 
         Return "Type 2 (CID)" 
      End If 
   End If 
 
   If String.Compare(PDFFont.TypeType1, font.FontType, True) = 0 Then 
      Return "Type 1" 
   End If 
 
   If String.Compare(PDFFont.TypeType3, font.FontType, True) = 0 Then 
      Return "Type 3" 
   End If 
 
   Return font.FontType 
End Function 
 
Private Shared Function GetPDFFontEncodingName(font As PDFFont) As String 
   If String.IsNullOrEmpty(font.Encoding) Then 
      Return "Custom" 
   End If 
   If String.Compare(PDFFont.EncodingWinAnsiEncoding, font.Encoding, True) = 0 Then 
      Return "Ansi" 
   End If 
   If String.Compare(PDFFont.EncodingStandardEncoding, font.Encoding, True) = 0 Then 
      Return "Standard" 
   End If 
   If String.Compare(PDFFont.EncodingPDFDocEncoding, font.Encoding, True) = 0 Then 
      Return "PDF" 
   End If 
   If String.Compare(PDFFont.EncodingMacExpertEncoding, font.Encoding, True) = 0 Then 
      Return "MAC Expert" 
   End If 
   If String.Compare(PDFFont.EncodingMacRomanEncoding, font.Encoding, True) = 0 Then 
      Return "MAC Roman" 
   End If 
   Return font.Encoding 
End Function 

Requirements

Target Platforms

Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly