@@ -175,6 +175,10 @@ private void pdfGz()
175175 {
176176 imgYz = RotateImg ( imgYz , rotation ) ;
177177 }
178+ if ( opacity < 100 )
179+ {
180+ imgYz = SetImageOpacity ( imgYz , opacity ) ;
181+ }
178182
179183 //目录模式还是文件模式
180184 if ( wjType == 0 )
@@ -237,7 +241,32 @@ private void pdfGz()
237241 MessageBox . Show ( ex . ToString ( ) ) ;
238242 }
239243 }
244+ //设置图片透明度
245+ private Bitmap SetImageOpacity ( Image srcImage , int opacity )
246+ {
247+ opacity = opacity * 255 / 100 ;
248+ Bitmap pic = new Bitmap ( srcImage ) ;
249+ for ( int w = 0 ; w < pic . Width ; w ++ )
250+ {
251+ for ( int h = 0 ; h < pic . Height ; h ++ )
252+ {
253+ Color c = pic . GetPixel ( w , h ) ;
254+ Color newC ;
255+ if ( ! c . Equals ( Color . FromArgb ( 0 , 0 , 0 , 0 ) ) )
256+ {
257+ newC = Color . FromArgb ( opacity , c ) ;
258+ }
259+ else
260+ {
261+ newC = c ;
262+ }
263+ pic . SetPixel ( w , h , newC ) ;
264+ }
265+ }
240266
267+ return pic ;
268+ }
269+ //旋转图片
241270 public Bitmap RotateImg ( Image b , int angle )
242271 {
243272 angle = angle % 360 ;
@@ -306,8 +335,8 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
306335 float picbl = 1.003f ; //别问我这个数值怎么来的
307336 float picmm = 2.842f ; //别问我这个数值怎么来的
308337
309- PdfGState state = new PdfGState ( ) ;
310- state . FillOpacity = 0.01f * opacity ; //印章图片不透明度
338+ // PdfGState state = new PdfGState();
339+ // state.FillOpacity = 0.01f*opacity;//印章图片不透明度
311340
312341 //throw new NotImplementedException();
313342 PdfReader pdfReader = null ;
@@ -353,10 +382,10 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
353382 waterMarkContent = pdfStamper . GetOverContent ( page ) ; //获取当前页内容
354383 int rotation = pdfReader . GetPageRotation ( page ) ; //获取当前页的旋转度
355384 iTextSharp . text . Rectangle psize = pdfReader . GetPageSize ( page ) ; //获取当前页尺寸
356- iTextSharp . text . Image image = iTextSharp . text . Image . GetInstance ( nImage [ x - 1 ] , System . Drawing . Imaging . ImageFormat . Bmp ) ; //获取骑缝章对应页的部分
357- image . Transparency = new int [ ] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ; //这里透明背景的图片会变黑色,所以设置黑色为透明
358- waterMarkContent . SaveState ( ) ; //通过PdfGState调整图片整体的透明度
359- waterMarkContent . SetGState ( state ) ;
385+ iTextSharp . text . Image image = iTextSharp . text . Image . GetInstance ( nImage [ x - 1 ] , System . Drawing . Imaging . ImageFormat . Png ) ; //获取骑缝章对应页的部分
386+ // image.Transparency = new int[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };//这里透明背景的图片会变黑色,所以设置黑色为透明
387+ // waterMarkContent.SaveState();//通过PdfGState调整图片整体的透明度
388+ // waterMarkContent.SetGState(state);
360389 //image.GrayFill = 20;//透明度,灰色填充
361390 //image.Rotation//旋转
362391 //image.ScaleToFit(140F, 320F);//设置图片的指定大小
@@ -398,7 +427,7 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
398427 }
399428 image . SetAbsolutePosition ( xPos , yPos ) ;
400429 waterMarkContent . AddImage ( image ) ;
401- waterMarkContent . RestoreState ( ) ;
430+ // waterMarkContent.RestoreState();
402431 }
403432 startpage += tmp ;
404433 }
@@ -414,8 +443,8 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
414443
415444 if ( yzType != 0 )
416445 {
417- img = iTextSharp . text . Image . GetInstance ( imgYz , System . Drawing . Imaging . ImageFormat . Bmp ) ; //创建一个图片对象
418- img . Transparency = new int [ ] { 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 } ; //这里透明背景的图片会变黑色,所以设置黑色为透明
446+ img = iTextSharp . text . Image . GetInstance ( imgYz , System . Drawing . Imaging . ImageFormat . Png ) ; //创建一个图片对象
447+ // img.Transparency = new int[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };//这里透明背景的图片会变黑色,所以设置黑色为透明
419448 //img.RotationDegrees = rotation;
420449
421450 if ( sizeType == 0 )
@@ -466,8 +495,8 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
466495 int rotation = pdfReader . GetPageRotation ( i ) ; //获取指定页面的旋转度
467496 iTextSharp . text . Rectangle psize = pdfReader . GetPageSize ( i ) ; //获取当前页尺寸
468497
469- waterMarkContent . SaveState ( ) ; //通过PdfGState调整图片整体的透明度
470- waterMarkContent . SetGState ( state ) ;
498+ // waterMarkContent.SaveState();//通过PdfGState调整图片整体的透明度
499+ // waterMarkContent.SetGState(state);
471500
472501 float wbl = 0 ;
473502 float hbl = 1 ;
@@ -499,7 +528,7 @@ private bool PDFWatermark(string inputfilepath, string outputfilepath)
499528 }
500529 img . SetAbsolutePosition ( xPos , yPos ) ;
501530 waterMarkContent . AddImage ( img ) ;
502- waterMarkContent . RestoreState ( ) ;
531+ // waterMarkContent.RestoreState();
503532
504533 //普通印章跟数字印章已经完美重叠,所以就不需要通过以下方法特殊区分了
505534 //同时启用印章和数字签名的话用最后一个印章用数字签名代替
@@ -680,7 +709,7 @@ public void PDFToiPDF(string pdfPath)
680709 {
681710 Bitmap pageImage = pdfFile . GetPageImage ( i , dpi ) ;
682711 bitmaps [ i ] = pageImage ;
683- // pageImage.Save("D:\\tmp\\img\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
712+ pageImage . Save ( "D:\\ tmp\\ img\\ " + i + ".png" , System . Drawing . Imaging . ImageFormat . Png ) ;
684713 }
685714 pdfFile . Dispose ( ) ;
686715
0 commit comments