Issue setting watermark for pdf on Unix machine. Works on windows

I’m setting watermark for pdf documents using Aspose.Pdf nuget package(22.2.0).
My code works on my Windows NT machine, but when deployed to Unix(4.15.0.166), it gives error: “Font Arial was not found”

using (var pdfDoc = new Aspose.Pdf.Document(fileMemoryStream))

{

foreach (Aspose.Pdf.Page pdfPage in pdfDoc.Pages)
{
var artifact = new WatermarkArtifact();
artifact.SetTextAndState(
“Draft”,
new TextState()
{
FontSize = 12,
ForegroundColor = Aspose.Pdf.Color.Gray,
Font = FontRepository.FindFont(SellSideConstants.BulkEmail.WatermarkFontName, true)
});
artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
artifact.ArtifactVerticalAlignment = VerticalAlignment.Center;
artifact.Rotation = 45;
artifact.Opacity = 0.2;
pdfPage.Artifacts.Add(artifact);
}
}


I also tried to load the font from ttf file, but still get the same exception

@hemantsavant

We have logged an investigation ticket as PDFNET-51889 to further analyze this case. We will look into its details and let you know as soon as the ticket is resolved. Can you please also share the compete error stack trace with us so that we can also log it with the ticket?

We apologize for the inconvenience.

Hi Ali,
I was able to fix the problem by storing font file as .net assembly resource and adding path to it in FontRepository sources, so I’m able to set watermark for pdf file on Unix box.
You can close the issue.
Thank you.

1 Like

@hemantsavant

We are pleased to know that you have been able to resolve your issue. Please keep using our API and feel free to create a new topic in case you face any problems.

Hi Hemantsavant,

Did you add the embedded fonts to a MemoryFontSource or use FileFontSource?

Thanks!

@williamfa

Aspose.PDF for .NET requires windows fonts to work correctly and generate expected results. Therefore, we recommend installing msttcorefonts package in the non-Windows and Linux like environments. These fonts should be placed in “/usr/share/fonts/truetype/msttcorefonts” directory as Aspose.PDF for .NET scans this folder on Linux like operating systems. You can either install fonts or use below line of code to set the font directory for the API:

Aspose.Pdf.Text.FontRepository.Sources.Add(new FolderFontSource("<user's path to ms fonts>"));

Furthermore, we have already opened an investigation ticket and attached it to your original post where you will be notified once it is resolved.