Final pdf is incorrect after adding digital signature on a merged pdf document

295 Views Asked by At

Following is the process.

  1. We have two pdf files. first pdf with 10 pages and second one with 1 page.
  2. We merge these two files using a merging tool and create one file of 11 pages.
  3. We add the digital signature on this file using iText 7 library.
  4. The output pdf file has more than 11 pages.
  5. When we open this file in pdf reader, there are sixteen pages and these additional five pages have some unwanted images.
  6. Also first two original pages are missing from the final output which is digitally signed.
  7. Following is the image of the output pdf file. enter image description here

c# Source Code

 private static string certificate = @"E:\CertificatePath";//Certificate
        private static string password = XXXXXXXX!";
        private static string srcFile = @"xxxx\Merged.pdf";
        private static string dstFile = @"xxxx\Output.pdf";// output pdf path
        static void Main(string[] args)
        {
            try
            {
                try
                {
                    LicenseKey.LoadLicenseFil(@"xxx\x.xml");//key                
                    var licenseInfo = LicenseKey.GetLicenseeInfo();
                    bool isTrial = LicenseKey.IsTrial();
                    Console.WriteLine("License loaded successfully...");
                    Console.WriteLine();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception occurred while loading license - " + ex.Message);
                }

                DateTime start = DateTime.Now;
                Console.WriteLine("Started the process for adding digital signature on pdf");
                Console.WriteLine();
                File.Delete(dstFile);
                AddDigitalSignature(certificate, password, srcFile, "", dstFile);
                Console.WriteLine(string.Format("Process completed in {0} ms", (DateTime.Now - start).TotalMilliseconds));
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred while adding digital certificate on file...");
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

public static void AddDigitalSignature(string _certficate, string _password, string _srcPDF, string _srcPDFPassword, string _dstPDF)
        {
            try
            {        
                using (PdfReader reader = new PdfReader(_srcPDF))
                {
                    var signer = new PdfSigner(reader, new FileStream(_dstPDF, FileMode.OpenOrCreate, FileAccess.Write), false);
                    Rectangle rect = new Rectangle(0, 0, 0, 0);
                    DigitalCert digitalCert = new DigitalCert(certificate, password);
                    // Creating the appearance
                    PdfSignatureAppearance appearance = signer.GetSignatureAppearance()
                        .SetSignatureCreator("XXXX")
                        .SetReason("XXXXX")
                        .SetLocation("XXXXX")
                        .SetReuseAppearance(false)
                        .SetPageRect(rect)
                        .SetPageNumber(1);
                    signer.SetSignDate(DateTime.Now);
                    signer.SetFieldName("XXXXX");
                    // Creating the signature
                    IExternalSignature pks = new PrivateKeySignature(digitalCert.pk, digitalCert.DigestAlgorithm);
                    signer.SignDetached(pks, digitalCert.chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
                    reader.Close();
                }
            }
            catch (Exception ex)
            {`enter code here`
                throw ex; 
            }
        }
0

There are 0 best solutions below