
{
static string strPath = @"C:\Users\Administrator\Desktop\Test\TEST\ConsoleApplication1\bin\Debug\pic\"
public static void CreateWaterMark()
{
try
{
//////参数说明
//outputPath 增加水印后,新生成的pdf文件位置
//watermarkimagepath 水印图片的位置
///////////////
string inputPath = @"C:\Users\Administrator\Desktop\Test\TEST\TEMP_PDF\Chap0605.pdf"
string outputPath = @"C:\Users\Administrator\Desktop\Test\TEST\TEMP_PDF\Chap0605_new.pdf"
string watermarkPath = strPath + "logo.png"
PdfReader pdfReader = new PdfReader(inputPath)
int numberOfPages = pdfReader.NumberOfPages
FileStream outputStream = new FileStream(outputPath, FileMode.Create)
PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream)
PdfContentByte waterMarkContent
string watermarkimagepath = watermarkPath
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(watermarkimagepath)
image.GrayFill = 100
image.RotationDegrees = -45
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1)
float width = psize.Width
float height = psize.Height
float left = width / 2 - image.Width + 80
float top = height / 2 - image.Height
image.SetAbsolutePosition(left, top)
for (int i = 1i <= numberOfPagesi++)
{
//waterMarkContent = pdfStamper.GetUnderContent(i)
waterMarkContent = pdfStamper.GetOverContent(i)
PdfGState gs = new PdfGState()
gs.FillOpacity = 0.3f
waterMarkContent.SetGState(gs)
waterMarkContent.AddImage(image)
}
pdfStamper.Close()
pdfReader.Close()
}
catch (Exception ex)
{
// WriteLog.Log(ex.ToString())
throw ex
}
}
}
public PdfContentByte GetOverContent(int pageNum){
return this.stamper.GetOverContent(pageNum)
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)