Free Support Forum - aspose.com

Sign PDF and Add Watermark using Aspose.PDF for .NET - Second signature causes first one invalid if watermark was added

Hello,

We have been using Aspose.PDF to convert DOC to PDF/A, add watermarks, and sign document.
We have found that the result PDF/A file is invalid for multiple signing if it contein watermark .

We have the following flow:

Convert DocX to PDF/A-1B
Add watermark to the PDF/A-1B
Sign PDF/A-1A with first signature
Sign PDF/A-1A with second signature

And after step 4, the first signature (applied on the 3td step) become invalid.

NOTE 1: we use external tool to sign PDF/A file, however when we try to sign with Aspose, we still have this problem.
NOTE 2: It seems that problem not in signature, but in the created PDF/A-1A file. If converted file is opened and re-saved with Adobe - signing works correct.
NOTE 3: we are using Aspose.Pdf v19.11 and Aspose.Words v19.11
NOTE 4: perhaps this issue related to 206359 or 206359

Code example:

using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Facades;
using Aspose.Pdf.Text;
using Aspose.Words.Saving;
using PdfSaveOptions = Aspose.Pdf.PdfSaveOptions;

namespace TestAsposeAddWatermark
{
    class Program
    {
        private static string PfxFileName = @"D:\pdfaSigningIssue\cert.pfx";
        private static string CertificatePassword = "1q2w3e4r5t";

        static void Main(string[] args)
        {
            var input = @"D:\pdfaSigningIssue\docx.docx";
            var converted = @"D:\pdfaSigningIssue\converted.pdf";
            var withWatermark = @"D:\pdfaSigningIssue\withWatermark.pdf";
            var firstSign = @"D:\pdfaSigningIssue\firstSign.pdf";
            var result = @"D:\pdfaSigningIssue\result.pdf";

            ConvertToPdfA1b(input, converted);
            AddWatermarkAndConvert(converted, withWatermark);

            Sign(withWatermark, firstSign);
            Sign(firstSign, result);
        }

        private static void ConvertToPdfA1b(string inputFilePath, string outputFilePath)
        {
            new Aspose.Words.License()
                .SetLicense("Aspose.Total.lic");

            var inputDocument = new Aspose.Words.Document(inputFilePath);
            
            var pdfSaveOptions = new Aspose.Words.Saving.PdfSaveOptions();
            pdfSaveOptions.OutlineOptions.HeadingsOutlineLevels = 9;
            pdfSaveOptions.DmlRenderingMode = DmlRenderingMode.DrawingML;
            pdfSaveOptions.Compliance = PdfCompliance.PdfA1b;

            inputDocument.Save(outputFilePath, pdfSaveOptions);
        }

        private static void AddWatermarkAndConvert(string inputFilePath, string outputFilePath)
        {
            var license = new Aspose.Pdf.License();
            license.SetLicense("Aspose.Total.lic");
            
            using (Document pdfDocument = new Document(inputFilePath))
            {
                TextStamp textStamp = new TextStamp("Test stamp", new TextState
                {
                    FontStyle = FontStyles.Bold,
                    ForegroundColor = Color.Red
                });
            
                foreach (Page page in pdfDocument.Pages)
                {
                    page.AddStamp(textStamp);
                }

                // we had to convert file to PDF/A-1b becouse
                // aspose saves loaded PDF/A-1b as PDF. 
                pdfDocument.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.None);

                pdfDocument.Save(outputFilePath);
            }
        }

        private static void Sign(string inputFilePath, string outputFilePath)
        {
            var license = new License();
            license.SetLicense("Aspose.Total.lic");

            using (PdfFileSignature pdfSign = new PdfFileSignature())
            {
                pdfSign.BindPdf(inputFilePath);
                pdfSign.SetCertificate(PfxFileName, CertificatePassword);
                pdfSign.Sign(1, "Signature Reason", "Contact", "Location", false, System.Drawing.Rectangle.Empty);
                pdfSign.Save(outputFilePath);
            }
        }
    }
}

Files:
pdfaSigningIssue.zip (443.4 KB)

@uaprogrammer,

Thanks for contacting support.

I have observed your issue and like to inform that I have created ticket with ID PDFNET-47669 in our issue tracking system to investigate and resolve this issue as soon as possible. We will share good news with you soon.