Skip to content

Commit 64b28a8

Browse files
committed
新增支持加密PDF
默认印章白色背景转透明 优化旋转不切边可能导致尺寸差异的问题
1 parent 4fed859 commit 64b28a8

File tree

2 files changed

+113
-27
lines changed

2 files changed

+113
-27
lines changed

PDFQFZ/Form1.Designer.cs

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

PDFQFZ/Form1.cs

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using O2S.Components.PDFRender4NET;
1111
using System.Collections.Generic;
1212
using iTextSharp.text;
13+
using iTextSharp.text.exceptions;
14+
using Org.BouncyCastle.Crypto.Generators;
1315

1416
namespace PDFQFZ
1517
{
@@ -21,7 +23,7 @@ public partial class Form1 : Form
2123
DataTable dt = new DataTable();//PDF列表
2224
DataTable dtPos = new DataTable();//PDF各文件印章位置表
2325
DataTable dtYz = new DataTable();//PDF列表
24-
string sourcePath = "",outputPath = "",imgPath = "",previewPath = "",signText = "", password="";
26+
string sourcePath = "",outputPath = "",imgPath = "",previewPath = "",signText = "", password="",pdfpassword="";
2527
int wjType = 1, qfzType = 0, yzType = 0, djType = 0, qmType = 0, wzType = 3, qbflag = 0, size = 40, rotation = 0, opacity = 100, wz = 50, yzr = 10, maximg = 250, maxfgs = 20;
2628
Bitmap imgYz = null;
2729
X509Certificate2 cert = null;//证书
@@ -119,6 +121,7 @@ private void button1_Click(object sender, EventArgs e)
119121
imgPath = comboBoxYz.SelectedValue.ToString();
120122
signText = textname.Text;
121123
password = textpass.Text;
124+
pdfpassword = textpdfpass.Text;
122125

123126
if (sourcePath != "" && outputPath != "" && (imgPath != "" || qfzType == 1 && yzType == 0))
124127
{
@@ -209,15 +212,20 @@ private void pdfGz()
209212
}
210213

211214
imgYz = new Bitmap(imgPath);
212-
215+
//默认把印章的白色部分重置为透明
216+
imgYz = SetWhiteToTransparent(imgYz);
217+
//再判断是否需要调整整体的透明度
218+
if (opacity < 100)
219+
{
220+
imgYz = SetImageOpacity(imgYz, opacity);
221+
}
222+
//再看是否需要旋转印章
213223
if (rotation != 0)
214224
{
215225
bool qb = qbflag==0?true: false;
226+
int iw = imgYz.Width;
216227
imgYz = RotateImg(imgYz, rotation, qb);
217-
}
218-
if (opacity < 100)
219-
{
220-
imgYz = SetImageOpacity(imgYz, opacity);
228+
xzbl = 1f*imgYz.Width/iw;
221229
}
222230

223231
//目录模式还是文件模式
@@ -266,9 +274,17 @@ private void pdfGz()
266274
}
267275
string source = file;
268276
bool isSurrcess = PDFWatermark(source, output);
269-
if (isSurrcess && djType == 1)
277+
if (isSurrcess)
270278
{
271-
PDFToiPDF(output);
279+
if(djType == 1)
280+
{
281+
PDFToiPDF(output);
282+
}
283+
if (pdfpassword != "")
284+
{
285+
string jmoutput = outputPath + "\\" + System.IO.Path.GetFileNameWithoutExtension(file) + "_QFZ_加密.pdf";
286+
EncryptPDF(output, jmoutput, pdfpassword);
287+
}
272288
}
273289
if (isSurrcess)
274290
{
@@ -287,8 +303,30 @@ private void pdfGz()
287303
MessageBox.Show(ex.ToString());
288304
}
289305
}
306+
//设置JPG图片白色为透明
307+
private Bitmap SetWhiteToTransparent(System.Drawing.Bitmap img)
308+
{
309+
Bitmap bitmap = new Bitmap(img);
310+
// 遍历图片的每个像素
311+
for (int x = 0; x < bitmap.Width; x++)
312+
{
313+
for (int y = 0; y < bitmap.Height; y++)
314+
{
315+
Color pixelColor = bitmap.GetPixel(x, y);
316+
317+
// 判断像素颜色是否为白色
318+
if (pixelColor.R > 230 && pixelColor.G > 230 && pixelColor.B > 230)
319+
{
320+
// 将白色像素设置为透明
321+
bitmap.SetPixel(x, y, Color.Transparent);
322+
}
323+
}
324+
}
325+
326+
return bitmap;
327+
}
290328
//设置图片透明度
291-
private Bitmap SetImageOpacity(System.Drawing.Image srcImage, int opacity)
329+
private Bitmap SetImageOpacity(System.Drawing.Bitmap srcImage, int opacity)
292330
{
293331
opacity = opacity * 255/100;
294332
Bitmap pic = new Bitmap(srcImage);
@@ -313,28 +351,26 @@ private Bitmap SetImageOpacity(System.Drawing.Image srcImage, int opacity)
313351
return pic;
314352
}
315353
//旋转图片
316-
public Bitmap RotateImg(System.Drawing.Image b, int angle,bool original = true)
354+
public Bitmap RotateImg(System.Drawing.Bitmap bitmap, int angle,bool original = true)
317355
{
318356
angle = angle % 360;
319357
//弧度转换
320358
double radian = angle * Math.PI / 180.0;
321359
double cos = Math.Cos(radian);
322360
double sin = Math.Sin(radian);
323361
//原图的宽和高
324-
int w = b.Width;
325-
int h = b.Height;
362+
int w = bitmap.Width;
363+
int h = bitmap.Height;
326364
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
327365
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
328366

367+
//为了尽可能的去除白边,减小印章旋转后尺寸的误差,这里保持原印章宽度,切掉部分角
329368
if (original)
330369
{
331-
//为了尽可能的去除白边,减小印章旋转后尺寸的误差,这里保持原印章宽度,切掉部分角
332370
H = H * w / W;
333371
W = w;
334372
}
335373

336-
xzbl = 1f * W / w;
337-
338374
//目标位图
339375
Bitmap dsImage = new Bitmap(W, H);
340376
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dsImage);
@@ -349,7 +385,7 @@ public Bitmap RotateImg(System.Drawing.Image b, int angle,bool original = true)
349385
g.RotateTransform(angle);
350386
//恢复图像在水平和垂直方向的平移
351387
g.TranslateTransform(-center.X, -center.Y);
352-
g.DrawImage(b, rect);
388+
g.DrawImage(bitmap, rect);
353389
//重至绘图的所有变换
354390
g.ResetTransform();
355391
g.Save();
@@ -408,7 +444,7 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
408444
PdfStamper pdfStamper = null;
409445
try
410446
{
411-
pdfReader = new PdfReader(inputfilepath);//选择需要印章的pdf
447+
pdfReader = new PdfReader(inputfilepath, new System.Text.UTF8Encoding().GetBytes(pdfpassword));//选择需要印章的pdf
412448
if (qmType != 0)
413449
{
414450
//最后的true表示保留原签名
@@ -727,6 +763,11 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
727763
}
728764
return true;
729765
}
766+
catch (BadPasswordException)
767+
{
768+
MessageBox.Show("PDF密码错误.");
769+
return false;
770+
}
730771
catch (Exception ex)
731772
{
732773
MessageBox.Show(ex.ToString());
@@ -802,6 +843,28 @@ public static void SignaturePDF(string inputPath,string outPath, X509Certificate
802843
}
803844

804845
}
846+
//加密PDF文件
847+
public static void EncryptPDF(string inputFilePath, string outputFilePath, string password)
848+
{
849+
try
850+
{
851+
using (FileStream fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
852+
{
853+
using (PdfReader reader = new PdfReader(inputFilePath))
854+
{
855+
using (Document document = new Document())
856+
{
857+
PdfEncryptor.Encrypt(reader, fs, true, password, password, PdfWriter.ALLOW_PRINTING);
858+
}
859+
}
860+
}
861+
File.Delete(inputFilePath);
862+
}
863+
catch (Exception ex)
864+
{
865+
MessageBox.Show("添加密码保护时发生错误:" + ex.Message);
866+
}
867+
}
805868

806869
private void SaveSources(object sender, EventArgs e)
807870
{

0 commit comments

Comments
 (0)