C#利用iTextSharp组件给PDF文档添加图片/文字水印
作者:细数青春 时间:2021-11-03 20:18:31
最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了。以下做个记录:
首先会用到iTextSharp组件,大家可以去官网下载,同时我也会在本文中附加进来。
代码中添加引用为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text;
创建一个显示指定图片的pdf
/// <summary>
/// 创建一个显示指定图片的pdf
/// </summary>
/// <param name="picPdfPath"></param>
/// <param name="picPath"></param>
/// <returns></returns>
public static bool CreatePDFByPic(string picPdfPath,string picPath)
{
//新建一个文档
Document doc = new Document();
try
{
//建立一个书写器(Writer)与document对象关联
PdfWriter.GetInstance(doc, new FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite));
//打开一个文档
doc.Open();
//向文档中添加内容
Image img = Image.GetInstance(picPath);
//img.SetAbsolutePosition();
doc.Add(img);
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
finally
{
if (doc != null)
{
doc.Close();
}
}
}
为PDF文档添加图片水印
/// <summary>
/// 加图片水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="ModelPicName"></param>
/// <param name="top"></param>
/// <param name="left"></param>
/// <returns></returns>
public static bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)
{
//throw new NotImplementedException();
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
int numberOfPages = pdfReader.NumberOfPages;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
PdfContentByte waterMarkContent;
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);
image.GrayFill = 20;//透明度,灰色填充
//image.Rotation//旋转
//image.RotationDegrees//旋转角度
//水印的位置
if (left < 0)
{
left = width/2 - image.Width + left;
}
//image.SetAbsolutePosition(left, (height - image.Height) - top);
image.SetAbsolutePosition(left, (height/2 - image.Height) - top);
//每一页加水印,也可以设置某一页加水印
for (int i = 1; i <= numberOfPages; i++)
{
//waterMarkContent = pdfStamper.GetUnderContent(i);//内容下层加水印
waterMarkContent = pdfStamper.GetOverContent(i);//内容上层加水印
waterMarkContent.AddImage(image);
}
//strMsg = "success";
return true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
添加普通偏转角度文字水印
/// <summary>
/// 添加普通偏转角度文字水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="waterMarkName"></param>
/// <param name="permission"></param>
public static void setWatermark(string inputfilepath, string outputfilepath,string waterMarkName)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
int total = pdfReader.NumberOfPages + 1;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++)
{
content = pdfStamper.GetOverContent(i);//在内容上方加水印
//content = pdfStamper.GetUnderContent(i);//在内容下方加水印
//透明度
gs.FillOpacity = 0.3f;
content.SetGState(gs);
//content.SetGrayFill(0.3f);
//开始写入文本
content.BeginText();
content.SetColorFill(BaseColor.LIGHT_GRAY);
content.SetFontAndSize(font, 100);
content.SetTextMatrix(0, 0);
content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50,height / 2 - 50, 55);
//content.SetColorFill(BaseColor.BLACK);
//content.SetFontAndSize(font, 8);
//content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0);
content.EndText();
}
}catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
添加倾斜水印
/// <summary>
/// 添加倾斜水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="waterMarkName"></param>
/// <param name="userPassWord"></param>
/// <param name="ownerPassWord"></param>
/// <param name="permission"></param>
public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
// 设置密码
//pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission);
int total = pdfReader.NumberOfPages + 1;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
gs.FillOpacity = 0.2f;//透明度
int j = waterMarkName.Length;
char c;
int rise = 0;
for (int i = 1; i < total; i++)
{
rise = 500;
content = pdfStamper.GetOverContent(i);//在内容上方加水印
//content = pdfStamper.GetUnderContent(i);//在内容下方加水印
content.BeginText();
content.SetColorFill(BaseColor.DARK_GRAY);
content.SetFontAndSize(font, 50);
// 设置水印文字字体倾斜 开始
if (j >= 15)
{
content.SetTextMatrix(200, 120);
for (int k = 0; k < j; k++)
{
content.SetTextRise(rise);
c = waterMarkName[k];
content.ShowText(c + "");
rise -= 20;
}
}
else
{
content.SetTextMatrix(180, 100);
for (int k = 0; k < j; k++)
{
content.SetTextRise(rise);
c = waterMarkName[k];
content.ShowText(c + "");
rise -= 18;
}
}
// 字体设置结束
content.EndText();
// 画一个圆
//content.Ellipse(250, 450, 350, 550);
//content.SetLineWidth(1f);
//content.Stroke();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
调用方法的例子:
string path = "D://my.pdf";
HtmlToPDFUtil.HtmlToPdf(Request.Url.AbsoluteUri, path);
//HtmlToPDFUtil.HtmlToPdf(sbUrlList.ToString(), path);
PDFSetWaterMark.PDFWatermark(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0);
PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST");
//PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST", "", "", 1);
附件:iTextSharp
来源:https://www.cnblogs.com/xishuqingchun/p/3838185.html
标签:itextsharp,pdf,水印
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
hadoop中实现java网络爬虫(示例讲解)
2021-12-22 01:35:20
![](https://img.aspxhome.com/file/2023/2/77092_0s.png)
Java 中的抽象介绍
2023-03-27 06:10:20
spring中使用@Autowired注解无法注入的情况及解决
2023-06-10 21:48:24
![](https://img.aspxhome.com/file/2023/9/64159_0s.jpg)
flutter material widget组件之信息展示组件使用详解
2023-06-22 08:45:35
Java设计模式之java原型模式详解
2022-04-01 03:16:40
![](https://img.aspxhome.com/file/2023/8/88568_0s.png)
10种简单的Java性能优化
2023-06-20 20:43:41
![](https://img.aspxhome.com/file/2023/6/57526_0s.png)
详解Java中类的加载与其初始化
2023-06-21 04:56:45
![](https://img.aspxhome.com/file/2023/3/65603_0s.png)
Struts和servlet不能共存问题解决方法
2022-09-20 07:26:59
关于Java的ArrayList数组自动扩容机制
2021-11-26 13:12:46
Java逃逸分析详解及代码示例
2021-05-29 15:23:15
java oshi如何查看cpu信息
2023-01-06 21:16:39
Java多线程之同步工具类CyclicBarrier
2021-10-13 00:24:20
Android开发 旋转屏幕导致Activity重建解决方法
2022-03-02 06:00:23
分享Java常用开发编辑器工具
2023-11-06 07:35:37
![](https://img.aspxhome.com/file/2023/6/58896_0s.png)
java8 stream中Collectors.toMap空指针问题及解决
2023-01-16 13:05:28
![](https://img.aspxhome.com/file/2023/2/63662_0s.png)
C#读取Excel到DataTable的方法示例
2023-04-28 07:04:32
java图片滑动验证(登录验证)原理与实现方法详解
2023-07-10 13:29:53
![](https://img.aspxhome.com/file/2023/8/72538_0s.jpg)
SpringBoot项目中遇到的BUG问题及解决方法
2022-01-19 14:44:38
![](https://img.aspxhome.com/file/2023/7/63287_0s.png)
Java编程计算兔子生兔子的问题
2023-08-01 15:24:37
Spring Boot实现分布式锁的自动释放的示例代码
2023-10-17 11:06:24