Skip to content

Commit 308ba01

Browse files
committed
新增支持骑缝章位置为上下左右
优化印章文件不存在时的提示
1 parent 72cd567 commit 308ba01

File tree

2 files changed

+99
-72
lines changed

2 files changed

+99
-72
lines changed

PDFQFZ/Form1.Designer.cs

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

PDFQFZ/Form1.cs

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public partial class Form1 : Form
2222
DataTable dtPos = new DataTable();//PDF各文件印章位置表
2323
DataTable dtYz = new DataTable();//PDF列表
2424
string sourcePath = "",outputPath = "",imgPath = "",previewPath = "",signText = "", password="";
25-
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;
25+
int wjType = 1, qfzType = 0, yzType = 0, djType = 0, qmType = 0, wzType = 3, size = 40, rotation = 0, opacity = 100, wz = 50, yzr = 10, maximg = 250;
2626
Bitmap imgYz = null;
2727
X509Certificate2 cert = null;//证书
2828

@@ -54,7 +54,7 @@ private void Form1_Load(object sender, EventArgs e)
5454
comboYz.SelectedIndex = yzType;
5555
comboDJ.SelectedIndex = djType;
5656
comboQmtype.SelectedIndex = qmType;
57-
comboBoxBL.SelectedIndex = sizeType;
57+
comboBoxWZ.SelectedIndex = wzType;
5858
fw = this.Width;
5959
fh = this.Height;
6060
dtYz.Columns.Add("Name", typeof(string));
@@ -99,7 +99,7 @@ private void button1_Click(object sender, EventArgs e)
9999
yzType = comboYz.SelectedIndex;
100100
djType = comboDJ.SelectedIndex;
101101
qmType = comboQmtype.SelectedIndex;
102-
sizeType = comboBoxBL.SelectedIndex;
102+
wzType = comboBoxWZ.SelectedIndex;
103103

104104
if (qfzType == 1&& yzType == 0 && qmType == 0)
105105
{
@@ -119,7 +119,11 @@ private void button1_Click(object sender, EventArgs e)
119119

120120
if (sourcePath != "" && outputPath != "" && (imgPath != "" || qfzType == 1 && yzType == 0))
121121
{
122-
if (!int.TryParse(textBili.Text, out size) || (sizeType == 0 && size > 100))
122+
if (!File.Exists(imgPath))
123+
{
124+
MessageBox.Show("印章文件读取失败,请重新导入印章。");
125+
}
126+
else if (!int.TryParse(textCC.Text, out size) || size > 100)
123127
{
124128
MessageBox.Show("印章尺寸设置错误,请输入正确的比例或尺寸。");
125129
}
@@ -301,7 +305,7 @@ private Bitmap SetImageOpacity(System.Drawing.Image srcImage, int opacity)
301305
return pic;
302306
}
303307
//旋转图片
304-
public Bitmap RotateImg(System.Drawing.Image b, int angle)
308+
public Bitmap RotateImg(System.Drawing.Image b, int angle,bool original = true)
305309
{
306310
angle = angle % 360;
307311
//弧度转换
@@ -314,10 +318,12 @@ public Bitmap RotateImg(System.Drawing.Image b, int angle)
314318
int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
315319
int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
316320

317-
318-
//为了尽可能的去除白边,减小印章旋转后尺寸的误差,这里保持原印章宽度,切掉部分角
319-
H = H * w / W;
320-
W = w;
321+
if (original)
322+
{
323+
//为了尽可能的去除白边,减小印章旋转后尺寸的误差,这里保持原印章宽度,切掉部分角
324+
H = H * w / W;
325+
W = w;
326+
}
321327

322328

323329
//目标位图
@@ -340,7 +346,7 @@ public Bitmap RotateImg(System.Drawing.Image b, int angle)
340346
g.Save();
341347
g.Dispose();
342348
//保存旋转后的图片
343-
//dsImage.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
349+
//dsImage.Save("D:\\tmp\\img\\tmp.png", System.Drawing.Imaging.ImageFormat.Png);
344350

345351
return dsImage;
346352
}
@@ -383,15 +389,7 @@ private static Bitmap[] subImages(Bitmap img, int n)//图片分割
383389
//PDF盖章
384390
private bool PDFWatermark(string inputfilepath, string outputfilepath)
385391
{
386-
float sfbl = 100f;
387-
if (sizeType == 0)
388-
{
389-
sfbl = size * 1.003f;//别问我为什么要乘这个
390-
}
391-
else
392-
{
393-
sfbl = 100f * size * 2.842f / imgYz.Height;
394-
}
392+
float sfbl = 100f * size * 2.842f / imgYz.Height;
395393

396394
//PdfGState state = new PdfGState();
397395
//state.FillOpacity = 0.01f*opacity;//印章图片不透明度
@@ -483,7 +481,27 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
483481
waterMarkContent = pdfStamper.GetOverContent(page);//获取当前页内容
484482
int rotation = pdfReader.GetPageRotation(page);//获取当前页的旋转度
485483
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(page);//获取当前页尺寸
486-
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(nImage[y], System.Drawing.Imaging.ImageFormat.Png);//获取骑缝章对应页的部分
484+
float pWidth, pHeight;
485+
if (rotation == 90 || rotation == 270)
486+
{
487+
pWidth = psize.Height;
488+
pHeight = psize.Width;
489+
}
490+
else
491+
{
492+
pWidth = psize.Width;
493+
pHeight = psize.Height;
494+
}
495+
Bitmap qfzImage = null;
496+
if(wzType == 3|| wzType == 2)
497+
{
498+
qfzImage = nImage[y];
499+
}
500+
else if (wzType == 1|| wzType == 0)
501+
{
502+
qfzImage = RotateImg(nImage[y], 90, false);
503+
}
504+
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(qfzImage, System.Drawing.Imaging.ImageFormat.Png);//获取骑缝章对应页的部分
487505
//image.Transparency = new int[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };//这里透明背景的图片会变黑色,所以设置黑色为透明
488506
//waterMarkContent.SaveState();//通过PdfGState调整图片整体的透明度
489507
//waterMarkContent.SetGState(state);
@@ -497,16 +515,26 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
497515
imageH = image.Height * sfbl / 100f;
498516

499517
//水印的位置
500-
float xPos, yPos;
501-
if (rotation == 90 || rotation == 270)
518+
float xPos=0, yPos=0;
519+
if (wzType == 3)
502520
{
503-
xPos = psize.Height - imageW;
504-
yPos = (psize.Width - imageH) * (100 - wz) / 100;
521+
xPos = pWidth - imageW;
522+
yPos = (pHeight - imageH) * (100 - wz) / 100;
505523
}
506-
else
524+
else if (wzType == 2)
525+
{
526+
xPos = 0;
527+
yPos = (pHeight - imageH) * (100 - wz) / 100;
528+
}
529+
else if (wzType == 1)
530+
{
531+
xPos = (pWidth - imageW) * wz / 100;
532+
yPos = 0;
533+
}
534+
else if (wzType == 0)
507535
{
508-
xPos = psize.Width - imageW;
509-
yPos = (psize.Height - imageH) * (100 - wz) / 100;
536+
xPos = (pWidth - imageW) * wz / 100;
537+
yPos = pHeight - imageH;
510538
}
511539
image.SetAbsolutePosition(xPos, yPos);
512540
waterMarkContent.AddImage(image);
@@ -943,20 +971,6 @@ private void pictureBox1_Click(object sender, EventArgs e)
943971
pictureBox2.Location = new Point(pt1.X + pt.X, pt1.Y + pt.Y);
944972

945973
}
946-
//印章比例类型
947-
private void comboBoxBL_SelectionChangeCommitted(object sender, EventArgs e)
948-
{
949-
if (comboBoxBL.SelectedIndex != 0)
950-
{
951-
label7.Text = "mm";
952-
textBili.Text = "40";
953-
}
954-
else
955-
{
956-
label7.Text = "%";
957-
textBili.Text = "100";
958-
}
959-
}
960974
//文件/目录模式切换
961975
private void comboType_SelectionChangeCommitted(object sender, EventArgs e)
962976
{

0 commit comments

Comments
 (0)