Performance Issue with Aspose.PDF Watermarking - Urgent Assistance Requested

We have been using Aspose.PDF for watermarking purposes in our application, and we have noticed that the watermarking process is taking longer than expected, especially for larger documents.
We have compared the performance with another library (active PDF Toolkit) and found a significant difference in processing time. We want to replace ActivePDP Toolkit with Aspose but this is blocker for us.

Here are some details regarding our setup and the observed performance:

  1. Aspose.PDF Version: 21.7.0

  2. Operating System: Windows Server 2012

  3. .NET Framework Version: 4.7.2

  4. PDF Document Size: 1MB to 20 MB

  5. Watermark Type: Text

  6. Watermark Content: COPY

  7. Expected Performance: Active pdf toolkit is talking around 1 second to apply water mark on all pages

  8. Code Snippet:
    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(inputPath);

                 Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText("COPY");
    
                 Aspose.Pdf.TextStamp textStamp2 = new Aspose.Pdf.TextStamp(formattedText);
    
                 textStamp2.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
    
                 textStamp2.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
    
                 textStamp2.RotateAngle = 45;
    
                 textStamp2.Justify = true;
    
                 textStamp2.Opacity = 0.5;
    
                 textStamp2.XIndent = 200;
    
                 textStamp2.YIndent = 200;
    
                 textStamp2.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
    
                 textStamp2.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Regular;
    
                 textStamp2.TextState.FontSize = 45.0F;
    
                 textStamp2.TextState.RenderingMode = TextRenderingMode.StrokeText;
    
                 textStamp2.TextState.StrokingColor = Aspose.Pdf.Color.LightGray;
    
                 foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
    
                 {
    
                     page.AddStamp(textStamp2);
    
                 }
    
                 //To Reduce the size
    
                 Aspose.Pdf.Optimization.OptimizationOptions optimizationOptions = new Aspose.Pdf.Optimization.OptimizationOptions
    
                 {
    
                     RemoveUnusedObjects = true,
    
                     RemoveUnusedStreams = true,
    
                     LinkDuplcateStreams = true,
    
                     AllowReusePageContent = true
    
                 };
    
                 pdfDocument.OptimizeResources(optimizationOptions);
    
                 pdfDocument.Save(outputPath);
    

Thanks,
Saurabh

@sranjan50

Can you please make sure to use 23.7 version of the API as it has many improvements and it is always recommended using the latest version. Furthermore, please try using the below Aspose.Pdf.Facades approach to add stamp on all PDF Pages and let us know in case you still notice any issues. You can also please share the sample document for our reference so that we can try to replicate the issue in our environment and address it accordingly.

Thanks for Suggestion. As you suggested, I used Facades for the water mark but Text State binding is not working (stamp.BindTextState(textState);). Performance wise it improved but I need the watermark in a particular, for that I have to use textstate. Please help.

               Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText("COPY");
                

                Aspose.Pdf.Text.TextState textState = new Aspose.Pdf.Text.TextState();
                textState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
                textState.FontStyle = Aspose.Pdf.Text.FontStyles.Regular;
                textState.FontSize = 45.0F;
                textState.RenderingMode = TextRenderingMode.StrokeText;
                textState.StrokingColor = Aspose.Pdf.Color.LightGray;

                Aspose.Pdf.Facades.PdfFileStamp fileStamp = new Aspose.Pdf.Facades.PdfFileStamp();

                fileStamp.BindPdf(inputPath);

                Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
                
                stamp.BindTextState(textState);
                stamp.BindLogo(formattedText);
                stamp.SetOrigin(100, 400);
                stamp.Rotation = 45.0F;
                stamp.IsBackground = true;
                
                fileStamp.AddStamp(stamp);
                fileStamp.Save(outputPath);
                fileStamp.Close();

@sranjan50

Can you please share the sample PDF document for our reference? Also, please share screenshot or sample expected output as well. We will test the scenario in our environment and address it accordingly.

PS: Please use Aspose.PDF for .NET 23.7 before sharing the requested information and files.

Hi Team,

We can’t directly upgrade the version. In production, we are using this version after numerous regression testing. Any change in version has a large impact on modules, such as the conversion of PDF, header, and page count functionalities, which are currently using Aspose in the production environment. Please provide the solution for the reported version (21.7.0).

Please find the attached files for your reference.

ActivePdf_Aspose_Screenshot_difference.png (89.3 KB)

Orignal_Document.pdf (45.7 KB)
Watermark_ActivePdf.pdf (51.5 KB)
Watermark_Aspose.pdf (47.9 KB)

Thanks,
Saurabh

@sranjan50

We are checking it and will get back to you shortly.

@sranjan50

Please note that we provide support on the basis of the latest available version of the API. Also, the issues in older versions are resolved in the higher and latest version of the API. Therefore, we tested using 23.7 version and replicated the issue in our environment. We have logged it as PDFNET-55173 in our issue tracking system for the sake of correction. We will look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Our management decided to upgrade the latest version but still the solution provided below to add text tamp on all pages is not working for the pages that contains images, where we have set the watermark coordinates. Through ActivePDF we are able to apply the watermark on all pages in one short without these issues. Please advice.

Add Text Stamp on All Pages in a PDF File

Is there any way to apply Aspose.Pdf.TextStamp on all pages in one go (without iterating all pages)?

@sranjan50

Yes, we have also noticed that the text state is not being applied on the watermark using the latest version as well. Therefore, we have logged the associated ticket in order to fix this.

We are afraid that it is not possible using DOM approach. However, it is possible using Aspose.Pdf.Facades model which has already been shared with you in this forum thread.