如何制作可下载的正确 pdf 文件并在 asp.net 中的 itextsharp 中验证它?
How to make downloadable correct pdf file and verify it in itextsharp in asp.net?
我使用 itextsharp 创建了一个 PDF 文件,它工作正常。我已经创建了一个 Activex 控件来在客户端使用 USB 令牌对 PDF 文件进行数字签名。客户端使用其 USB 令牌对 PDF 文件进行数字签名,PDF 文件存储在服务器上,其他授权人员可以下载它。
Activex 只需获取一个 PDF 文件并进行数字签名并临时存储 PDF 文件,然后 returns 使用 itextsharp 库的 PdfEncodings class 将 PDF 文件作为 base64 字符串。
为了使用 Activex 对象,我创建了两个 asp.net 隐藏字段,一个用于未签名的 PDF,另一个用于签名的 PDF。创建 PDF 文件后,我将其转换为 base64 字符串,然后将其分配给 asp.net 隐藏字段,然后调用 javascript 函数使用 Activex 控件进行签名,然后将值分配给另一个 asp.net 隐藏字段,然后获取该值并使其可下载但是,当下载 PDF 时,它说 PDF 已损坏。我已经检查了临时文件夹中的 PDF 文件是正确的。
在 activex 控件中对 pdf 进行数字签名的代码
PdfReader pdfReader = new PdfReader(PdfEncodings.ConvertToBytes(unsignedPDF, null));
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
FileStream signedPdf = new FileStream(fileName, FileMode.Create);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '[=11=]');
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);
if (sel.Count > 0)
{
X509Certificate2 cert = sel[0];
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-1");
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.Reason = "I declare this document is legal";
signatureAppearance.Location = "Delhi";
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), pdfReader.NumberOfPages, "Signature");
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
return PdfEncodings.ConvertToString(File.ReadAllBytes(fileName), null);
}
else
{
return "ERROR";
}
从浏览器下载PDF的代码:
byte[] pdfByte = PdfEncodings.ConvertToBytes(signed.Value, null);
Response.Clear();
MemoryStream ms = new MemoryStream(pdfByte);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();
Pdfencodings 并非用于此目的,我只是使用了 convert.tobase64string 和 convert.frombase64string 并且它起作用了。 :)
我使用 itextsharp 创建了一个 PDF 文件,它工作正常。我已经创建了一个 Activex 控件来在客户端使用 USB 令牌对 PDF 文件进行数字签名。客户端使用其 USB 令牌对 PDF 文件进行数字签名,PDF 文件存储在服务器上,其他授权人员可以下载它。 Activex 只需获取一个 PDF 文件并进行数字签名并临时存储 PDF 文件,然后 returns 使用 itextsharp 库的 PdfEncodings class 将 PDF 文件作为 base64 字符串。 为了使用 Activex 对象,我创建了两个 asp.net 隐藏字段,一个用于未签名的 PDF,另一个用于签名的 PDF。创建 PDF 文件后,我将其转换为 base64 字符串,然后将其分配给 asp.net 隐藏字段,然后调用 javascript 函数使用 Activex 控件进行签名,然后将值分配给另一个 asp.net 隐藏字段,然后获取该值并使其可下载但是,当下载 PDF 时,它说 PDF 已损坏。我已经检查了临时文件夹中的 PDF 文件是正确的。
在 activex 控件中对 pdf 进行数字签名的代码
PdfReader pdfReader = new PdfReader(PdfEncodings.ConvertToBytes(unsignedPDF, null));
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";
FileStream signedPdf = new FileStream(fileName, FileMode.Create);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '[=11=]');
X509Store store = new X509Store(StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);
if (sel.Count > 0)
{
X509Certificate2 cert = sel[0];
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-1");
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.Reason = "I declare this document is legal";
signatureAppearance.Location = "Delhi";
signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 150), pdfReader.NumberOfPages, "Signature");
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
return PdfEncodings.ConvertToString(File.ReadAllBytes(fileName), null);
}
else
{
return "ERROR";
}
从浏览器下载PDF的代码:
byte[] pdfByte = PdfEncodings.ConvertToBytes(signed.Value, null);
Response.Clear();
MemoryStream ms = new MemoryStream(pdfByte);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();
Pdfencodings 并非用于此目的,我只是使用了 convert.tobase64string 和 convert.frombase64string 并且它起作用了。 :)