How to Manage Fonts in PDFs
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.
How to Manage Fonts in PDFs
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
// Import necessary namespaces for using IronPdf and dealing with collections in C#
using IronPdf;
using IronPdf.Fonts;
using System.Collections.Generic;
// Load a PDF file from the specified path
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Retrieve a collection of fonts used in the loaded PDF document
PdfFontCollection fonts = pdf.Fonts;
// Iterate through the fonts and output details about each one
foreach (PdfFont font in fonts)
{
// Output the name and other properties of each font
System.Console.WriteLine($"Font Name: {font.Name}");
System.Console.WriteLine($"Font Type: {font.Type}");
System.Console.WriteLine($"Font Embedded: {font.Embedded}");
System.Console.WriteLine(); // Add a blank line for better readability
}
// Note: To successfully execute this code, ensure that the IronPdf library is properly
// referenced in your project and that the path to "sample.pdf" is correct.
// This code assumes that a PDF file named "sample.pdf" exists in the current directory.
Find Font
Finding a specific font is straightforward with IronPDF. Using the PdfFontCollection object, you can specify the font name. This example shows how to access a font object by name and inspect properties.
:path=/static-assets/pdf/content-code-examples/how-to/manage-font-find-font.cs
using IronPdf;
using System;
using System.Linq;
// Load a PDF document from file
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Assume a font with a specific name is needed from the PDF document
// Check if the font list contains the font with the specified name
PdfFont font = pdf.Fonts.FirstOrDefault(f => f.Name == "SpecialFontName");
if (font != null)
{
// If the font is found, perform operations with the font here
// For example, you might extract text that uses this particular font
Console.WriteLine("The font 'SpecialFontName' was found in the PDF.");
// You can add additional code here to utilize the found font
}
else
{
// This block will run if the specified font is not found in the PDF document
Console.WriteLine("The specified font 'SpecialFontName' was not found in the PDF.");
}
Add Font
Use the Add
method to add both a standard font and a font file as byte data. The method accepting the font name only accepts one of the 14 standard fonts. Adding a standard font does not embed it, as the standard font is already guaranteed 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 an existing PDF file and create a PdfDocument object
// Make sure that 'sample.pdf' is present at the specified location.
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Add the Helvetica font to the PDF document
// Ensure that the font exists in the system's font directory or provide the correct path to it.
pdf.Fonts.AddFamily("Helvetica");
// Note: This code assumes that you have the IronPdf library correctly installed
// and referenced in your project. The IronPdf library is a third-party library
// used for PDF manipulation in C#. The PdfDocument.FromFile method loads an
// existing PDF file into a PdfDocument object. The Fonts.AddFamily method adds
// a font family (such as Helvetica) to the PDF document, which can be used for
// styling text within the document. Ensure that the required font files are
// available on your system or specify the correct paths to them. Adjustments or
// additional code might be necessary based on the specific version of IronPdf
// and the requirements of your application.
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 System.IO;
using System.Linq;
// Import the PDF document from the specified file path.
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Note: Font embedding functionality might differ based on the library version.
// This example assumes the functionality is correct for IronPdf as described.
// Add the font by reading font data from a specified font file.
// Replace "dir/to/font.ttf" with the actual path to your specific font file.
byte[] fontData = File.ReadAllBytes("dir/to/font.ttf");
// Add the font data to the PDF document's font list.
pdf.Fonts.Add(fontData);
// Embed the last added font to ensure the PDF displays correctly across different systems.
// It assumes that the last added font in the list is the one we want to embed.
pdf.Fonts.Last().Embed();
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 file size. 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 the PDF document from a specified file location
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Retrieve the collection of fonts used in the PDF document
PdfFontCollection fonts = pdf.Fonts;
// Check if we have at least one font in the collection to unembed
if (fonts.Count > 0)
{
// Unembed the first font in the collection
// This means the font will not be included in the PDF file
// The viewing application will use a default or system font instead.
// Unembedding a font can be useful for reducing the file size of the PDF.
fonts[0].Unembed();
}
else
{
// Handle the case where no fonts are embedded in the PDF
Console.WriteLine("No fonts to unembed in the PDF document.");
}
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 must ensure that the new font aligns well with the original font.
Before proceeding
:path=/static-assets/pdf/content-code-examples/how-to/manage-font-replace-font.cs
using IronPdf;
using System.IO;
// Load an existing PDF document from a file
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Load the binary data of a TrueType font from a file
byte[] fontData = File.ReadAllBytes("dir/to/font.ttf");
// Replace the 'Courier' font in the PDF document with the new font loaded from the file
if (pdf.Fonts.TryGetValue("Courier", out PdfFont font))
{
font.ReplaceWith(fontData);
}
else
{
// Handle the case where the font is not found in the PDF
// This could involve logging an error or notifying the user
Console.WriteLine("The 'Courier' font was not found in the PDF document.");
}
// Note:
// 1. Ensure the "sample.pdf" file exists at the specified path.
// 2. Ensure the font file "dir/to/font.ttf" exists at the specified path.
// 3. IronPdf must be correctly referenced in your project to use PdfDocument class and related methods.
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
Frequently Asked Questions
What is a font in the context of PDFs?
A font is a set of characters, symbols, and glyphs with a consistent style and design used in typography to display text in a visually appealing and coherent manner.
How can I manage fonts in a PDF using IronPDF?
IronPDF provides functionalities such as finding, getting, embedding, unembedding, and replacing fonts within a PDF document.
How do I find and retrieve fonts in a PDF using IronPDF?
Access the Fonts property of a PdfDocument to get the PdfFontCollection object, which contains all the fonts in the document. You can then iterate over this collection to find and inspect specific fonts.
How do I embed a font in a PDF with IronPDF?
To embed a font, retrieve the specific font from the PdfFontCollection and use the Embed method to include the font's byte stream data in the PDF document.
What is the purpose of unembedding a font in a PDF?
Unembedding a font removes the font's embedded byte stream data from the PDF, reducing the file size while potentially sacrificing visual consistency if the font is not available on the viewer's system.
How can I replace a font in a PDF using IronPDF?
Use the Replace method of the PdfFontCollection to substitute an existing font with a new one, ensuring the new font aligns well with the original in terms of styling and character encoding.
What are the 14 standard fonts in PDFs?
The 14 standard fonts in PDFs, also known as 'Base 14 Fonts' or 'Standard Type 1 Fonts,' include Courier, Helvetica, and Times-Roman, among others. These fonts are widely supported and don't need to be embedded in the document.
Can I add custom fonts to a PDF using IronPDF?
Yes, you can add custom fonts to a PDF by using the Add method with font file data. This allows you to include fonts beyond the standard set.