C#使用iTextSharp给PDF添加水印
2016-10-10 10:41
483 查看
代码:
View Code
/// <summary> /// 添加普通偏转角度文字水印 /// </summary> public static void SetWatermark(string filePath, string text) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; string tempPath = Path.GetDirectoryName(filePath) + Path.GetFileNameWithoutExtension(filePath) + "_temp.pdf"; try { pdfReader = new PdfReader(filePath); pdfStamper = new PdfStamper(pdfReader, new FileStream(tempPath, 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.GRAY); content.SetFontAndSize(font, 30); content.SetTextMatrix(0, 0); content.ShowTextAligned(Element.ALIGN_CENTER, text, width - 120, height - 120, -45); //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(); System.IO.File.Copy(tempPath, filePath, true); System.IO.File.Delete(tempPath); } }
View Code
相关文章推荐
- C#使用iTextSharp添加PDF水印
- C#使用iTextSharp为pdf添加文字
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- C#利用iTextSharp添加PDF水印
- GdPicture.NET使用教程:在PDF文件中添加水印
- C#使用itextsharp生成PDF文件
- C# PDF添加水印
- c#使用itextsharp输出pdf(动态填充表单内容,显示中文)
- GdPicture.NET使用教程:在PDF文件中添加水印
- iTextSharp简单生成pdf和操作pdf添加水印
- C#使用iTextSharp从PDF文档获取内容的方法
- C# 使用 itextsharp 对pdf文件进行分割的几种方法(1)——按页码拷贝分割
- C# 给PDF文件添加水印
- C#使用itextsharp生成PDF文件
- C#使用iTextSharp设置PDF所有页面背景图功能实例
- [.Net C#]利用iTextSharp在PDF上新增浮水印(WaterMark)
- C# 中使用iTextSharp组件创建PDF的简单方法
- iTextSharp简单生成pdf和操作pdf添加水印
- 给PDF添加水印(Python+C#)
- 使用 ItextSharp HTML生成Pdf(C#)