Free Support Forum - aspose.com

Merge and Sign PDF files digitally in C# using Aspose.PDF for .NET - Signature becomes invalid

Hello,

We have been using Aspose.PDF to merge PDF files. We have found that multiple signature invalid for merged PDF files.

We have the following flow:

Merge two PDF/A-1B files
Sign result file with first signature
Sign result file with second signature

And after step 3, the first signature (applied on the 2td 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-1B 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: perhabs similar to this issue (PDFNET-47442)

Code example:

using System.Collections.Generic;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Facades;

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

        static void Main()
        {
            var files = new List<string>();
            files.Add("pdfa1.pdf");
            files.Add("pdfa2.pdf");

            var mergedFileName = "merged.pdf";
            var firstSign = "firstSign.pdf";
            var result = "result.pdf";
            Merge(files, mergedFileName);

            Sign(mergedFileName, firstSign);
            Sign(firstSign, result);
        }
        
        private static void Merge(
            List<string> filesPath,
            string outputFilePath)
        {
            var license = new License();
            license.SetLicense("Aspose.Total.lic");

            Document outputDocument = null;

            foreach (var filePath in filesPath)
            {
                if (outputDocument == null)
                {
                    outputDocument = new Document();
                }

                var inputDocument = new Document(filePath);

                outputDocument.Pages.Add(inputDocument.Pages);
            }

            outputDocument.Convert(
                new MemoryStream(),
                PdfFormat.PDF_A_1B,
                ConvertErrorAction.None /*GetErrorAction(parameters)*/);

            outputDocument.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);
            }
        }
    }
}

@uaprogrammer,

Can you please share source files along with generated result and environment details so that we may further investigate to help you out.