Watermark messing up in PDF - .NET Core

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