C#: Get the Font Information in PDF

Get PDF font information is the process of extracting details about the fonts used in a PDF document. This information typically includes the font name, size, type, color, and other attributes. Knowing these details can help in ensuring consistency, copyright compliance, and aesthetics of the PDF document. In this article, you will learn how to get the font information in PDF in C# using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Get the Fonts of Specified Text in PDF in C#

With Spire.PDF for .NET, you can find specified text and get its font formatting such as font name, size, style and color through the corresponding properties of the PdfTextFragment class. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get a specified page using PdfDocument.Pages[] property.
  • Create a PdfTextFinder instance.
  • Find the specified text using PdfTextFinder.Find() method and return a PdfTextFragment object.
  • Create a StringBuilder instance to store information.
  • Iterate through all found text.
    • Get the found text using PdfTextFragment.Text property.
    • Get the font name of the found text using PdfTextFragment.TextStates[0].FontName property.
    • Get the font size of the found text using PdfTextFragment.TextStates[0].FontSize property.
    • Get the font family of the found text using PdfTextFragment.TextStates[0].FontFamily property.
    • Indicate whether the font is bold or faux bold (font style set to fill and stroke) using PdfTextFragment.TextStates[0].IsSimulateBold and PdfTextFragment.TextStates[0].IsItalic properties.
    • Get the font color of the found text using PdfTextFragment.TextStates[0].ForegroundColor property.
    • Append the font information to the StringBuilder instance using StringBuilder.AppendLine() method.
  • Write to a txt file.
  • C#
using Spire.Pdf;
using Spire.Pdf.Texts;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;

namespace GetTextFont
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load a PDF file
            pdf.LoadFromFile("NET Framework.pdf");

            // Get the first page
            PdfPageBase page = pdf.Pages[0];

            // Create a PdfTextFinder instance
            PdfTextFinder finds = new PdfTextFinder(page);

            // Find specified text on the page
            finds.Options.Parameter = TextFindParameter.None;
            List result = finds.Find(".NET Framework");

            // Create a StringBuilder instance
            StringBuilder str = new StringBuilder();

            // Iterate through all found text
            foreach (PdfTextFragment find in result)
            {
                // Get the found text
                string text = find.Text;
                // Get the font name 
                string FontName = find.TextStates[0].FontName;
                // Get the font size
                float FontSize = find.TextStates[0].FontSize;
                // Get the font family
                string FontFamily = find.TextStates[0].FontFamily;
                // Indicate whether the font is bold or italic
                bool IsBold = find.TextStates[0].IsBold;
                bool IsSimulateBold = find.TextStates[0].IsSimulateBold;
                bool IsItalic = find.TextStates[0].IsItalic;
                // Get the font color
                Color color = find.TextStates[0].ForegroundColor;

                // Append the font information to the StringBuilder instance
                str.AppendLine(text);
                str.AppendLine("FontName: " + FontName);
                str.AppendLine("FontSize: " + FontSize);
                str.AppendLine("FontFamily: " + FontFamily);
                str.AppendLine("IsBold: " + IsBold);
                str.AppendLine("IsSimulateBold: " + IsSimulateBold);
                str.AppendLine("IsItalic: " + IsItalic);
                str.AppendLine("color: " + color);
                str.AppendLine(" ");
            }
            // Write to a txt file
            File.WriteAllText("PdfFont.txt", str.ToString());
        }
    }
}

Get the font name, size, color and style of the specified text in PDF

Get the Used Fonts in PDF in C#

Spire.PDF for .NET also provides the PdfUsedFont class to represent the fonts used in a PDF document. To get the formatting of all used fonts, you can iterate through each font and retrieve its font name, size, type and style using the corresponding properties of the PdfUsedFont class. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a PDF file using PdfDocument.LoadFromFile() method.
  • Get all the fonts used in the PDF file using PdfDocument.UsedFonts property.
  • Create a StringBuilder instance to store information.
  • Iterate through the used fonts.
    • Get the font name using PdfUsedFont.Name property.
    • Get the font size using PdfUsedFont.Size property.
    • Get the font type using PdfUsedFont.Type property.
    • Get the font style using PdfUsedFont.Style property.
    • Append the font information to the StringBuilder instance using StringBuilder.AppendLine() method.
  • Write to a txt file
  • C#
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;
using System.IO;
using System.Text;

namespace GetPdfFont
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();

            // Load a PDF file
            pdf.LoadFromFile("NET Framework.pdf");

            // Get the used fonts in the PDF file
            PdfUsedFont[] fonts = pdf.UsedFonts;

            // Create a StringBuilder instance
            StringBuilder str = new StringBuilder();

            // Iterate through the used fonts
            foreach (PdfUsedFont font in fonts)
            {
                // Get the font name
                string name = font.Name;

                // Get the font size
                float size = font.Size;

                // Get the font type
                PdfFontType type = font.Type;

                // Get the font style
                PdfFontStyle style = font.Style;

                // Append the font information to the StringBuilder instance
                str.AppendLine("FontName: " + name + " FontSize: " + size + " FontType: " + type + " FontStyle: " + style);

            }

            // Write to a txt file
            File.WriteAllText("PdfFontInfo.txt", str.ToString());
        }
    }
}

Get the font name, size, style and type of all used fonts in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.