Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
This comprehensive tutorial demonstrates how to manage fonts in PDF documents using Iron PDF. Starting with installing the Iron PDF package via the NuGet Package Manager, the guide walks you through the process of setting up a console application. Key steps include importing a PDF, retrieving and displaying all fonts used, and specifying a font by name, such as Helvetica. The tutorial explains how to add new fonts, like Arial, to the document and embed custom fonts by specifying font file paths and reading font data. Additionally, the tutorial discusses unembedding fonts to reduce PDF size and replacing existing fonts with custom fonts. The final step involves saving the modified PDF, ensuring all changes are applied. The tutorial concludes by encouraging viewers to experiment with Iron PDF and subscribe for more informative videos.
Below is a C# code example to illustrate working with fonts in PDF using Iron PDF:
// Make sure to install IronPDF using NuGet Package Manager.
using System;
using IronPdf;
class Program
{
static void Main()
{
// Load an existing PDF document
var renderer = new ChromePdfRenderer();
var pdfDocument = PdfDocument.FromFile("example.pdf");
// Retrieve all fonts used in the PDF
var fonts = pdfDocument.GetFonts();
Console.WriteLine("Fonts used in the document:");
foreach (var font in fonts)
{
Console.WriteLine("- " + font.Name);
}
// Specify and use a specific font by name, e.g., "Helvetica"
var helveticaFont = new PdfFont("Helvetica");
// Add a new font (e.g., Arial) to the document
var arialFont = PdfFont.EmbedFromFile("path/to/arial.ttf");
// Embedding the custom Arial font in the document
pdfDocument.AddFont(arialFont);
Console.WriteLine("Arial font added.");
// Unembedding fonts to reduce PDF size
pdfDocument.UnembedFonts();
// Replacing existing fonts with custom ones
foreach (var font in fonts)
{
if (font.Name == "Helvetica")
{
pdfDocument.ReplaceFont(font, arialFont);
}
}
// Save the modified PDF to a new file
pdfDocument.SaveAs("modifiedExample.pdf");
Console.WriteLine("PDF saved with modified fonts.");
}
}
// Make sure to install IronPDF using NuGet Package Manager.
using System;
using IronPdf;
class Program
{
static void Main()
{
// Load an existing PDF document
var renderer = new ChromePdfRenderer();
var pdfDocument = PdfDocument.FromFile("example.pdf");
// Retrieve all fonts used in the PDF
var fonts = pdfDocument.GetFonts();
Console.WriteLine("Fonts used in the document:");
foreach (var font in fonts)
{
Console.WriteLine("- " + font.Name);
}
// Specify and use a specific font by name, e.g., "Helvetica"
var helveticaFont = new PdfFont("Helvetica");
// Add a new font (e.g., Arial) to the document
var arialFont = PdfFont.EmbedFromFile("path/to/arial.ttf");
// Embedding the custom Arial font in the document
pdfDocument.AddFont(arialFont);
Console.WriteLine("Arial font added.");
// Unembedding fonts to reduce PDF size
pdfDocument.UnembedFonts();
// Replacing existing fonts with custom ones
foreach (var font in fonts)
{
if (font.Name == "Helvetica")
{
pdfDocument.ReplaceFont(font, arialFont);
}
}
// Save the modified PDF to a new file
pdfDocument.SaveAs("modifiedExample.pdf");
Console.WriteLine("PDF saved with modified fonts.");
}
}
PdfDocument.GetFonts()
method retrieves all fonts used in your document, and you can list them with a simple loop.PdfFont.EmbedFromFile()
to add new font files to your PDF.ReplaceFont()
method allows replacing existing fonts with your custom ones.Further Reading: How to Manage Fonts in PDF