在合并的pdf文档上添加数字签名后,最终的pdf不正确

时间:2017-09-01 14:21:05

标签: c# pdf itext7

以下是这个过程。

  1. 我们有两个pdf文件。第一页是pdf,有10页,第二页是1页。
  2. 我们使用合并工具合并这两个文件,并创建一个11页的文件。
  3. 我们使用iText 7库在此文件上添加数字签名。
  4. 输出pdf文件超过11页。
  5. 当我们在pdf阅读器中打开此文件时,有16页,这些额外的5页有一些不需要的图像。
  6. 最终输出中还缺少前两个原始页面,这些页面经过数字签名。
  7. 以下是输出pdf文件的图像。 enter image description here
  8. c#源代码

     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 个答案:

没有答案