给PDF添加水印,可以用iTextSharp。
步骤1:下载iTextSharp
步骤2:在项目中添加引用itextsharp.dll
步骤3:在程序中使用iTextSharp.text.pdf
using iTextSharp.text.pdf;
步骤5:详细实现
public static bool AddWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
int numberOfPages = pdfReader.NumberOfPages;
iTextSharp.text.Rectangle pagesize = pdfReader.GetPageSize();
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
PdfContentByte waterMarkContent;
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);
image.GrayFill = ;//透明度,灰色填充 float waterLeft = pagesize.Width - image.Width - left;
float waterTop = pagesize.Height - image.Height - top;
image.SetAbsolutePosition(waterLeft, waterTop); for (int i = ; i <= numberOfPages; i++)
{
waterMarkContent = pdfStamper.GetOverContent(i);//.GetUnderContent(i);
waterMarkContent.AddImage(image);
}
return true;
}
catch (Exception ex)
{
ex.Message.Trim();
return false;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close(); if (pdfReader != null)
pdfReader.Close();
}
}