使用itextsharp的数字签名中的边框 [英] Border in Digital Signature using itextsharp
问题描述
我正在使用Itextsharp将pdf数字签名添加到pdf中,该数字签名已完美添加.但是现在我需要在盒子中添加数字签名.
I am adding a digital signature to a pdf using Itextsharp, which is being added perfectly. But now I need to add that digital signature in a box.
我尝试了以下操作:- 1)在矩形中创建一个textform字段,并尝试用数字签名替换form字段,但未发生,因为它无法将其识别为签名字段. (IDK的实际含义)
I tried the below things:- 1) created a textform field in a rectangle and tried to replace the form field with a digital signature, but did not happen as it does not recognise it as a signature field. (IDK what it actually means)
2)当我在Adobe中打开pdf时,在签名悬停时可以看到一个矩形框闪烁. 无需验证.
2) When I open the pdf in adobe I can see a rectangular box flicker on hover of the signature. No verification needed.
但是我需要在框中签名.
But i need the signature in the box.
我的代码添加了 @mkl的答案中的更改:
My code after adding the changes from @mkl's answer:
PdfReader reader1 = new PdfReader(this.inputPDF);
PdfStamper st = PdfStamper.CreateSignature(reader1, new FileStream(this.outputPDF, FileMode.Create, FileAccess.ReadWrite), '\0');
AcroFields pdfFormFields = st.AcroFields;
st.SetEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowPrinting);
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.SignDate = Convert.ToDateTime(DateTime.Now.ToLocalTime());
sap.Layer2Font = new Font(Font.HELVETICA, 12, Font.BOLD);
sap.Layer2Text = "Digitally Signed \nVICE PRESIDENT- \nDate:" + ; //+ "\nLocation: BENGALURU";
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.SELF_SIGNED);
sap.Contact = SigContact;
sap.Location = SigLocation;
Rectangle obj = new iTextSharp.text.Rectangle(75, 90, 600, 300);
sap.SetVisibleSignature(obj, 1, "sig");
sap.GetAppearance();
PdfTemplate layer20 = sap.GetLayer(2);
Rectangle rectangle = sap.Rect;
layer20.SetRGBColorStroke(0, 0, 0);
layer20.SetLineWidth(5);
layer20.Rectangle(00, 80, 250, 100);
layer20.Stroke();
st.close();
当前使用5.5.13.
Currently using 5.5.13.
此外,在使用SSL证书将其托管在服务器上时,我得到了错误的DER长度超过4个字节.
Moreover, I get an error DER length is more than 4 bytes while hosting it on server with SSL certificate.
相同的堆栈跟踪.
`System.IO.IOException was caught
HResult=-2146232800
Message=DER length more than 4 bytes: 32
Source=itextsharp
StackTrace:
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadLength(Stream s, Int32 limit)
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildEncodableVector()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildDerEncodableVector(DefiniteLengthInputStream dIn)
at Org.BouncyCastle.Asn1.Asn1InputStream.CreateDerSequence(DefiniteLengthInputStream dIn)
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag, Int32 tagNo, Int32 length)
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildEncodableVector()
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildDerEncodableVector(DefiniteLengthInputStream dIn)
at Org.BouncyCastle.Asn1.Asn1InputStream.CreateDerSequence(DefiniteLengthInputStream dIn)
at Org.BouncyCastle.Asn1.Asn1InputStream.BuildObject(Int32 tag, Int32 tagNo, Int32 length)
at Org.BouncyCastle.Asn1.Asn1InputStream.ReadObject()
at Org.BouncyCastle.Asn1.Asn1Object.FromStream(Stream inStr)
at Org.BouncyCastle.Pkcs.Pkcs12Store.Load(Stream input, Char[] password)
at Org.BouncyCastle.Pkcs.Pkcs12Store..ctor(Stream input, Char[] password)
at Letters.Models.PDFSigner.Sign(String SigReason, String SigContact, String SigLocation, Boolean visible, String strType, String path, String password) in :line 93`
再次共享已编辑的代码,请帮助我查找其中是否有错误.
Edited code sharing again, Please help me find if there is some error in it.
Stream path1 = new FileStream(path, FileMode.Open, FileAccess.Read);
Pkcs12Store pk12 = new Pkcs12Store(path1, password.ToCharArray());
path1.Dispose();
//then Iterate throught certificate entries to find the private key entry
string alias = null;
foreach (string tAlias in pk12.Aliases)
{
if (pk12.IsKeyEntry(tAlias))
{
alias = tAlias;
break;
}
}
var pk = pk12.GetKey(alias).Key;
PdfReader reader = new PdfReader(this.inputPDF);
FileStream os = new FileStream(this.outputPDF, FileMode.Create);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Layer2Font = new Font(Font.FontFamily.HELVETICA, 12, Font.BOLD);`
推荐答案
使用iText 5.5.x对PDF签名时,通常会执行类似的操作
When you sign a PDF using iText 5.5.x, you usually do something like
// Creating the reader and the stamper
PdfReader reader = new PdfReader(SRC);
FileStream os = new FileStream(DEST, FileMode.Create);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Reason = "Test customized appearance";
appearance.Location = "Singularity";
appearance.SetVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
IExternalSignature pks = new PrivateKeySignature(parameters, DigestAlgorithms.SHA256);
MakeSignature.SignDetached(appearance, pks, chain, null, null, null, 0, CryptoStandard.CADES);
您可以在appearance.SetVisibleSignature
调用后通过处理图层模板来自由自定义视觉外观,例如像这样:
You can freely customize the visual appearance by working on the layer templates after the appearance.SetVisibleSignature
call, e.g. like this:
// trigger creation of default layers contents
appearance.GetAppearance();
// Customize the layer contents
PdfTemplate layer2 = appearance.GetLayer(2);
Rectangle rect = appearance.Rect;
layer2.SetRGBColorStroke(255, 0, 0);
layer2.SetLineWidth(2);
layer2.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);
layer2.Stroke();
沿着签名外观边框绘制一条红线
to draw a red line along the signature appearance border
这篇关于使用itextsharp的数字签名中的边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!