Watermark Applied to All Pages Slow in K8S

Our every-page watermark takes over 5 minutes to finish a 146 page document…we need to support 1000+ documents.

We can scale up our containers to address some of this, but are there code techniques to apply the stamp more efficiently?

//SET DRAFT WATERMARK AND SAVE THE DOC
                            using (var pdfDocument = new Aspose.Pdf.Document(sourceStream))
                            {
                                MakeNotEditable(pdfDocument);

                                using (var watermarkStream = await watermarkBlobClient.OpenReadAsync())
                                {
                                    var fileStamp = new PdfFileStamp();
                                    fileStamp.BindPdf(pdfDocument);
                                    // Create stamp
                                    var stamp = new Aspose.Pdf.Facades.Stamp();
                                    stamp.BindImage(watermarkStream);
                                    stamp.SetOrigin(-180, -50);
                                    stamp.Rotation = 45.0F;
                                    stamp.IsBackground = false;

                                    pdfDocument.Flatten();

                                    // Add stamp to PDF file
                                    fileStamp.AddStamp(stamp);
                                    using (var writeStream = await reportDocBlobClient.OpenWriteAsync(true))
                                    {
                                        fileStamp.Save(writeStream);
                                    }
                                    fileStamp.Close();
                                }
                            }


        private static void MakeNotEditable(Aspose.Pdf.Document pdfDocument)
        {
            //Forbid all privileges on the document
            var privilege = DocumentPrivilege.ForbidAll;
            privilege.AllowPrint = true;
            privilege.AllowCopy = true;
            var fileSecurity = new PdfFileSecurity();
            fileSecurity.BindPdf(pdfDocument);
            fileSecurity.SetPrivilege(privilege);
        }

@brooksclarkpwc,

The code you are doing seems good. The issue is the PDF documents are extensive.

I cannot think of changes that will speed up the process.

I am sorry I cannot give you a hint that will alleviate the process. Watermarking every page is an expensive process.

Thanks for the reply.

Concerning the privileges part of the code above…the table of contents bookmarking is no longer working. This corresponds to that code being introduced. Is there a setting that prevents changes to the PDF but allows the bookmarks to work (I may be calling them book marks incorrectly…simply put, we can no longer navigate to our pages from the TOC)

@brooksclarkpwc,

Let me review that specific issue.

So you to clarify, If I remove the stamping code and I only leave the privilege code, the ToC stop working?

Also can you provide a PDF with the TOC that get this issue?

@brooksclarkpwc,

I created a document from scratch and then made a ToC and bookmarks.

Then I set the privilege and still works:

private void Logic()
{
    Document doc = new Document();

    Document doc1 = new Document($"{PartialPath}_input_1.pdf");
    Document doc2 = new Document($"{PartialPath}_input_2.pdf");
    Document doc3 = new Document($"{PartialPath}_input_rotated.pdf");
    Document doc4 = new Document($"{PartialPath}_input_3.pdf");

    var docList = new List<Document> { doc1, doc2, doc3, doc4 };

    Page tocPage = doc.Pages.Add();
    tocPage.TocInfo = new TocInfo();

    int countDoc = 0;
    int countPage = 0;

    doc.Outlines.Clear();

    OutlineItemCollection rootOutline = new OutlineItemCollection(doc.Outlines);
    rootOutline.Title = "Table of Content";
    rootOutline.Destination = new GoToAction(tocPage);
    rootOutline.Action = new GoToAction(tocPage);
    doc.Outlines.Add(rootOutline);


    foreach (var docToMerge in docList)
    {
        countDoc++;
        foreach (var pageToAdd in docToMerge.Pages)
        {
            countPage++;

            var page = doc.Pages.Add(pageToAdd);

            page.AddStamp(new PageNumberStamp());

            string textTitle = $"Doc: {countDoc} - page: {countPage}";
            tocPage.Paragraphs.Add(new Heading(1)
            {
                TocPage = tocPage,
                DestinationPage = page,
                Text = textTitle,
                Margin = new MarginInfo(0, 20, 0, 20)
            });

            OutlineItemCollection pdfOutline = new OutlineItemCollection(doc.Outlines);
            pdfOutline.Title = textTitle;
            pdfOutline.Destination = new GoToAction(page);
            pdfOutline.Action = new GoToAction(page);
            doc.Outlines.Add(pdfOutline);
        }
    }

    var privilege = DocumentPrivilege.ForbidAll;
    privilege.AllowPrint = true;
    privilege.AllowCopy = true;
    var fileSecurity = new PdfFileSecurity();
    fileSecurity.BindPdf(doc);
    fileSecurity.SetPrivilege(privilege);

    doc.Save($"{PartialPath}_output.pdf");
}

The output:
TocStopWorkingAfterSettingPrivileges_output.pdf (113.3 KB)

If you run it as is, the TOC doesn’t work. And it has just been pointed out to me that none of our hyperlinks are working as well. Problem started with the introduction of the MakeNotEditable code.

It was the pdfDocument.Flatten()

@brooksclarkpwc,

Great to see that you figure that one out.

I will do a little more research to check if that is the expected behavior or I should raise an issue to the dev team.

I see there is an OptimizedMemoryStream… can that be used with the watermark stamping to improve response time?

@brooksclarkpwc,

I have not used it myself, but I give you this link to see the conde sample: documentation.

That documentation refers to code snippets that are not there:

image.png (30.5 KB)

This occurs throughout the Aspose .Net documentation.

@brooksclarkpwc,

you need to check your browser. The code snippet is there:

You have something blocking that section.