1、用iTextSharp去除文字水印。(水印文字需在contents中)
————Removing Watermark from a PDF using iTextSharp
2、用iTextSharp去除图片水印。(水印图片需为xobject对象)
————Remove mask p_w_picpath“Watermark” from PDF itextsharp
3、使用PDFlib-6.0.2库生成PDF文档的水印清除方法
example code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace ClearWatermark { class Program { static void Main(string[] args) { string pathfile_in = "D:\\test\\input\\test.pdf"; string pathfile_out = "D:\\test\\output\\test.pdf"; ProcOne4Contents(pathfile_in, pathfile_out); ProcOne4Xobject(pathfile_in, pathfile_out); Console.ReadKey(); } //去文字水印示例(walker) static bool ProcOne4Contents(string pathfile_in, string pathfile_out) { PdfReader pdfReader = new PdfReader(pathfile_in); PRStream stream; String content; PdfDictionary page; PdfArray contentarray; int pageCount = pdfReader.NumberOfPages; for (int i = 1; i <= pageCount; ++i) { page = pdfReader.GetPageN(i); contentarray = page.GetAsArray(PdfName.CONTENTS); if (contentarray != null) { for (int j = 0; j < contentarray.Size; j++) { stream = (PRStream)contentarray.GetAsStream(j); content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream)); //if (content.IndexOf("/OC") >= 0 && content.IndexOf("I'm watermark") >= 0) if (content.Length == 123 && content.IndexOf("I'm watermark") >= 0) { stream.Put(PdfName.LENGTH, new PdfNumber(0)); stream.SetData(new byte[0]); } } } } pdfReader.RemoveUnusedObjects(); //写到输出文件 using (FileStream outStream = new FileStream(pathfile_out, FileMode.Create, FileAccess.Write, FileShare.None)) { using (PdfStamper stamper = new PdfStamper(pdfReader, outStream)) { } } return true; } //去图片水印示例(walker) static bool ProcOne4XObject(string pathfile_in, string pathfile_out) { string pdfTempFilename = pathfile_out; FileStream outStream = new FileStream(pdfTempFilename, FileMode.Create); PdfReader pdfReader = new PdfReader(pathfile_in); PdfStamper stp = new PdfStamper(pdfReader, outStream); //关联输入和输出 PdfDictionary page1 = pdfReader.GetPageN(1); PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(page1.Get(PdfName.RESOURCES)); PdfDictionary xobjDict = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)); if (xobjDict != null) { foreach (PdfName name in xobjDict.Keys) { //Console.WriteLine("name:" + name.ToString()); PdfObject obj = xobjDict.Get(name); if (obj.IsIndirect()) { PdfDictionary objDict = (PdfDictionary)PdfReader.GetPdfObject(obj); PdfName type = objDict.GetAsName(PdfName.SUBTYPE); //Console.WriteLine("type:" + type); if (PdfName.IMAGE.Equals(type) && name.ToString().IndexOf("I1") > 0) { PdfReader.KillIndirect(obj); } } } } pdfReader.RemoveUnusedObjects(); stp.Close(); return true; } /*通过提取内容创建新PDF来变相去水印(walker) * 参考:http://www.sharejs.com/codes/csharp/8619 * http://www.4guysfromrolla.com/articles/030911-1.aspx * https://gist.github.com/7shi/805326 */ static void ProcOneByExtract(string pathfile_in, string pathfile_out) { PdfReader pdfReader = new PdfReader(pathfile_in); Document outDocument = new Document(); FileStream outStream = new FileStream(pathfile_out, FileMode.Create); PdfWriter pdfWriter = PdfWriter.GetInstance(outDocument, outStream); outDocument.Open(); int pageNum = pdfReader.NumberOfPages; Console.WriteLine("pageNum:" + pageNum); for (int i = 1; i <= pageNum; i++) { PdfDictionary pg = pdfReader.GetPageN(i); PdfDictionary res = PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)) as PdfDictionary; PdfDictionary xobj = PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)) as PdfDictionary; if (xobj == null) { Console.WriteLine("xobj == null"); continue; } var keys = xobj.Keys; if (keys.Count == 0) { Console.WriteLine("keys.Count == 0"); continue; } PdfObject obj = xobj.Get(keys.ElementAt(0)); if (!obj.IsIndirect()) { Console.WriteLine("!obj.IsIndirect()"); continue; } PdfDictionary tg = PdfReader.GetPdfObject(obj) as PdfDictionary; PdfName type = PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)) as PdfName; Console.WriteLine("type:" + type); if (!PdfName.IMAGE.Equals(type)) { continue; } int XrefIndex = (obj as PRIndirectReference).Number; PRStream pdfStream = pdfReader.GetPdfObject(XrefIndex) as PRStream; byte[] data = PdfReader.GetStreamBytesRaw(pdfStream); Image img = Image.GetInstance(obj as PRIndirectReference); float pageWidth = pdfReader.GetPageSize(i).Width; float pageHeight = pdfReader.GetPageSize(i).Height; float imgWidth = img.Width; float imgHeight = img.Height; outDocument.SetPageSize(new Rectangle(pageWidth, pageHeight)); outDocument.NewPage(); img.ScaleToFit(pageWidth, pageHeight); img.SetAbsolutePosition(0, 0); outDocument.Add(img); } outDocument.Close(); } } }
*** walker ***
转载于:https://blog.51cto.com/walkerqt/1408792