Invalid font name - error occurs on only a particular document

Hello,

When using Aspose 24.7 and (after upgrading to see if it has been fixed) 24.12 we are seeing an Invalid font name error in two areas of the code for a specific PDF.

This is one of the stack traces, and our highest priority to resolve.

com.aspose.pdf.internal.ms.System.lh: Invalid font name
	at com.aspose.pdf.FontCollection.get_Item(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.ly.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.ly.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.ly.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.ly.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.lk(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.<init>(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.<init>(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.internal.l7h.l0t.<init>(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.Redaction.redactText(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.Redaction.redactAreaInternal(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.facades.PdfAnnotationEditor.lI(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]
	at com.aspose.pdf.facades.PdfAnnotationEditor.redactArea(Unknown Source) ~[aspose-pdf-24.12.jar:24.12]

This is another area I could repro the error:

class com.aspose.pdf.internal.ms.System.lh: Invalid font name
com.aspose.pdf.FontCollection.get_Item(Unknown Source)
com.aspose.pdf.internal.l7j.ly.lI(Unknown Source)
com.aspose.pdf.internal.l7j.ly.lI(Unknown Source)
com.aspose.pdf.internal.l7j.ly.lI(Unknown Source)
com.aspose.pdf.internal.l7j.ly.lI(Unknown Source)
com.aspose.pdf.internal.l7j.l0t.lI(Unknown Source)
com.aspose.pdf.internal.l7j.l0t.lI(Unknown Source)
com.aspose.pdf.internal.l7j.l0t.lk(Unknown Source)
com.aspose.pdf.internal.l7j.l0t.<init>(Unknown Source)
com.aspose.pdf.internal.l7j.l0t.<init>(Unknown Source)
com.aspose.pdf.TextAbsorber.visit(Unknown Source)
com.aspose.pdf.Page.accept(Unknown Source)
com.aspose.pdf.PageCollection.accept(Unknown Source)

This is the problem PDF:

invalid-font-name-problem-document.pdf (159.1 KB)

Thank you for your help!

@shane.guymon

The “Invalid font name” error you are encountering with specific PDF documents can often be attributed to missing fonts that are not available on the system where the Aspose library is running. Here are some steps you can take to resolve this issue:

  1. Font Substitution: You can implement font substitution in your code. This allows you to specify a replacement font when the original font is not found. Here’s an example of how to do this in C#:

    using System;
    using System.IO;
    using Aspose.Pdf;
    using Aspose.Pdf.Text;
    
    namespace Aspose.Pdf.Examples.CSharp
    {
        public class ReplaceMissingFonts
        {
            public static void Run()
            {
                string dataDir = "path_to_your_documents_directory/";
                Document pdfDocument = new Document(dataDir + "input.pdf");
    
                // Add font substitution
                FontRepository.Substitutions.Add(new SimpleFontSubstitution("MissingFontName", "Arial"));
    
                pdfDocument.Save(dataDir + "output.pdf");
            }
        }
    }
    

    In this example, replace "MissingFontName" with the actual name of the font that is causing the issue, and "Arial" with a suitable replacement font.

  2. Check for Warnings: You can also set up a handler to get warnings for font substitutions. This can help you identify which fonts are being substituted and ensure that the replacements are appropriate. Here’s how you can implement this:

    using System;
    using Aspose.Pdf;
    
    namespace Aspose.Pdf.Examples.CSharp
    {
        public class GetWarningsForFontSubstitution
        {
            public static void Run()
            {
                string dataDir = "path_to_your_documents_directory/";
                Document pdfDocument = new Document(dataDir + "input.pdf");
    
                pdfDocument.FontSubstitution += new Document.FontSubstitutionHandler(OnFontSubstitution);
            }
    
            static void OnFontSubstitution(Font oldFont, Font newFont)
            {
                Console.WriteLine($"Font '{oldFont.FontName}' was substituted with '{newFont.FontName}'");
            }
        }
    }
    
  3. Verify Font Availability: Ensure that the fonts used in the PDF are installed on the machine where the Aspose library is being executed. If the fonts are not available, you may need to install them or use the substitution method as described above.

By following these steps, you should be able to handle the “Invalid font name” error effectively. If the problem persists, consider checking the specific PDF file for any unusual font references or corruption.

If you need further assistance, feel free to ask!

Sources:
[1]: ReplaceMissingFonts.cs
[2]: GetWarningsForFontSubstitution.cs