How to Manage Fonts in HTML-to-PDF Conversion

Managing Fonts in PDF Documents Using Iron PDF

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.

Code Example

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.");
    }
}
$vbLabelText   $csharpLabel
  • Make sure to install IronPDF to work with the above code.
  • The PdfDocument.GetFonts() method retrieves all fonts used in your document, and you can list them with a simple loop.
  • Use PdfFont.EmbedFromFile() to add new font files to your PDF.
  • Unembedding fonts can help reduce file size, particularly useful for emails or uploads where size is limited.
  • The ReplaceFont() method allows replacing existing fonts with your custom ones.

Further Reading: How to Manage Fonts in PDF

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.
< PREVIOUS
How to Convert Markdown to PDF
NEXT >
How to Manage Logins and Authentication Using C#