Watermark messing up in PDF - .NET Core

I have tested with API project and working fine with PDF files. Please give me some time to figure out that what is wrong in my code. Thanks again for your quick support.

Finally, I am able to identify the issue.
GroupDocs working fine, the issue is with my application. Just for detailed information, I am using Azure Document Translation service, and I was adding watermark before translation. After translation done by service, watermark is messing up in the translated document. So, the issue is with Azure service which can’t handle the watermarks in PDF files.

Now I will try another approach like will add watermark to translated documents and will give a demo to the client. Once he will satisfy, will go to buy the suggested license.

I really like your support. Even it is free a support, you are paying attention quicky on the issues. I am impressed :slight_smile: :heart:

1 Like

@ksgbnl Thank you for your feedback and we are glad that you found the cause of the problem. Actually GroupDocs.Watermak library supports 3 different watermark types for the PDF document. By default it uses XObject type (you can find more details about pdf watermark types in our documentation Watermarks in PDF document | Documentation (groupdocs.com).

So you can try to update your sample code (which uses Azure) with next lines:

var options = new PdfAnnotationWatermarkOptions();
//var options = new PdfArtifactWatermarkOptions();
watermarker.Add(watermark, options); //instead of watermarker.Add(watermark)

Check the result with Annotation watermark type and then with Artifact type. Probably Azure Document Translation service will handle them more properly. But of course, even if it works, such approach will depend from the Azure service.

I have implemented the code for translated documents, and everything looks fine.

Tomorrow I will try suggested rest watermark types for PDF document as well and will let you know the result for your future reference.

Thanks

1 Like

Hi, I am able to insert watermark in all files on local (Visual Studio local debug) but when I tried to deploy the application on Azure App Services then it stopped working.
Throwing following exception after deploying:

System.InvalidOperationException: Only 10 documents can be loaded per application run in evaluation mode.

image.png (40.9 KB)

@ksgbnl

It looks like you are evaluating the API in trial mode. Please note that the API has evaluation limitations. However, you can avail a temporary license (it’s time-restricted full license).

As you suggested, I have applied for temporary license but did not receive any email on business email yet. Waiting for email.

@ksgbnl

If you haven’t received the temporary license yet, you can raise this issue on the purchase forum.

I have received the temporary license file, and I have included and configured that but after deploying on Azure web services, I am getting below exception:

GroupDocs.Watermark.Exceptions.FontNotFoundException: Exception of type ‘GroupDocs.Watermark.Exceptions.FontNotFoundException’ was thrown.

Any idea?

Here is the log screenshot:
image.png (31.9 KB)

@ksgbnl

The FontNotFoundException occurs when the font specified in the TextWatermark is not installed on the machine where the application is deployed. For instance, if the code provided earlier includes:

Font font = new Font("Calibri", 60, FontStyle.Bold);

This indicates that the “Calibri” font is not installed on the machine.

To resolve this, you should either use fonts that are installed on the system or include custom fonts in your project. You can follow the example in this guide to use custom fonts: Using Custom Fonts with Text Watermarks.

I have tried the custom fonts. Custom fonts are working on local (local system debug) but throwing the same exception when deployed to Azure app service.

Please check this screenshot of my code:
image.png (134.3 KB)

@ksgbnl

C an you provide details about your Azure App Service environment (OS, Windows/Linux), how you’re deploying the app, and how custom fonts are included (e.g., CSS @font-face or stored in the project)? Also, can you share the full exception message, deployment logs, and check for any browser console errors related to fonts?

@ksgbnl Have you tried your code using a docker linux container or debugging on windows on your local machine ? I tried setting up the folder path like in your example

path = “/fonts/”

and it doesn’t work for me on either windows or linux. I assume your code works on your local machine because “calibri” is installed on your machine. Can you please check for example your code with this font GroupDocs.Watermark-for-.NET/Examples/Resources/SampleFiles/Fonts at master · groupdocs-watermark/GroupDocs.Watermark-for-.NET (github.com), it has name “OT Chekharda Bold Italic”.
Also please make sure that font file marked as always copy to the output directory.
Try to configure folder path using relative path:

var path = @“./fonts”;

I tested this in a web API application, it works in the Windows debugger and a Linux Docker container.

Thanks for looking into it. I have deployed my application on Azure app service (Linux OS) directly using the Visual Studio 2022 publish option. I am not using any container. I have tried my code using debugging on windows on my local machine and it is working fine.
Let me try it by changing the path variable and copy to the output directory. I will let you know the result soon.
Thanks.

1 Like

Hi, I have resolved FontNotFoundException exception issue using

var path = Path.Combine(env.WebRootPath, "fonts");

but now I am getting new exception below:

The type initializer for ‘Gdip’ threw an exception.
System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms.

Here is full exception:

2024-09-20T17:23:33.4009495Z fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
2024-09-20T17:23:33.4010385Z       An unhandled exception has occurred while executing the request.
2024-09-20T17:23:33.4010436Z       System.TypeInitializationException: The type initializer for 'Gdip' threw an exception.
2024-09-20T17:23:33.4017807Z        ---> System.PlatformNotSupportedException: System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information.
2024-09-20T17:23:33.4018699Z          at System.Drawing.LibraryResolver.EnsureRegistered()
2024-09-20T17:23:33.4022903Z          at System.Drawing.SafeNativeMethods.Gdip.PlatformInitialize()
2024-09-20T17:23:33.4023131Z          at System.Drawing.SafeNativeMethods.Gdip..cctor()
2024-09-20T17:23:33.4023205Z          --- End of inner exception stack trace ---
2024-09-20T17:23:33.4027011Z          at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, IntPtr scan0, IntPtr& bitmap)
2024-09-20T17:23:33.4029021Z          at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
2024-09-20T17:23:33.4029079Z          at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
2024-09-20T17:23:33.4029110Z          at .(String , Font , Thickness )
2024-09-20T17:23:33.4032773Z          at GroupDocs.Watermark.Watermarks.TextWatermark.d6j8hma6hg6ubbavbtg9zghtays3vwqhe()
2024-09-20T17:23:33.4032990Z          at .()
2024-09-20T17:23:33.4033056Z          at .()
2024-09-20T17:23:33.4033091Z          at .(TextWatermark , PdfPage ,  )
2024-09-20T17:23:33.4033122Z          at .e(TextWatermark , PdfPage ,  )
2024-09-20T17:23:33.4187338Z          at .e(Watermark , ContentPart ,  )
2024-09-20T17:23:33.4187694Z          at .(ContentPart , Watermark ,  , IDictionary`2 )
2024-09-20T17:23:33.4187736Z          at .(ContentPart , Watermark ,  , IDictionary`2 )
2024-09-20T17:23:33.4187775Z          at .(ContentPart , Watermark ,  ,  )
2024-09-20T17:23:33.4187809Z          at GroupDocs.Watermark.Contents.ContentPart.zeumdk2eqe4dcdkfe89auyve5hpn4u5ve(Watermark )
2024-09-20T17:23:33.4187899Z          at GroupDocs.Watermark.Watermarker.Add(Watermark watermark, WatermarkOptions options)
2024-09-20T17:23:33.4187933Z          at GroupDocs.Watermark.Watermarker.Add(Watermark watermark)

Please check and provide a solution asap as I am too late to give a demo to client.


UPDATE:
I can understand that System.Drawing.Common has some limitations in respect of cross platform. So, I followed the Microsoft documentation, see Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn

This solution seems working, but application keeps crashing. Please suggest me if there is any other solution.

@ksgbnl
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WATERMARKNET-1845

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @ksgbnl

System.PlatformNotSupportedException relates to the System.Drawing.Common limitation on the non Windows environments. This can be fixed by adding the following code to the .csproj file:

	<ItemGroup>
		<RuntimeHostConfigurationOption Include="System.Drawing.EnableUnixSupport" Value="true" />
	</ItemGroup>

The message The type initializer for ‘Gdip’ threw an exception appears for the next reason. The System.Drawing.Common NuGet package works on Windows, Linux, and macOS. However, there are some platform differences. On Linux and macOS, the GDI functionality is implemented by the libgdiplus library, so it should be installed separately on Linux machine .NET Standard 2.0 API Limitations | Documentation

This sample show how to add libgdiplus and some fonts in the docker Linux container.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

#adding libgdiplus, libc6-dev
RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev

#fonts
RUN sed -i'.bak' 's/$/ contrib/' /etc/apt/sources.list
RUN apt-get update; apt-get install -y ttf-mscorefonts-installer fontconfig
RUN fc-cache -f -v

....

PS. As you mentioned that you use Azure App service so you can follow this advice Linux App Service - ‘System.Drawing.GDIPlus’ threw an exception. Unable to load shared library ‘gdiplus’ or one of its dependencies - Microsoft Q&A how to add libgdiplus in it.

1 Like

Thank you so much for providing the solutions. Now I am able to add watermark successfully.

One last small issue, watermark overlapping the document text. In Word document everything is fine but only in Excel and PDF I am facing this issue.

Here are the screenshots of PDF and Excel file screenshots:
pdf.png (163.8 KB)
excel.png (211.2 KB)

@ksgbnl please try to enable such option in the watermark

watermark.IsBackground = true;

It works properly for the pdf files. For excel it’s not possible to create shape behind the cells How do I position a picture BEHIND cells? - Microsoft Community
We will investigate more about Excel.

1 Like