第一步通过Nuget添加iTextSharp引用,添加引用完成后就可以开始具体实现了
代码入下:
/// <summary>
/// 添加文本水印
/// </summary>
/// <param name="pdfPath">pdf文件</param>
/// <param name="outPath">输出文件位置</param>
/// <param name="addText">水印文字</param>
public static void PdfAddWatermark(string pdfPath, string outPath, string addText)
{
//读取pdf
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfPath);
//创建新pdf
System.IO.Stream outStream = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outStream)