Time adding watermark to a PDF programatically using c# .NET

We have downloaded the version 24.5 to evaluate it.
We have been testing the time it takes adding watermarks to a PDF file used as a manual (in this case a file with more than 200 pages with images, index, etc). And it takes more than 2 minutes adding the watermark.
For us, it is too long time.
We had a previous version 18.8, but it creates problems when we add watermaks to Word documents (it added a line per each Watermark in each page).
Can we check if the time adding the watermark in PDF is less in a non-evaluation version?
We are interested in it, but this time is excesive for us.

@MikelGB

Could you please share the problematic file (if it’s a large file, compress it to ZIP, upload to Google Drive and share link ) and the sample Watermark code? We’ll then investigate this issue at our end.

There are API evaluation limitations. We could further check it if you share sample file and the code with us.

We have been adding the Watermark to the PDF with a code as:

            PdfLoadOptions loadOptions = new PdfLoadOptions();
            using (Watermarker watermarker = new Watermarker(fileToFillPath, loadOptions))
            {
                // Build the text to be written in the Watermark
                string strWatermark = GetStringWatermark(fileToFillPath);

                PdfArtifactWatermarkOptions options = new PdfArtifactWatermarkOptions();
                // Create text watermark

                //initialize the font to be used for watermark1
                GroupDocs.Watermark.Watermarks.Font font = new GroupDocs.Watermark.Watermarks.Font("Arial", 40, GroupDocs.Watermark.Watermarks.FontStyle.Regular);
                TextWatermark textWaterMark = new TextWatermark(strWatermark, font);
                textWaterMark.ForegroundColor = GroupDocs.Watermark.Watermarks.Color.Blue;
                textWaterMark.BackgroundColor = GroupDocs.Watermark.Watermarks.Color.Beige;
                textWaterMark.HorizontalAlignment = GroupDocs.Watermark.Common.HorizontalAlignment.Center;
                textWaterMark.VerticalAlignment = GroupDocs.Watermark.Common.VerticalAlignment.Top;
                textWaterMark.TextAlignment = TextAlignment.Center;
                textWaterMark.Opacity = 0.3;
                textWaterMark.RotateAngle = 0;
                textWaterMark.Margins = new Margins(MarginType.RelativeToParentDimensions, 0.1, 0.1, 0.1, 0.1);

                textWaterMark.SizingType = SizingType.ScaleToParentDimensions;
                textWaterMark.ScaleFactor = 0.3;

                //apply the watermark
                watermarker.Add(textWaterMark, options);

                //save the changes
                watermarker.Save();
            }

And the example PDF to add the Watermark is: manual-dlp-example.zip - Google Drive

This task (jusy adding the watermark) takes a minute for us, and more actions are executed later. Adding the watermark would require most the time, and too long.

Can we optimize the action of adding the watermark? Or the WM added in the evaluation mode of exacution adds too much time? Or, perhaps, is there a size limite for a right execution?

We are analyzing the library to see if it is Ok for us to adquire it.

@MikelGB

We followed the code and problematic file that you shared. It took almost 25 seconds with API version 24.6, please take a look at this screenshot (138.2 KB).
Could you please share a sample application to reproduce the issue? Also share the development environment details (e.g. OS details, .NET version).

Hi,
In a Word document, the Watermark can be associated with HeaderFooter and it is assigned much faster.
I need for PDF to add a Dynamic Watermark (added each time the file is accessed). I had this with GrupDocs.Watermark version 18.8 and there was an option faster, calling to:
//apply the watermark
foreach (var page in doc.Pages)
{
page.AddAnnotationWatermark(textWaterMark, false);
}

I test it and it was executed in a faster way in PDF (in version 18.8). But i see it can not be implemented in this way now.
But version 18.8 had problems to add Watermarks in Word files.

Regards,

Mikel

@MikelGB

Could you please share a simple console application to add Watermarks dynamically in PDF? We need to see how you are doing it and please also mention the number of PDF files you are processing and how much time it takes for each.
Moreover, we would like to add that if you utilize API without license, you face some limitations. Please request a temporary license here and try the web app again.

Hello @MikelGB

Thank you for using our product.
Let me update you, that during the trial limitations each document page will have a trial watermark that potentially slows the process of saving the file. I would recommend ordering a temporary license and ensuring big documents can be processed with better performance.
Also please be aware that the latest version has new Tiling options and other new features.
We will try to investigate the shared document and get back to you shortly.

Hello,

In the response Atir sent me a response a week ago, he informed that it took almost 26 seconds for him to add a Watermark to this PDF with an API 24.6 (it is a very long time for us).
I understand he executed with full license.
I do not understand why the executions was faster in older versions for PDF files (but this older versions created problems with Word files).
And in little PDF files it takes to me 5 or 6 seconds only for adding the Watermark, so muy program would require more time for Watermarking than for all the tasks we must execute in our program.

HI @MikelGB

That’s right, but execution time depends of the particular running machine parameters.

I asked the GroupDocs.Watermark’ lead developer to run it and it took about 10 seconds. So maybe the best way would be to request a temporary license and ensure the product performs appropriately for your workflow on your server equipment.

@MikelGB

We did tests using GroupDocs.Watermark version 18.8 and the latest version 24.6, and i got next results for small pdf:

version 18.8 without applying license:
Execution #1. RunTime 00:00:04.32
Execution #2. RunTime 00:00:00.03
Execution #3. RunTime 00:00:00.04

version 24.6 without applying license:
Execution #1. RunTime 00:00:05.92
Execution #2. RunTime 00:00:00.06
Execution #3. RunTime 00:00:00.11

So in both cases, only the first execution takes around 5 seconds, because during the first executions assembly initialization is required. This is sample code (for version 24.6):

AddWatermark(1);
AddWatermark(2);
AddWatermark(3);

Console.WriteLine();
Console.WriteLine("All done.");
Console.ReadKey();


static void AddWatermark(int executionNo)
{
    string inputFileName = @"c:\small_document.pdf";
    string outputFileName = $@"c:\out_{executionNo}.pdf";

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();

    PdfLoadOptions loadOptions = new PdfLoadOptions();
    using (Watermarker watermarker = new Watermarker(inputFileName, loadOptions))
    {
        TextWatermark watermark = new TextWatermark("Watermark text", new Font("Arial", 19));
        watermark.ForegroundColor = Color.Blue;
        watermark.BackgroundColor = Color.Beige;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Top;
        watermark.TextAlignment = TextAlignment.Center;
        watermark.Opacity = 0.3;
        watermark.RotateAngle = 0;
        watermark.Margins = new Margins(MarginType.RelativeToParentDimensions, 0.1, 0.1, 0.1, 0.1);

        watermark.SizingType = SizingType.ScaleToParentDimensions;
        watermark.ScaleFactor = 0.3;

        var options = new PdfArtifactWatermarkOptions();
        //apply the watermark
        watermarker.Add(watermark, options);

        watermarker.Save(outputFileName);
    }

    stopWatch.Stop();
    // Get the elapsed time as a TimeSpan value.
    TimeSpan ts = stopWatch.Elapsed;

    // Format and display the TimeSpan value.
    string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);

    Console.WriteLine($"Execution #{executionNo}. RunTime " + elapsedTime);
}

@MikelGB

We just released GroupDocs.Watermark for .Net version 24.8, please take a look at the release notes. This release significantly improves performance when adding watermarks to the PDF documents (we also tested the large PDF file you shared earlier). This improvement also speeds up adding a watermark without license.
Moreover, you provided code using GroupDocs.Watermark 18.8 that adds watermark with type annotation (page.AddAnnotationWatermark(textWaterMark, false)) in the PDF.
The similar watermark type in the GroupDocs.Watermark 24.8 can be achieved using class PdfAnnotationWatermarkOptions, sample code:

// Create an instance of PdfLoadOptions to specify any loading options for the PDF file.
PdfLoadOptions loadOptions = new PdfLoadOptions();

// Use a using statement to ensure the Watermarker is disposed properly after use.
using (Watermarker watermarker = new Watermarker(inputFileName, loadOptions))
{
    // Create a TextWatermark instance with the specified text and font.
    TextWatermark watermark = new TextWatermark("Watermark text", new Font("Arial", 19));

    // Create options for the PDF annotation watermark.
    var options = new PdfAnnotationWatermarkOptions();

    // Add the watermark to the document with the specified options.
    watermarker.Add(watermark, options);

    // Save the watermarked PDF to the specified output file.
    watermarker.Save(outputFileName);
}