FontNotFoundException in MacOS

Hi Team,
I was testing groupdocs watermark in MacOS with custom font family. I have installed ttf font by double clicking the file and followed UI prompts. When i set the font family name in the Font() constructor and tried getting FontNotFoundException. How to make sure Groupdocs watermark detect the font. I can see java loads the font but watermark SDK giving error. Regular installation not seems to be working.

JRE Version: 17
Watermark SDK: 21.3
OS: Mac Sequoia 15.6

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();

        // Get a list of all available font family names
        Font[] fontNames = ge.getAllFonts();

        // Print all the font names
        System.out.println("Available Fonts:");
        for (Font fontName : fontNames) {
            if (fontName.getFontName().contains("Roboto")) {
                System.out.println(">>>>>>>>>>>>>>>>" + fontName.getStyle());
            }
        }
Caused by: com.groupdocs.watermark.exceptions.FontNotFoundException
	at app//com.groupdocs.watermark.internal.aU.a(Unknown Source)
	at app//com.groupdocs.watermark.internal.aI.a(Unknown Source)
	at app//com.groupdocs.watermark.internal.aI.a(Unknown Source)
	at app//com.groupdocs.watermark.internal.bV.a(Unknown Source)
	at app//com.groupdocs.watermark.internal.bV.a(Unknown Source)
	at app//com.groupdocs.watermark.internal.bV.a(Unknown Source)
	at app//com.groupdocs.watermark.internal.bV.a(Unknown Source)
	at app//com.groupdocs.watermark.contents.ContentPart.addWatermark(Unknown Source)
	at app//com.groupdocs.watermark.contents.PdfContent.add(Unknown Source)
	at app//com.groupdocs.watermark.Watermarker.add(Unknown Source)
	at app//com.groupdocs.watermark.Watermarker.add(Unknown Source)
	at app//com.zscaler.unimask.watermark.WatermarkDataMasker.lambda$waterMark$2(WatermarkDataMasker.java:177)
	at java.base@17.0.15/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
	at java.base@17.0.15/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base@17.0.15/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base@17.0.15/java.util.AbstractList$RandomAccessSpliterator.forEachRemaining(AbstractList.java:720)
	at java.base@17.0.15/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base@17.0.15/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base@17.0.15/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
	at java.base@17.0.15/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
	at java.base@17.0.15/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base@17.0.15/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at app//com.zscaler.unimask.watermark.WatermarkDataMasker.waterMark(WatermarkDataMasker.java:179)
	at app//com.zscaler.unimask.watermark.WatermarkDataMasker.maskUnstructured(WatermarkDataMasker.java:141)
	... 87 more

@shiva.k

It seems you’re encountering a FontNotFoundException while using the GroupDocs Watermark SDK on macOS with a custom font. Here are some steps you can take to troubleshoot and resolve this issue:

  1. Verify Font Installation: Ensure that the font is correctly installed on your macOS. You can check this by opening the Font Book application and confirming that the font appears there.

  2. Font Family Name: Make sure that the font family name you are using in the Font() constructor matches exactly with the name displayed in the Font Book. Sometimes, the name used in code may differ slightly from the displayed name.

  3. Font Cache: Java may not be recognizing the newly installed font due to caching issues. You can try clearing the font cache by restarting your application or, in some cases, restarting your macOS.

  4. Set Font Folder: If the font is still not recognized, you can explicitly set the font folder in your code. Here’s how you can do it:

    FontConfigs.setFontFolder("/Library/Fonts", true); // or the path where your font is installed
    
  5. Check Available Fonts: Use the following code snippet to list all available fonts and ensure that your custom font appears in that list. This will help confirm whether Java can access the font:

    String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
    for (String fontName : fontNames) {
        System.out.println(fontName);
    }
    
  6. Fallback Font: If the custom font is not found, consider using a fallback font in your watermarking code to avoid the exception.

If you follow these steps and still face issues, please provide additional details about the font you are using and any other relevant code snippets. This will help in diagnosing the problem further.

Feel free to reach out if you need further assistance!

Hi @shiva.k ,
Thank you for your question.

Could you please share a sample of your GroupDocs.Watermark code where the issue occurs? Additionally, if possible, please provide a copy of the font file you’re using so we can test it on our side. Sometimes, a font’s internal family name differs from the file name, so it’s worth double-checking that the name you pass to the Font constructor matches the actual font family defined inside the file.

For your reference, the library also supports using custom fonts that are not installed in the system. You can do this by specifying both the font name and the folder containing the font file using the special Font constructor:

You can also find more details in this blog post:

(The examples there are in C#, but the concept is the same for Java.)

Please let us know once you share the details — we’ll be happy to help you further.