2009-08-17 [.Net C#]利用iTextSharp在PDF上新增浮水印(WaterMark) [二] 11295 0 .Net C# 摘要:[.Net C#]利用iTextSharp在PDF上新增浮水印(WaterMark) [二] 原本以為使用插入圖片就可以替代在iTextSharp3.8之後沒有的浮水印但PDF內容原本就是圖檔的竟令此解決方法破功所以只好拜請google大神找出iTextSharp3.8的板本程式碼如下:public void Watermark(string inputPath, string outputPath, string watermarkPath) { try { PdfReader reader = new PdfReader(inputPath); iTextSharp.text.Document document = new iTextSharp.text.Document(); PdfWriter writer = PdfWriter.GetInstance(document,new FileStream(outputPath, FileMode.Create)); Watermark watermark = new Watermark(iTextSharp.text.Image.GetInstance(watermarkPath), 255, 785); document.Add(watermark); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; int iPageNum = reader.NumberOfPages; for(int j=1; j<= iPageNum;j++) { document.NewPage(); newPage = writer.GetImportedPage(reader,j); cb.AddTemplate(newPage,0,0); } document.Close(); writer.Close(); reader.Close(); } catch (Exception ex) { WriteLog.Log(ex.ToString()); throw ex; } } 回首頁