Get the font information of text in PDF

2016-11-01 08:10:31 Written by  support iceblue
Rate this item
(0 votes)

Spire.PDF provides a class named PdfDocument that represents a PDF file, this class contains a property named UsedFonts which allows us to access the fonts used in PDF file, and then we can get the font information such as name, size, type and style easily.

The following steps explain how to get the font information of text in PDF by using Spire.PDF.

Step 1: Instantiate an object of PdfDocument class and load the PDF file.

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(@"E:\Program Files\Sample.pdf");

Step 2: Get the fonts that are used in the PDF file and put them into a PdfUsedFont array.

PdfUsedFont[] usedfont = pdf.UsedFonts;

Step 3: Loop through the array and print out the name, size, type and style of each font.

foreach (PdfUsedFont font in usedfont)
{
    Console.WriteLine("{0}, {1}, {2}, {3}", font.Name, font.Size, font.Type, font.Style);
}
Console.ReadKey();

Output:

How to get the font information of text in PDF

Full code:

[C#]
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics.Fonts;

namespace Get_the_font_information_of_text_in_PDF
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile(@"E:\Program Files\Sample.pdf");
            PdfUsedFont[] usedfont = pdf.UsedFonts;
            foreach (PdfUsedFont font in usedfont)
            {
                Console.WriteLine("{0}, {1}, {2}, {3}", font.Name, font.Size, font.Type, font.Style);
            }
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics.Fonts

Namespace Get_the_font_information_of_text_in_PDF
	Class Program
		Private Shared Sub Main(args As String())
			Dim pdf As New PdfDocument()
			pdf.LoadFromFile("E:\Program Files\Sample.pdf")
			Dim usedfont As PdfUsedFont() = pdf.UsedFonts
			For Each font As PdfUsedFont In usedfont
				Console.WriteLine("{0}, {1}, {2}, {3}", font.Name, font.Size, font.Type, font.Style)
			Next
			Console.ReadKey()
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Get the font information of text in PDF
Last modified on Sunday, 26 September 2021 01:55