@@ -16,6 +16,7 @@ namespace PDFQFZ
1616{
1717 public partial class Form1 : Form
1818 {
19+ int fw , fh ;
1920 System . Reflection . Assembly CurrentDomain_AssemblyResolve ( object sender , ResolveEventArgs args ) //把DLL打包到EXE需要用到
2021 {
2122 string dllName = args . Name . Contains ( "," ) ? args . Name . Substring ( 0 , args . Name . IndexOf ( ',' ) ) : args . Name . Replace ( ".dll" , "" ) ;
@@ -120,36 +121,117 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath, string Mo
120121
121122 float height = psize . Height ;
122123
124+
123125 Bitmap [ ] nImage = subImages ( ModelPicName , numberOfPages ) ;
124126
125127 pdfStamper = new PdfStamper ( pdfReader , new FileStream ( outputfilepath , FileMode . Create ) ) ; //加完印章后的pdf
126128
127129 PdfContentByte waterMarkContent ;
128130
131+ int zyz = comboBox1 . SelectedIndex ; //如何加印章
132+ bool all = false ;
133+ int index = 0 ;
134+ if ( zyz == 3 )
135+ {
136+ all = true ;
137+ } else if ( zyz == 2 )
138+ {
139+ index = 1 ;
140+ } else if ( zyz == 1 )
141+ {
142+ index = numberOfPages ;
143+ }
144+
129145 //每一页加水印,也可以设置某一页加水印
130- for ( int i = 0 ; i < numberOfPages ; i ++ )
146+ for ( int i = 0 ; i < numberOfPages ; )
131147 {
132148 iTextSharp . text . Image image = iTextSharp . text . Image . GetInstance ( nImage [ i ] , ImageFormat . Bmp ) ;
133149
134150 //image.GrayFill = 20;//透明度,灰色填充
135151 //image.Rotation//旋转
152+ //image.ScaleToFit(140F, 320F);//设置图片的指定大小
136153 //image.RotationDegrees//旋转角度
137- float bili = 0.24f ; //印章图片由72dpi转300dpi
138- image . ScalePercent ( bili * 100 ) ; //设置图片比例
154+ int bili ;
155+ if ( ! int . TryParse ( textBili . Text , out bili ) )
156+ {
157+ bili = 24 ;
158+ }
159+ float result = bili / 100f ; //印章图片由72dpi转300dpi
160+ image . ScalePercent ( bili ) ; //设置图片比例
139161 image . Transparency = new int [ ] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ; //这里透明背景的图片会变黑色,所以设置黑色为透明
162+
163+ i ++ ; //pdf页面是从1开始的,所以这里要+1
164+ int rotation = pdfReader . GetPageRotation ( i ) ; //获取指定页面的旋转度
165+
140166 //水印的位置
141- if ( width < height ) {
142- image . SetAbsolutePosition ( width - image . Width * bili , ( height - image . Height * bili ) / 2 ) ;
167+ if ( rotation == 90 || rotation == 270 )
168+ {
169+ image . SetAbsolutePosition ( height - image . Width * result , ( width - image . Height * result ) / 2 ) ;
143170 }
144171 else
145172 {
146- image . SetAbsolutePosition ( height - image . Width * bili , ( width - image . Height * bili ) / 2 ) ;
173+ image . SetAbsolutePosition ( width - image . Width * result , ( height - image . Height * result ) / 2 ) ;
147174 }
175+ //switch (rotation)
176+ //{
177+ // case 90:
178+ // image.SetAbsolutePosition(height - image.Width * bili, (width - image.Height * bili) / 2);
179+ // break;
180+ // case 180:
181+ // image.SetAbsolutePosition(width - image.Width * bili, (height - image.Height * bili) / 2);
182+ // break;
183+ // case 270:
184+ // image.SetAbsolutePosition(height - image.Width * bili, (width - image.Height * bili) / 2);
185+ // break;
186+ // default:
187+ // image.SetAbsolutePosition(width - image.Width * bili, (height - image.Height * bili) / 2);
188+ // break;
189+ //}
148190
149- waterMarkContent = pdfStamper . GetOverContent ( i + 1 ) ;
191+ waterMarkContent = pdfStamper . GetOverContent ( i ) ;
150192
151193 waterMarkContent . AddImage ( image ) ;
152-
194+
195+ if ( all || i == index ) //如果是最后一页加入指定的图片
196+ {
197+ //创建一个图片对象
198+ iTextSharp . text . Image img = iTextSharp . text . Image . GetInstance ( ModelPicName ) ;
199+
200+ //按比例缩放
201+ img . ScalePercent ( bili ) ;
202+
203+ //把图片增加到内容页的指定位子 b width c height e bottom f left
204+ //waterMarkContent.AddImage(img,0,100f,100f,0,10f,20f);
205+
206+ float wbl = Convert . ToSingle ( textPx . Text ) ; //这里根据比例来定位
207+ float hbl = 1 - Convert . ToSingle ( textPy . Text ) ; //这里根据比例来定位
208+ if ( rotation == 90 || rotation == 270 )
209+ {
210+ img . SetAbsolutePosition ( ( height - img . Width * result ) * wbl , ( width - img . Height * result ) * hbl ) ;
211+ }
212+ else
213+ {
214+ img . SetAbsolutePosition ( ( width - img . Width * result ) * wbl , ( height - img . Height * result ) * hbl ) ;
215+ }
216+ waterMarkContent . AddImage ( img ) ;
217+
218+ ////开始增加文本
219+ //waterMarkContent.BeginText();
220+
221+ //BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
222+ ////设置字体 大小
223+ //waterMarkContent.SetFontAndSize(bf, 9);
224+
225+ ////指定添加文字的绝对位置
226+ //waterMarkContent.SetTextMatrix(imgLeft, 200);
227+ ////增加文本
228+ //waterMarkContent.ShowText("GW INDUSTRIAL LTD");
229+
230+ ////结束
231+ //waterMarkContent.EndText();
232+
233+ }
234+
153235
154236 }
155237 //strMsg = "success";
@@ -211,5 +293,35 @@ private void clear_Click(object sender, EventArgs e)
211293 textGZpath . Text = "" ;
212294 log . Text = "" ;
213295 }
296+
297+ private void Form1_Load ( object sender , EventArgs e )
298+ {
299+ comboBox1 . SelectedIndex = 0 ;
300+ fw = this . Width ;
301+ fh = this . Height ;
302+ }
303+
304+ private void pictureBox1_Click ( object sender , EventArgs e )
305+ {
306+ Point pt = pictureBox1 . PointToClient ( Control . MousePosition ) ;
307+ float picw = pictureBox1 . Width ;
308+ float pich = pictureBox1 . Height ;
309+ float px = pt . X / picw ;
310+ float py = pt . Y / pich ;
311+ textPx . Text = px . ToString ( "#0.0000" ) ;
312+ textPy . Text = py . ToString ( "#0.0000" ) ;
313+ }
314+
315+ private void comboBox1_SelectionChangeCommitted ( object sender , EventArgs e )
316+ {
317+ if ( comboBox1 . SelectedIndex != 0 )
318+ {
319+ this . Size = new Size ( fw + 200 , fh ) ;
320+ }
321+ else
322+ {
323+ this . Size = new Size ( fw , fh ) ;
324+ }
325+ }
214326 }
215327}
0 commit comments