Watermark messing up in PDF - .NET Core

Hi everyone,
I am trying the version 24.8.0 to evaluate GroupDocs.Watermark.
I have a web application (.NET Core 6) and I am trying to insert watermark in MemoryStream.

Here is the actual code I am using:

public static async Task<string> UploadFromFileAsync(IFormFile file, string userId)
{
    string response = "success";
    string fileName = $"{userId.ToLower()}/{file.FileName}";
    BlobContainerClient blobContainerClient = await GetBlobContainer(true);
 
    using (var memoryStream = new MemoryStream())
    {
        file.CopyTo(memoryStream);
        memoryStream.Position = 0;
 
        // GroupDocs Start
        using (Watermarker watermarker = new Watermarker(memoryStream))
        {
            // watermarking goes here
            // Initialize the font to be used for watermark
            Font font = new Font("Calibri", 60, FontStyle.Bold);
            TextWatermark watermark = new TextWatermark("Converted by AI, not original controlled document", font);
 
            // Specify font color and text opacity, rotation and alignments
            watermark.ForegroundColor = Color.LightGray;
            watermark.Opacity = 0.5;
            watermark.HorizontalAlignment = GroupDocs.Watermark.Common.HorizontalAlignment.Center;
            watermark.VerticalAlignment = GroupDocs.Watermark.Common.VerticalAlignment.Center;
            watermark.RotateAngle = 315;
 
            watermarker.Add(watermark);
 
            // Saves the document to the underlying source (stream or file)
            watermarker.Save();
            memoryStream.Position = 0;
        }
        // GroupDocs End
 
        try
        {
            _ = await blobContainerClient.UploadBlobAsync(fileName, memoryStream, default);
        }
        catch (RequestFailedException ex)
        {
            response = ex.ErrorCode;
        }
        catch (Exception ex)
        {
            response = ex.Message;
        }
 
    }
    return response;
}

PDF Result:
image.png (53.1 KB)

Watermark looks fine in all other document types except PDF files. In PDF document, watermark messing up. This is the only problem which I am facing. If it can be resolved asap, then I will go for paid one.

Thanks

@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-1828

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.
We’ll notify you if any additional details are required.

Thanks for your quick reply.
Actually, I am developing a solution for one of my clients. I had a discussion over email with sales team to choose the right license and they suggested me GroupDocs.Watermark for .NET Developer OEM license. Client also agrees with this license but before purchase it he asked me to evaluate it. It is just because we have a tight timeline and can’t take any risk that can lead to extend the timeline. So, I can’t purchase paid support at this time.

I am facing this issue only with PDF documents. Also, I have tested the same code by creating a desktop application (WinForms, .NET Core 6) with local (not stream) files and it is working fine. Basically, issue is with memory stream.

It will be appreciated if you can fix it asap or even can share a clue to fix this in my code.

Thanks.

Hi @ksgbnl
We are trying to reproduce the mentioned problem, but so far without success. I am attaching an example of the original PDF file and the file with the result.
original.pdf (116.9 KB)

result.pdf (375.9 KB)

The result file was obtained using the following sample code with a stream in the Net6 console application:

string filePath = @"c:\tests\original.pdf";
string outFilePath = @"c:\tests\result.pdf";

using (var stream = new MemoryStream())
{
    using (FileStream fs = File.OpenRead(filePath))
    {
        fs.CopyTo(stream);
    }

    using (var watermarker = new Watermarker(stream))
    {
        // watermarking goes here
        // Initialize the font to be used for watermark
        Font font = new Font("Calibri", 60, FontStyle.Bold);
        TextWatermark watermark = new TextWatermark("Converted by AI, not original controlled document", font);

        // Specify font color and text opacity, rotation and alignments
        watermark.ForegroundColor = Color.LightGray;
        watermark.Opacity = 0.5;
        watermark.HorizontalAlignment = GroupDocs.Watermark.Common.HorizontalAlignment.Center;
        watermark.VerticalAlignment = GroupDocs.Watermark.Common.VerticalAlignment.Center;
        watermark.RotateAngle = 315;

        watermarker.Add(watermark);

        watermarker.Save();
        stream.Position = 0;
    }

    using (var fs = new FileStream(outFilePath, FileMode.Create, FileAccess.Write))
    {
        stream.WriteTo(fs);
    }
}

Does this code work properly for your PDF document? Anyway, we will continue the investigation and try to reproduce using Net 6 Web API project.

As I suggested, it is working fine with local files using desktop and console application. It can be reproduced with web application. Please try.

@ksgbnl I have attached an archive with an example Net 6 web api application. I uploaded different pdf files through the swagger and result looks like i sent in the previous message.
Can you try to run that app and upload your document through the swagger and check result? If it’s still a mess, we may have to ask you to provide us with your PDF file to make sure that its not document specific issue.

WebApp.zip (2.9 KB)

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.