PdfiumViewer组件扩展(Pdfium.Net.Free)--添加水印
项目地址:
Pdfium.Net:https://github.com/1000374/Pdfium.Net.Free
PdfiumViewer:https://github.com/1000374/PdfiumViewer
Pdfium.Net.Free 支持
-
.NETFramework 4.0
-
.NETFramework 4.5
-
.NETStandard 2.0
- .Net8.0
可以和PdfiumViewer.Free共同使用预览、编辑pdf,也可以直接引用Pdfium.Net.Free 操作pdf,Pdfium.Net.Free封装了现有Pdfium的函数,实现了部分操作pdf的功能,部分功能等待后续~~
添加水印函数定义:
/// <summary> /// Add WaterMark /// </summary> /// <param name="text"></param> /// <param name="font_size"></param> /// <param name="fillColor"></param> /// <param name="rotate">+ RightTop - RightBottom</param> /// <param name="totleHeight">default 100</param> /// <param name="render_mode">render_mode<see cref="FpdfTextRenderMode"/></param> /// <param name="strokeColor">stroke Color render_mode </param> /// <param name="strokeWidth">stroke Width</param> /// <returns></returns> /// <exception cref="ArgumentException"></exception> public bool WaterMark(string text, float font_size, Color fillColor, float rotate = 0, float totleHeight = 100f, FpdfTextRenderMode render_mode = FpdfTextRenderMode.FILL, Color? strokeColor = null, float strokeWidth = 0.2f)
支持实体水印、镂空水印两种模式,通过render_mode 参数控制;
其他参数:
fillColor:改变颜色及透明度
strokeColor、strokeWidth :镂空时生效
rotate :改变水印倾斜度
水印密集程度由totleHeight 及font_size控制
- 实体水印:FpdfTextRenderMode.FILL
using (var doc = PdfDocument.CreateNew()) { doc.Pages.Add(612, 792); var fontPath = @"c:\Windows\fonts\simhei.ttf"; var cSharpString = "我是水印"; doc.LoadFont(fontPath, true, cSharpString); doc.WaterMark(cSharpString, 20, Color.FromArgb(50, 255, 0, 0), totleHeight: 120); doc.Save("./Pdfium.NetTests/WaterMark.pdf"); }
效果:
- 镂空水印:FpdfTextRenderMode.STROKE
using (var doc = PdfDocument.CreateNew()) { doc.Pages.Add(612, 792); var fontPath = @"c:\Windows\fonts\simhei.ttf"; doc.LoadFont(fontPath); var cSharpString = "我是水印"; doc.WaterMark(cSharpString, 20, Color.FromArgb(50, 255, 0, 0), totleHeight: 120, render_mode: FpdfTextRenderMode.STROKE); doc.Save("./Pdfium.NetTests/WaterMark1.pdf"); }
效果: