Skip to content

Commit 72cd567

Browse files
committed
骑缝章新增支持单页/双页/随意页
1 parent 2f601df commit 72cd567

File tree

2 files changed

+96
-29
lines changed

2 files changed

+96
-29
lines changed

PDFQFZ/Form1.Designer.cs

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

PDFQFZ/Form1.cs

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Security.Cryptography;
99
using System.Security.Cryptography.X509Certificates;
1010
using O2S.Components.PDFRender4NET;
11+
using System.Collections.Generic;
12+
using iTextSharp.text;
1113

1214
namespace PDFQFZ
1315
{
@@ -19,7 +21,7 @@ public partial class Form1 : Form
1921
DataTable dt = new DataTable();//PDF列表
2022
DataTable dtPos = new DataTable();//PDF各文件印章位置表
2123
DataTable dtYz = new DataTable();//PDF列表
22-
string sourcePath = "",outputPath = "",imgPath = "",previewPath = null,signText = "", password="";
24+
string sourcePath = "",outputPath = "",imgPath = "",previewPath = "",signText = "", password="";
2325
int wjType = 1, qfzType = 0, yzType = 0, djType = 0, qmType = 0, sizeType = 1, size = 40, rotation = 0, opacity = 100, wz = 50, yzr = 10, maximg = 250;
2426
Bitmap imgYz = null;
2527
X509Certificate2 cert = null;//证书
@@ -274,7 +276,7 @@ private void pdfGz()
274276
}
275277
}
276278
//设置图片透明度
277-
private Bitmap SetImageOpacity(Image srcImage, int opacity)
279+
private Bitmap SetImageOpacity(System.Drawing.Image srcImage, int opacity)
278280
{
279281
opacity = opacity * 255/100;
280282
Bitmap pic = new Bitmap(srcImage);
@@ -299,7 +301,7 @@ private Bitmap SetImageOpacity(Image srcImage, int opacity)
299301
return pic;
300302
}
301303
//旋转图片
302-
public Bitmap RotateImg(Image b, int angle)
304+
public Bitmap RotateImg(System.Drawing.Image b, int angle)
303305
{
304306
angle = angle % 360;
305307
//弧度转换
@@ -326,7 +328,7 @@ public Bitmap RotateImg(Image b, int angle)
326328
//计算偏移量
327329
Point Offset = new Point((W - w) / 2, (H - h) / 2);
328330
//构造图像显示区域:让图像的中心与窗口的中心点一致
329-
Rectangle rect = new Rectangle(Offset.X, Offset.Y, w, h);
331+
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Offset.X, Offset.Y, w, h);
330332
Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
331333
g.TranslateTransform(center.X, center.Y);
332334
g.RotateTransform(angle);
@@ -377,6 +379,7 @@ private static Bitmap[] subImages(Bitmap img, int n)//图片分割
377379
}
378380
return nImage;
379381
}
382+
380383
//PDF盖章
381384
private bool PDFWatermark(string inputfilepath, string outputfilepath)
382385
{
@@ -410,19 +413,62 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
410413
}
411414

412415
int numberOfPages = pdfReader.NumberOfPages;//pdf页数
416+
int qfzPages = 0;
417+
List<int> qfzList = new List<int>();
418+
if (qfzType == 0)
419+
{
420+
for (int i = 1; i <= numberOfPages; i ++)
421+
{
422+
qfzList.Add(i);
423+
qfzPages++;
424+
}
425+
}
426+
else if(qfzType == 2)
427+
{
428+
for (int i = 1; i <= numberOfPages; i += 2)
429+
{
430+
qfzList.Add(i);
431+
qfzPages++;
432+
}
433+
}
434+
else if(qfzType == 3)
435+
{
436+
for (int i = 2; i <= numberOfPages; i += 2)
437+
{
438+
qfzList.Add(i);
439+
qfzPages++;
440+
}
441+
}
442+
else if(qfzType == 4)
443+
{
444+
// 遍历 DataTable 的所有行
445+
foreach (DataRow row in dtPos.Rows)
446+
{
447+
if (row["Path"].ToString()== inputfilepath)
448+
{
449+
// 获取当前行的某一列的值,假设这一列的列名为 "ColumnName"
450+
int columnValue = Convert.ToInt32(row["Page"]);
451+
452+
// 将获取的值添加到列表中
453+
qfzList.Add(columnValue);
454+
qfzPages++;
455+
}
456+
}
457+
qfzList.Sort();
458+
}
413459

414460
PdfContentByte waterMarkContent;
415461

416-
if(qfzType == 0&& numberOfPages > 1)
462+
if(qfzType != 1&& qfzPages > 1)
417463
{
418464
int max = 20;//骑缝章最大分割数
419-
int ss = numberOfPages / max + 1;
420-
int sy = numberOfPages - ss * max / 2;
465+
int ss = qfzPages / max + 1;
466+
int sy = qfzPages - ss * max / 2;
421467
int sys = sy / ss;
422468
int syy = sy % ss;
423469
int pp = max / 2 + sys;
424470
Bitmap[] nImage;
425-
int startpage = 0;
471+
int startIndex = 0;
426472
for (int i = 0; i < ss; i++)
427473
{
428474
int tmp = pp;
@@ -431,20 +477,20 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
431477
tmp++;
432478
}
433479
nImage = subImages(imgYz, tmp);
434-
for (int x = 1; x <= tmp; x++)
480+
for (int y = 0; y < tmp; y++)
435481
{
436-
int page = startpage + x;
482+
int page = qfzList[startIndex + y];
437483
waterMarkContent = pdfStamper.GetOverContent(page);//获取当前页内容
438484
int rotation = pdfReader.GetPageRotation(page);//获取当前页的旋转度
439485
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(page);//获取当前页尺寸
440-
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(nImage[x - 1], System.Drawing.Imaging.ImageFormat.Png);//获取骑缝章对应页的部分
441-
//image.Transparency = new int[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };//这里透明背景的图片会变黑色,所以设置黑色为透明
442-
//waterMarkContent.SaveState();//通过PdfGState调整图片整体的透明度
443-
//waterMarkContent.SetGState(state);
444-
//image.GrayFill = 20;//透明度,灰色填充
445-
//image.Rotation//旋转
446-
//image.ScaleToFit(140F, 320F);//设置图片的指定大小
447-
//image.RotationDegrees = rotation//旋转角度
486+
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(nImage[y], System.Drawing.Imaging.ImageFormat.Png);//获取骑缝章对应页的部分
487+
//image.Transparency = new int[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };//这里透明背景的图片会变黑色,所以设置黑色为透明
488+
//waterMarkContent.SaveState();//通过PdfGState调整图片整体的透明度
489+
//waterMarkContent.SetGState(state);
490+
//image.GrayFill = 20;//透明度,灰色填充
491+
//image.Rotation//旋转
492+
//image.ScaleToFit(140F, 320F);//设置图片的指定大小
493+
//image.RotationDegrees = rotation//旋转角度
448494
float imageW, imageH;
449495
image.ScalePercent(sfbl);//设置图片比例
450496
imageW = image.Width * sfbl / 100f;
@@ -462,11 +508,11 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
462508
xPos = psize.Width - imageW;
463509
yPos = (psize.Height - imageH) * (100 - wz) / 100;
464510
}
465-
image.SetAbsolutePosition(xPos,yPos);
511+
image.SetAbsolutePosition(xPos, yPos);
466512
waterMarkContent.AddImage(image);
467513
//waterMarkContent.RestoreState();
468514
}
469-
startpage += tmp;
515+
startIndex += tmp;
470516
}
471517
}
472518

@@ -934,7 +980,7 @@ private void comboType_SelectionChangeCommitted(object sender, EventArgs e)
934980
//上一页
935981
private void buttonUp_Click(object sender, EventArgs e)
936982
{
937-
if (previewPath != null&&imgStartPage > 1)
983+
if (previewPath != ""&&imgStartPage > 1)
938984
{
939985
imgStartPage--;
940986
PDFFile pdfFile = PDFFile.Open(previewPath);
@@ -947,7 +993,7 @@ private void buttonUp_Click(object sender, EventArgs e)
947993
DataRow[] arrRow = dtPos.Select("Path = '" + previewPath + "' and Page = " + imgStartPage);
948994
if (arrRow == null || arrRow.Length == 0)
949995
{
950-
if (comboYz.SelectedIndex == 4)
996+
if (comboYz.SelectedIndex == 4 || comboQfz.SelectedIndex == 4)
951997
{
952998
pictureBox2.Visible = false;
953999
}
@@ -972,7 +1018,7 @@ private void buttonUp_Click(object sender, EventArgs e)
9721018
//下一页
9731019
private void buttonNext_Click(object sender, EventArgs e)
9741020
{
975-
if (previewPath != null&&imgStartPage < imgPageCount)
1021+
if (previewPath != ""&&imgStartPage < imgPageCount)
9761022
{
9771023
imgStartPage++;
9781024
PDFFile pdfFile = PDFFile.Open(previewPath);
@@ -985,7 +1031,7 @@ private void buttonNext_Click(object sender, EventArgs e)
9851031
DataRow[] arrRow = dtPos.Select("Path = '" + previewPath + "' and Page = " + imgStartPage);
9861032
if (arrRow == null || arrRow.Length == 0)
9871033
{
988-
if (comboYz.SelectedIndex == 4)
1034+
if (comboYz.SelectedIndex == 4 || comboQfz.SelectedIndex == 4)
9891035
{
9901036
pictureBox2.Visible = false;
9911037
}
@@ -1179,6 +1225,10 @@ private void comboPDFlist_SelectionChangeCommitted(object sender, EventArgs e)
11791225
DataRow[] arrRow = dtPos.Select("Path = '" + previewPath + "' and Page = " + imgStartPage);
11801226
if (arrRow == null || arrRow.Length == 0)
11811227
{
1228+
if (comboYz.SelectedIndex == 4 || comboQfz.SelectedIndex == 4)
1229+
{
1230+
pictureBox2.Visible = false;
1231+
}
11821232
px = Convert.ToSingle(textPx.Text);//这里根据比例来定位
11831233
py = Convert.ToSingle(textPy.Text);//这里根据比例来定位
11841234
}
@@ -1201,7 +1251,7 @@ private void comboPDFlist_SelectionChangeCommitted(object sender, EventArgs e)
12011251
labelPage.Text = "1/1";
12021252
Bitmap bmp = new Bitmap(177, 250);
12031253
Graphics g = Graphics.FromImage(bmp);
1204-
g.FillRectangle(Brushes.White, new Rectangle(0, 0, 177, 250));
1254+
g.FillRectangle(Brushes.White, new System.Drawing.Rectangle(0, 0, 177, 250));
12051255
g.Dispose();
12061256
pictureBox1.Image = bmp;
12071257
}
@@ -1210,7 +1260,20 @@ private void comboPDFlist_SelectionChangeCommitted(object sender, EventArgs e)
12101260
//根据印章类型切换窗口大小
12111261
private void comboYz_SelectionChangeCommitted(object sender, EventArgs e)
12121262
{
1213-
if (comboYz.SelectedIndex != 0)
1263+
if (comboYz.SelectedIndex != 0 || comboQfz.SelectedIndex == 4)
1264+
{
1265+
this.Size = new Size(fw + 267, fh);
1266+
}
1267+
else
1268+
{
1269+
this.Size = new Size(fw, fh);
1270+
}
1271+
}
1272+
1273+
1274+
private void comboQfz_SelectionChangeCommitted(object sender, EventArgs e)
1275+
{
1276+
if (comboYz.SelectedIndex != 0 || comboQfz.SelectedIndex == 4)
12141277
{
12151278
this.Size = new Size(fw + 267, fh);
12161279
}

0 commit comments

Comments
 (0)