How to Manage Fonts in PDF

by Chaknith

A font is a set of characters, symbols, and glyphs with a consistent style and design. It represents a specific typeface, size, weight, and style (such as regular, bold, italic, etc.) of text. Fonts are used in typography to display text in a visually appealing and coherent manner.

IronPDF provides a convenient way to manage fonts, offering functionalities such as finding fonts, getting fonts, embedding fonts, unembedding fonts, and replacing fonts.

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
C# PDF DLL

Download DLL

Download DLL

Manually install into your project

Find and Retrieve Font

Retrieve Font

Accessing the Fonts property will return the PdfFontCollection object, which contains the list of all the fonts in the document. The Fonts property can be accessed directly by iterating over the PdfFontCollection object.

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-retrieve-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Collections.Generic;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Retreive font
PdfFontCollection fonts = pdf.Fonts;
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Collections.Generic

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Retreive font
Private fonts As PdfFontCollection = pdf.Fonts
VB   C#

Find Font

Finding a specific font is straightforward with IronPdf. Using the PdfFontCollection object, we can specify the font name as a string enclosed in square brackets. For example: Fonts["SpecialFontName"]. This will return a PdfFont object that we can use to inspect properties and perform additional methods.

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-find-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Collections.Generic;
using System.Linq;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Find font
PdfFont font = pdf.Fonts["SpecialFontName"];
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Collections.Generic
Imports System.Linq

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Find font
Private font As PdfFont = pdf.Fonts("SpecialFontName")
VB   C#

Add Font

Use the Add method to add both stardand font and font file as byte data. The Add method that accepts the font name only accepts one of the 14 standard fonts. Adding a standard font will not embed it, because the standard font already gaurenteed to be available on the operating system.

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-add-font.cs
using IronPdf;
using IronPdf.Fonts;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Add font
pdf.Fonts.Add("Helvetica");
Imports IronPdf
Imports IronPdf.Fonts

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Add font
pdf.Fonts.Add("Helvetica")
VB   C#

Embed Font

Embedding a font means including a font’s byte stream data in the PDF document itself. This way, it doesn't require the font to be installed on the system to view the PDF document properly. While this generally increases the file size of the PDF document, it's beneficial for visual consistency without the additional requirement of installing the font.

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-embed-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Linq;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Select which font to embed
PdfFont targetFont = pdf.Fonts["MyCustomFont"];

// Add the font
byte[] fontData = System.IO.File.ReadAllBytes("dir/to/font.ttf");
pdf.Fonts.Add(fontData);

// Embed the font
pdf.Fonts.Last().Embed(fontData);
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Linq

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Select which font to embed
Private targetFont As PdfFont = pdf.Fonts("MyCustomFont")

' Add the font
Private fontData() As Byte = System.IO.File.ReadAllBytes("dir/to/font.ttf")
pdf.Fonts.Add(fontData)

' Embed the font
pdf.Fonts.Last().Embed(fontData)
VB   C#

Unembed Font

Unembedding a font means removing the embedded byte stream data of the font included in a PDF document. The goal is to reduce the PDF document's filesize. Use the Unembed method to achieve this.

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-unembed-font.cs
using IronPdf;
using IronPdf.Fonts;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Get fonts
PdfFontCollection fonts = pdf.Fonts;

// Unembed a font
pdf.Fonts[0].Unembed();
Imports IronPdf
Imports IronPdf.Fonts

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Get fonts
Private fonts As PdfFontCollection = pdf.Fonts

' Unembed a font
pdf.Fonts(0).Unembed()
VB   C#

Replace Font

The font replacement operation will preserve the original font data structure, such as styling and character encoding, inside a PDF document but replace it with a newly specified font. Users have to ensure that the new font aligns well with the original font.

Before proceeding
In some rare cases, the resulting visual may not be a perfect fit. This is a current limitation of the font replacement method.

:path=/static-assets/pdf/content-code-examples/how-to/manage-font-replace-font.cs
using IronPdf;
using IronPdf.Fonts;
using System.Linq;

// Import PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

byte[] fontData = System.IO.File.ReadAllBytes("dir/to/font.ttf");
// Get and replace Font
pdf.Fonts["Courier"].ReplaceWith(fontData);
Imports IronPdf
Imports IronPdf.Fonts
Imports System.Linq

' Import PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

Private fontData() As Byte = System.IO.File.ReadAllBytes("dir/to/font.ttf")
' Get and replace Font
pdf.Fonts("Courier").ReplaceWith(fontData)
VB   C#

Standard Fonts

The 14 standard fonts in PDF, also known as the 'Base 14 Fonts' or 'Standard Type 1 Fonts,' form a set of fonts that are widely supported in PDF viewers and do not need to be embedded in the document. Standard fonts define 14 fonts that are guaranteed (as per the PDF document standard) to be available when working with a PDF document.

  • Courier
  • Courier-Bold
  • Courier-Oblique
  • Courier-BoldOblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-Oblique
  • Helvetica-BoldOblique
  • Times-Roman
  • Times-Bold
  • Times-Italic
  • Times-BoldItalic
  • Symbol
  • ZapfDingbats

Standard Fonts Mapping

For convenience when referring to the standard fonts, multiple string names point to the same font.

Map to Courier

  • StandardFont.Courier
    • Courier
    • CourierNew
    • CourierNewPSMT
    • CourierStd

Map to Courier-Bold

  • StandardFont.CourierBold
    • Courier,Bold
    • Courier-Bold
    • CourierBold
    • CourierNew,Bold
    • CourierNew-Bold
    • CourierNewBold
    • CourierNewPS-BoldMT
    • CourierStd-Bold

Map to Courier-Oblique

  • StandardFont.CourierOblique
    • Courier,Italic
    • Courier-Oblique
    • CourierItalic
    • CourierNew,Italic
    • CourierNew-Italic
    • CourierNewItalic
    • CourierNewPS-ItalicMT
    • CourierStd-Oblique

Map to Courier-BoldOblique

  • StandardFont.CourierBoldOblique
    • Courier,BoldItalic
    • Courier-BoldOblique
    • CourierBoldItalic
    • CourierNew,BoldItalic
    • CourierNew-BoldItalic
    • CourierNewBoldItalic
    • CourierNewPS-BoldItalicMT
    • CourierStd-BoldOblique

Map to Helvetica

  • StandardFont.Helvetica
    • Arial
    • ArialMT
    • Helvetica

Map to Helvetica-Bold

  • StandardFont.HelveticaBold
    • Arial,Bold
    • Arial-Bold
    • Arial-BoldMT
    • ArialBold
    • ArialMT,Bold
    • ArialRoundedMTBold
    • Helvetica,Bold
    • Helvetica-Bold
    • HelveticaBold

Map to Helvetica-Oblique

  • StandardFont.HelveticaOblique
    • Arial,Italic
    • Arial-Italic
    • Arial-ItalicMT
    • ArialItalic
    • ArialMT,Italic
    • Helvetica,Italic
    • Helvetica-Italic
    • Helvetica-Oblique
    • HelveticaItalic

Map to Helvetica-BoldOblique

  • StandardFont.HelveticaBoldOblique
    • Arial,BoldItalic
    • Arial-BoldItalic
    • Arial-BoldItalicMT
    • ArialBoldItalic
    • ArialMT,BoldItalic
    • Helvetica,BoldItalic
    • Helvetica-BoldItalic
    • Helvetica-BoldOblique
    • HelveticaBoldItalic

Map to Times-Roman

  • StandardFont.Times
    • Times-Roman
    • TimesNewRoman
    • TimesNewRomanPS
    • TimesNewRomanPSMT

Map to Times-Bold

  • StandardFont.TimesBold
    • Times-Bold
    • TimesBold
    • TimesNewRoman,Bold
    • TimesNewRoman-Bold
    • TimesNewRomanBold
    • TimesNewRomanPS-Bold
    • TimesNewRomanPS-BoldMT
    • TimesNewRomanPSMT,Bold

Map to Times-Italic

  • StandardFont.TimesOblique
    • Times-Italic
    • TimesItalic
    • TimesNewRoman,Italic
    • TimesNewRoman-Italic
    • TimesNewRomanItalic
    • TimesNewRomanPS-Italic
    • TimesNewRomanPS-ItalicMT
    • TimesNewRomanPSMT,Italic

Map to Times-BoldItalic

  • StandardFont.TimesBoldOblique
    • Times-BoldItalic
    • TimesBoldItalic
    • TimesNewRoman,BoldItalic
    • TimesNewRoman-BoldItalic
    • TimesNewRomanBoldItalic
    • TimesNewRomanPS-BoldItalic
    • TimesNewRomanPS-BoldItalicMT
    • TimesNewRomanPSMT,BoldItalic

Map to Symbol

  • StandardFont.Symbol
    • Symbol
    • SymbolMT

Map to ZapfDingbats

  • StandardFont.Dingbats
    • ZapfDingbats

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.