itextsharp 水印

itextsharp 水印,第1张

public class WaterMark

{

static string strPath = @"C:\Users\Administrator\Desktop\Test\TEST\ConsoleApplication1\bin\Debug\pic\"

public static void CreateWaterMark()

{

try

{

//////参数说明

//inputPath 需要增加水印的pdf文件位置

//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)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11642835.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存