我可以使用下面的代码成功签名。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.X509;
using iTextSharp.text.pdf.security;
using Org.BouncyCastle.Security;
namespace pdf6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Girdi ve çıkış dosya yolları
string input_pdf_path = @"C:\Users\Fimlog\Desktop\12.pdf";
string output_pdf_path = @"C:\Users\Fimlog\Desktop\output.pdf";
string pfx_file_path = @"C:\Users\Fimlog\Downloads\1.pfx";
string pfx_password = "3202043";
// PFX dosyasını ve şifresini kullanarak sertifikayı yükle
X509Certificate2 cert = new X509Certificate2(pfx_file_path, pfx_password);
// Belirtilen PDF dosyasını imzala
SignPdfWithCertificate(input_pdf_path, output_pdf_path, cert);
}
private void SignPdfWithCertificate(string inputPdfPath, string outputPdfPath, X509Certificate2 cert)
{
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");
PdfReader pdfReader = new PdfReader(inputPdfPath);
FileStream signedPdf = new FileStream(outputPdfPath, FileMode.Create); // the output pdf file
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
// here set signatureAppearance at your will
signatureAppearance.Reason = "Because I can";
signatureAppearance.Location = "My location";
signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
//MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CADES);
MessageBox.Show("Done");
}
}
}
但我想添加2个签名。我尝试了数百次。但每次第一个签名都是无效的。因为它停留在 rev1。它不会在 rev2 上放置第二个签名。
// Get the first time signed pdf
PdfReader reader = new
PdfReader(LeerPDF.class.getResourceAsStream("cas[signed].pdf"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
reader = new PdfReader(reader);
// work with new revision each time
PdfStamper stp1 = new PdfStamper(reader, out, '\3', true);
PdfFormField sig = PdfFormField.createSignature(stp1.getWriter());
sig.setWidget(new Rectangle(300, 400, 400, 500), null);
sig.setFlags(PdfAnnotation.FLAGS_PRINT);
sig.put(PdfName.DA, new PdfString("/Helv 0 Tf 0 g"));
sig.setFieldName("Signature4");
sig.setPage(1);
// Attached the blank signature field to the existing document
stp1.addAnnotation(sig, 1);
stp1.addAnnotation(sig, 2);
stp1.close();
out.close();
FileOutputStream fout = new FileOutputStream
("c:\\cas[signed][signed].pdf");
reader = new PdfReader(out.toByteArray());
// Fill the new signature fields in the correct pdf revision
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\3', null,
true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(getPrivateKey(), chain, null,
PdfSignatureAppearance.SELF_SIGNED);
sap.setReason("RAZON");
sap.setLocation("UBICACION");
sap.setLayer2Text("Texto");
sap.setAcro6Layers(true);
// Set visible signature field
sap.setVisibleSignature("Signature4");
// Close PdfStamper and output
stp.close();
fout.close();
我在网上找到了一段代码。我无法将此适应我的代码。你能帮我吗?
我想尝试使用 c# 使用 pfx 进行两符号 pdf
您在评论中澄清:
我想要的是一个 pdf 的两个不同的(示例 Jhon 和 Mike)签名(不可见)。
要在同一个 PDF 中正确应用多个不同的签名,您必须确保后面的签名不会更改以前签名所签署的数据的任何位。
PDF 格式允许通过将更改附加到所谓的“增量更新”中来应用对文档的更改。使用 iText,您可以通过激活 追加模式来创建增量更新。 签署 PDF 时,您可以使用
PdfStamper.CreateSignature
方法的适当重载来激活 iText 追加模式。根据您的情况更换
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0');
由
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0', null, true);