How to load a PDF document and get missing fonts in C# and .NET


After the loading process of the PDF documents, get the names of fonts that are not installed on your system use the FontSettingsMissingFonts property.

In addition, you can substitutes the original font with the name of an alternative font available on your system using the FontSettingsAddFontSubstitutes method.

The code example below shows how to get the names of fonts that are not installed on the system and substitutes these fonts with alternative font names.

Complete code

using System;
using System.IO;
using SautinSoft.Document;

namespace Example
{
    class Program
    {

        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            LoadPDF();
        }

        /// <summary>
        /// Load a PDF document and get missing fonts.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/load-pdf-document-get-missing-fonts-net-csharp-vb.php
        /// </remarks>
        static void LoadPDF()
        {            
            string inpFile = @"..\..\..\fonts.pdf";
            string outFile = "Result.docx";

            // Some PDF documents can use rare fonts which isn't installed in your system.
            // During the loading of a PDF document the component tries to find all necessary 
            // fonts installed in your system.
            // In case of the font is missing, you can find its name in the property 'MissingFonts' (after the loading process).
            DocumentCore dc = DocumentCore.Load(inpFile);
            
            if (SautinSoft.Document.FontSettings.MissingFonts.Count > 0)
            {
                Console.WriteLine("Missing Fonts:");
                foreach (string fontFamily in SautinSoft.Document.FontSettings.MissingFonts)
                    Console.WriteLine(fontFamily);
            }

            // Next, knowing missing fonts, you can install these fonts in your system.

            // Also, you can specify an extra folder where component should find fonts.
            SautinSoft.Document.FontSettings.UserFontsDirectory = @"d:\My Fonts";

            // Furthermore, you can add font substitutes, to use alternative fonts.            
            SautinSoft.Document.FontSettings.AddFontSubstitutes("Melvetika", "Segoe UI");
            Console.WriteLine("We\'ve changed Melvetica font to Segoe UI");

            // Load the document again.
            dc = DocumentCore.Load(inpFile);
            dc.Save(outFile);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
            Console.ReadKey();
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft.Document

Namespace Example
	Friend Class Program

		Shared Sub Main(ByVal args() As String)
			LoadPDF()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Load a PDF document and get missing fonts.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/load-pdf-document-get-missing-fonts-net-csharp-vb.php
		''' </remarks>
		Private Shared Sub LoadPDF()
			Dim inpFile As String = "..\..\..\fonts.pdf"
			Dim outFile As String = "Result.docx"

			' Some PDF documents can use rare fonts which isn't installed in your system.
			' During the loading of a PDF document the component tries to find all necessary 
			' fonts installed in your system.
			' In case of the font is missing, you can find its name in the property 'MissingFonts' (after the loading process).
			Dim dc As DocumentCore = DocumentCore.Load(inpFile)

			If SautinSoft.Document.FontSettings.MissingFonts.Count > 0 Then
				Console.WriteLine("Missing Fonts:")
				For Each fontFamily As String In SautinSoft.Document.FontSettings.MissingFonts
					Console.WriteLine(fontFamily)
				Next fontFamily
			End If

			' Next, knowing missing fonts, you can install these fonts in your system.

			' Also, you can specify an extra folder where component should find fonts.
			SautinSoft.Document.FontSettings.UserFontsDirectory = "d:\My Fonts"

			' Furthermore, you can add font substitutes, to use alternative fonts.            
			SautinSoft.Document.FontSettings.AddFontSubstitutes("Melvetika", "Segoe UI")
			Console.WriteLine("We've changed Melvetica font to Segoe UI")

			' Load the document again.
			dc = DocumentCore.Load(inpFile)
			dc.Save(outFile)

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
			Console.ReadKey()
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.