Adding watermarks to PDF gives font not found exception in Linux using .NET

Product Version: GroupDocs.Watermark for NET 20.7.0
OS: Ubuntu 20.04

I’ve installed the Adobe “Source Hans Sans” font to add some Chinese watermark text to a PDF file, however I’m keep getting the FontNotFoundException error.

GroupDocs.Watermark.Exceptions.FontNotFoundException: Exception of type 'GroupDocs.Watermark.Exceptions.FontNotFoundException' was thrown.
   at   .(TextState , TextWatermark )
   at   .     (TextWatermark , PdfPage ,    )
   at        (Watermark , ContentPart ,    )
   at  .(ContentPart , Watermark ,    , IDictionary`2)
   at  .(ContentPart , Watermark ,    , IDictionary`2)
   at  (ContentPart , Watermark ,    , )
   at  .     (ContentPart , Watermark ,    )
   at GroupDocs.Watermark.Contents.ContentPart.zeumdk2eqe4dcdkfe89auyve5hpn4u5v   (Watermark )
   at GroupDocs.Watermark.Watermarker.Add(Watermark watermark, WatermarkOptions options)
   at GroupDocs.Watermark.Watermarker.Add(Watermark watermark)

Then I changed the font to the per-installed ones like “DejaVu Sans”, still get the FontNotFoundException,

Here is the code snippet:

                PdfLoadOptions loadOptions = new PdfLoadOptions();
                using (Watermarker watermarker = new Watermarker(file, loadOptions))
                {
                    TextWatermark watermark = new TextWatermark("中文测试", new Font("Source Han Sans SC", 24));
                    watermark.ForegroundColor = Color.Gray;
                    watermark.HorizontalAlignment = HorizontalAlignment.Center;
                    watermark.VerticalAlignment = VerticalAlignment.Center;
                    watermark.IsBackground = true;
                    watermark.RotateAngle = -45;
                    watermark.ScaleFactor = 1;
                    watermark.Opacity = 0.5;

                    PdfAnnotationWatermarkOptions options = new PdfAnnotationWatermarkOptions();
                    watermarker.Add(watermark, options);
              }

I‘ve used the same font(Source Han Sans SC) successfully added watermarks to docx,xlsx,pptx,vsdx and images files, and the Chinese characters correctly rendered, only PDF file have problems, throws FontNotFoundException.

How do I add Chinese text watermark to PDF on Linux? Is there any recommended/supported fonts?

1 Like

@wangfu91

We’re investigating this scenario. Your investigation ticket ID is WATERMARKNET-1302. As there’s any further update, you’ll be notified.

It’s been 6 months, is there any updates on the progress? or are there any workarounds?

@wangfu91

We are sorry for the inconvenience.

The workaround for now is to use TrueType fonts. Once OpenType fonts is supported, we’ll let you know.

1 Like

@Atir_Tahir

The workaround for now is to use TrueType fonts.

I tried manually installed a TrueType font on Ubuntu 20.04 but got the same error.

$ sudo mv msyh.ttc /usr/share/fonts/truetype
$ sudo fc-cache -f -v
$ fc-list
/usr/share/fonts/truetype/msyh.ttc: Microsoft YaHei,微软雅黑:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
$ file ./msyh.ttc
./msyh.ttc: TrueType font collection data, 2.0, 2 fonts, digitally signed, at 0x20 TrueType Font data, 25 tables, 1st "GDEF"
      GroupDocs.Watermark.Exceptions.FontNotFoundException: Exception of type 'GroupDocs.Watermark.Exceptions.FontNotFoundException' was thrown.
         at   .(TextState , TextWatermark )
         at   .     (TextWatermark , PdfPage ,    )
         at        (Watermark , ContentPart ,    )
         at  .(ContentPart , Watermark ,    , IDictionary`2)
         at  .(ContentPart , Watermark ,    , IDictionary`2)
         at  (ContentPart , Watermark ,    , )
         at  .     (ContentPart , Watermark ,    )
         at GroupDocs.Watermark.Contents.ContentPart.zeumdk2eqe4dcdkfe89auyve5hpn4u5v   (Watermark )
         at GroupDocs.Watermark.Watermarker.Add(Watermark watermark, WatermarkOptions options)

@wangfu91

We’re further looking into this issue. We’ll notify you about the outcomes ASAP.

3 posts were split to a new topic: Loading Custom Fonts using C# in Windows and Linux

@wangfu91

GroupDocs.Watermark version 24.4 provides support to configure folder path with custom folders:

// Specify the folder path where your custom fonts are located
string fontsFolder = @"c:\CustomFonts\";

// Create a Font object by specifying the font name and the fonts folder path
Font font = new Font("Custom Font name", fontsFolder, 36);

// Create the watermark object using the TextWatermark class and provide the text and font
TextWatermark watermark = new TextWatermark("Test watermark", font);

Complete sample can be found in the our GitHub repository.