1. 程式人生 > >iTextSharp插入指定背景色透明的圖片

iTextSharp插入指定背景色透明的圖片

iTextSharp用來處理PDF相當的強大,雖然作者專門寫了一本書介紹這個,但有些功能的資料還是太少了.

iTextSharp可以直接插入具有透明度的PNG圖片, 但對於像BMP格式那些沒有透明度的圖片,在插入的時候還是可以對指定背景色透明的.關鍵程式碼如下:

//建立Image例項
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap((IntPtr)axMyActiveX1.hBitmap), new BaseColor(255, 255, 255));
//設定想要透明的顏色,如下對白色透明
//如果是RGBImage,Image.Transparency陣列的格式為{red起始值, red終止值, green起始值, green終止值, blue起始值, blue終止值};範圍為0~255
image.Transparency = new int[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
image.SetAbsolutePosition(imgActualPosX, imgActualPosY);
image.ScaleAbsolute(imgActualWidth, imgActualHeight);
PdfContentByte contentByte = stamper.GetOverContent(_pdfDoc.CurrentPage);
contentByte.AddImage(image);