Skip to content

Commit 9c722a1

Browse files
committed
支持印章跟文档合并为一个图层(会导致原数字签名丢失且文档不可编辑)
1 parent 1b19b4c commit 9c722a1

File tree

2 files changed

+120
-6
lines changed

2 files changed

+120
-6
lines changed

PDFQFZ/Form1.Designer.cs

Lines changed: 21 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PDFQFZ/Form1.cs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ private void pdfGz()
142142
string source = sourcepath + "\\" + fileInfo.Name;
143143
string output = outputpath + "\\" + fileInfo.Name;
144144
bool isSurrcess = PDFWatermark(source, output, cert);
145+
if (isSurrcess&&comboDJ.SelectedIndex==1)
146+
{
147+
PDFToiPDF(output, cert);
148+
}
145149
if (isSurrcess)
146150
{
147151
log.Text = log.Text + "成功!“" + fileInfo.Name + "”盖章完成!\r\n";
@@ -166,6 +170,10 @@ private void pdfGz()
166170
}
167171
string source = file;
168172
bool isSurrcess = PDFWatermark(source, output, cert);
173+
if (isSurrcess && comboDJ.SelectedIndex == 1)
174+
{
175+
PDFToiPDF(output, cert);
176+
}
169177
if (isSurrcess)
170178
{
171179
log.Text = log.Text + "成功!“" + filename + "”盖章完成!\r\n";
@@ -573,6 +581,96 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath, X509Certi
573581
}
574582
}
575583

584+
public static void ImageToPDF(Bitmap[] bitmaps,float bl, string trageFullName)
585+
{
586+
using (iTextSharp.text.Document document = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(0, 0), 0, 0, 0, 0))
587+
{
588+
iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(trageFullName, FileMode.Create, FileAccess.ReadWrite));
589+
document.Open();
590+
iTextSharp.text.Image image;
591+
for (int i = 0; i < bitmaps.Length; i++)
592+
{
593+
image = iTextSharp.text.Image.GetInstance(bitmaps[i], System.Drawing.Imaging.ImageFormat.Bmp);
594+
float Width = image.Width* bl, Height = image.Height* bl;
595+
image.ScaleToFit(Width, Height);
596+
document.SetPageSize(new iTextSharp.text.Rectangle(0, 0, Width, Height));
597+
document.NewPage();
598+
document.Add(image);
599+
}
600+
}
601+
}
602+
603+
604+
public static void SignaturePDF(string inputPath,string outPath, X509Certificate2 cert)
605+
{
606+
PdfReader pdfReader = null;
607+
PdfStamper pdfStamper = null;
608+
try
609+
{
610+
pdfReader = new PdfReader(inputPath);//选择需要印章的pdf
611+
pdfStamper = PdfStamper.CreateSignature(pdfReader, new FileStream(outPath, FileMode.Create), '\0', null, true);
612+
Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser();
613+
Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData) };
614+
615+
Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair pk = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(cert.GetRSAPrivateKey());
616+
IExternalSignature externalSignature = new PrivateKeySignature(pk.Private, DigestAlgorithms.SHA256);
617+
618+
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
619+
signatureAppearance.SignDate = DateTime.Now;
620+
//signatureAppearance.SignatureCreator = "";
621+
//signatureAppearance.Reason = "验证身份";
622+
//signatureAppearance.Location = "深圳";
623+
624+
//signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(0, 0, 0, 0), numberOfPages, null);
625+
626+
MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS);
627+
}
628+
catch (Exception ex)
629+
{
630+
MessageBox.Show(ex.ToString());
631+
}
632+
finally
633+
{
634+
635+
if (pdfStamper != null)
636+
pdfStamper.Close();
637+
638+
if (pdfReader != null)
639+
pdfReader.Close();
640+
}
641+
642+
}
643+
644+
public void PDFToiPDF(string pdfPath, X509Certificate2 cert)
645+
{
646+
PDFFile pdfFile = PDFFile.Open(pdfPath);
647+
Bitmap[] bitmaps = new Bitmap[pdfFile.PageCount];
648+
int dpi = 300;
649+
for (int i = 0; i < pdfFile.PageCount; i++)
650+
{
651+
Bitmap pageImage = pdfFile.GetPageImage(i, dpi);
652+
bitmaps[i] = pageImage;
653+
}
654+
pdfFile.Dispose();
655+
656+
float bl = 72f / dpi;//为了尽量保证转换的清晰度,这里需要把电脑的DPI缩放到DPF的DPI
657+
658+
string tmpPdf = pdfPath;
659+
660+
if (comboQmtype.SelectedIndex != 0)
661+
{
662+
tmpPdf = System.IO.Path.GetTempPath() + "PDFQFZ_tmp.pdf";
663+
}
664+
665+
ImageToPDF(bitmaps, bl, tmpPdf);
666+
667+
if (comboQmtype.SelectedIndex != 0)
668+
{
669+
SignaturePDF(tmpPdf,pdfPath, cert);
670+
}
671+
672+
}
673+
576674
//选择源文件
577675
private void SelectPath_Click(object sender, EventArgs e)
578676
{
@@ -649,6 +747,7 @@ private void Form1_Load(object sender, EventArgs e)
649747
comboYz.SelectedIndex = 0;
650748
comboBoxBL.SelectedIndex = 1;
651749
comboQmtype.SelectedIndex = 0;
750+
comboDJ.SelectedIndex = 0;
652751
fw = this.Width;
653752
fh = this.Height;
654753
dt.Columns.Add("Name", typeof(string));

0 commit comments

Comments
 (0)