gpt4 book ai didi

java - 无法在 itextSharp 5.5/Prop_Build 中添加签名创建器未正确渲染

转载 作者:行者123 更新时间:2023-12-02 04:00:15 32 4
gpt4 key购买 nike

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

这是用于签署 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

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

示例图片

enter image description here

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

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

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

示例图片

enter image description here

最佳答案

通过在类 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;
}

关于java - 无法在 itextSharp 5.5/Prop_Build 中添加签名创建器未正确渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56736214/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com