【问题标题】:Unable to add Signature Creator in itextSharp 5.5 /Prop_Build is not properly renderd无法在 itextSharp 5.5 中添加签名创建者 /Prop_Build 未正确呈现
【发布时间】:2019-11-06 05:44:59
【问题描述】:

无法在 adobe acrobat reader 中显示的签名属性中添加签名是使用(签名创建应用程序名称)创建的。在 itextSharp 中 如果我们设置 PdfSignature 类的 SignatureCreator 属性,它将在 pdf 中呈现为 Prop_Build 标记,但在 itextsharp。但是,它在 itext java 中运行良好。有什么办法可以纠正这个问题。

这是用于签署 pdf 的 c# 代码 sn-p。

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;

namespace ItextDemo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string pdfPath = "Test.pdf";
            string pfxPath = "Test.pfx";
            string pfxPassword = "TestPassword";
            int contentEstimated = 8192;
            X509Certificate2 x509Certificate2 = new X509Certificate2(pfxPath, pfxPassword);
            using (MemoryStream Ms = new MemoryStream())
            {
                PdfReader reader = new PdfReader(pdfPath);
                PdfStamper pdfStamper = PdfStamper.CreateSignature(reader, Ms, '\0', null, true);
                PdfSignatureAppearance pdfSignatureAppearance = pdfStamper.SignatureAppearance;

                pdfSignatureAppearance.Reason = "Testing";
                pdfSignatureAppearance.Location = "Test location";
                pdfSignatureAppearance.Contact = "Prashanth";
                pdfSignatureAppearance.SignDate = DateTime.Now.AddMinutes(1);
                pdfSignatureAppearance.Acro6Layers = false;
                pdfSignatureAppearance.CertificationLevel = PdfSignatureAppearance.NOT_CERTIFIED;
                pdfSignatureAppearance.Layer2Text = $"Name: {pdfSignatureAppearance.Contact}\nDate: {pdfSignatureAppearance.SignDate.ToString("dd-MMM-yyyy (HH:mm:ss)")}\nReason: {pdfSignatureAppearance.Reason}\nLocation: {pdfSignatureAppearance.Location}";
                Rectangle signatureRectangle = new Rectangle(25, 725, 150, 785);
                pdfSignatureAppearance.SetVisibleSignature(signatureRectangle, 1, "Pdf signature");


                Dictionary<PdfName, int> exc = new Dictionary<PdfName, int>();
                exc[PdfName.CONTENTS] = contentEstimated * 2 + 2;
                PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
                dic.Reason = pdfSignatureAppearance.Reason;
                dic.Location = pdfSignatureAppearance.Location;
                dic.Contact = pdfSignatureAppearance.Contact;
                dic.Date = new PdfDate(pdfSignatureAppearance.SignDate);

                //Test creator is not present in /Prop_Build tag in pdf

                dic.SignatureCreator = "Test creator"; 
                pdfSignatureAppearance.CryptoDictionary = dic;
                pdfSignatureAppearance.PreClose(exc);
                Stream presignedStream = pdfSignatureAppearance.GetRangeStream();
                byte[] presignedBytes = ReadFully(presignedStream);
                byte[] sigbytes = SignDocument(presignedBytes, x509Certificate2);

                byte[] paddedSig = new byte[contentEstimated];
                Array.Copy(sigbytes, 0, paddedSig, 0, sigbytes.Length);
                PdfDictionary dic2 = new PdfDictionary();
                dic2.Put(PdfName.CONTENTS, new PdfString(paddedSig).SetHexWriting(true));
                pdfSignatureAppearance.Close(dic2);
                File.WriteAllBytes($"{DateTime.Now.ToString("ddMMyyyyHHmmssfff")}Test.pdf", Ms.ToArray());
            }
        }
    }
}

这里是生成的pdf对象的示例

/Prop_Build<</App<</Name(Test creator)>>>>/ByteRange

因此,在下面给出的图像中,我们可以清楚地看到创建签名时使用的是不可用的。

示例图片

然而,在 java 中同样的代码可以工作并且

/Filter/Adobe.PPKLite/Type/Sig/Prop_Build<</App<</Name/Test Creator>>>>/ByteRange 

请找到正确呈现构建属性并显示测试创建者的签名属性图像。

示例图片

【问题讨论】:

  • 基本上你发现了一个错误。它存在于 .Net 的 iTextSharp 和 Java 的 iText 中。最初两个版本都在那里创建了一个 PDF 名称,并且都切换到使用 PDF 字符串。可能您的 iText for Java 只是老到不包括这个“修复”。可能这个问题是为了回应this question而引入的;该问题并未要求将 Name 更改为 String,而只是相应地对名称进行编码,但在解决此问题时可能会误解要采取的措施。
  • 更多细节可以在这里找到stackoverflow.com/questions/43983330/…
  • 我已在答案中添加了修复程序,任何人都可以验证修复程序是否正确

标签: java c# pdf itext adobe


【解决方案1】:

通过在iTextSharp.text.pdf.security.PdfSignatureAppDictionary 类中添加以下方法解决了这个问题

PdfSignatureAppDictionary getPdfSignatureAppProperty()
{
    PdfSignatureAppDictionary appPropDic = (PdfSignatureAppDictionary)GetAsDict(PdfName.APP);
    if (appPropDic == null)
    {
        appPropDic = new PdfSignatureAppDictionary();
        Put(PdfName.APP, appPropDic);
    }
    return appPropDic;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多