当前位置: 首页 > 知识库问答 >
问题:

无法在itextSharp 5.5中添加签名创建者/Prop_生成未正确渲染

邵弘伟
2023-03-14

无法添加签名是使用adobe acrobat阅读器中显示的签名属性创建的(签名创建应用程序名称)。在itextSharp中,如果我们设置PdfSignature类的SignatureCreator属性,它将在pdf中呈现为Prop_Build标记,但在itextSharp中不能正确呈现。

这是用于签署pdf的c#代码片段。

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 

请查找签名属性的图像,其中正确呈现了生成属性,并显示了测试创建者。

样本图像

共有1个答案

戚兴邦
2023-03-14

这是通过在类iTextS中添加以下方法harp.text.pdf.security.PdfSignatureAppDicpedia

PdfSignatureAppDictionary getPdfSignatureAppProperty()
{
    PdfSignatureAppDictionary appPropDic = (PdfSignatureAppDictionary)GetAsDict(PdfName.APP);
    if (appPropDic == null)
    {
        appPropDic = new PdfSignatureAppDictionary();
        Put(PdfName.APP, appPropDic);
    }
    return appPropDic;
}
 类似资料:
  • 我有一个用例,我使用lambda函数生成有符号的网址上传到S3桶,我还在生成有符号的网址时设置了元数据值,我的boto3版本是boto3==1.18.35。以前,当我生成有符号的网址上传到桶时,网址看起来像这样: https://bucket-name.s3.amazonaws.com/scanned-file-list/cf389880-09ff-4301-8fa7-b4054941685b/6

  • 我正在尝试生成签名APK。但由于生成时的lint错误而无法生成。我检查了我的lint报告,它在我的XML文件中显示错误。 林特在组装释放目标时发现致命错误。要继续,要么修复lint识别的问题,要么修改构建脚本,如下所示:...android{lintOptions{checkReleaseBuilds false//或者,如果您愿意,您可以继续检查版本构建中的错误,//但即使发现错误,也可以继续构

  • 我在Android Studio中生成签名的APK有一个问题。几天前它还很好,我不知道哪里出了问题。我在尝试fgenerate签名APK的任何项目中都得到了这个错误:

  • 生成PDF框 正如您所看到的,左侧的签名面板是无效的,但左侧的签名字段是存在的并且可以工作。 我用PDFTRON生成相同的PDF。这就是结果: 生成的PDF Tron 在这种情况下,左侧的签名面板正确显示签名字段的存在。 我想获得这第二个案例(正确),但我不明白为什么PDF框可以这样做。 多谢

  • 在控制台上获取以下第一行: 35026:1978749]CoreText注意:客户端请求的名称“.SFNS-Regular”,它将获得Times-Roman而不是预期的字体。所有系统UI字体访问都应通过适当的API,如CTFontCreateUIFontForLanguage()或[NSFont systemFontOfSize:]。2021 06月09日00:00:46.808 java[350