iTextSharp之pdfRead(两个文件文本内容的比较,指定页数的pdf截取,水印的添加)
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class pdfRead { /// <summary> /// 读取2个pdf的内容判断文件是否相同 /// </summary> /// <param name="filepath"></param> /// <param name="filepath2"></param> /// <returns></returns> public string reading(string filepath, string filepath2) { try { PdfReader pdfReader = new PdfReader(filepath); PdfReader pdfReader2 = new PdfReader(filepath2); int numberOfPages = pdfReader.NumberOfPages; int numberOfPages2 = pdfReader2.NumberOfPages; int minnum=Math.Min(numberOfPages, numberOfPages2);//为了防止文件页数不同赞成错误 找最小的那页 string text = string.Empty; string text2 = string.Empty; if (numberOfPages != numberOfPages2) { Console.WriteLine("pdf页数不同所以发生变更"); } for (int i = 1; i <= minnum; ++i) { text=GetTextFromPage(pdfReader, i); ///获取text内容 text2=GetTextFromPage(pdfReader2, i); if(!text.Equals(text2)) { changePdf(filepath2); } } pdfReader.Close(); return text; } catch (Exception ex) { return ex.ToString(); } } public static string GetTextFromPage(PdfReader reader, int pageNum) { ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy(); return PdfTextExtractor.GetTextFromPage(reader, pageNum, strategy); } public static void changePdf(string filepath) { Document sourceDocument = null; PdfCopy pdfCopyProvider = null; PdfImportedPage importedPage = null; BaseFont simheiBase = BaseFont.CreateFont(@"C:/Windows/Fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //设置字体 Font simheiFont = new Font(simheiBase); simheiFont.Color = BaseColor.RED; simheiFont.Size = 16f; PdfReader reader = new PdfReader(@"D:\needchange.pdf"); Rectangle pagesize = reader.GetPageSize(1); #region 截取pdf的指定页 sourceDocument = new Document(reader.GetPageSizeWithRotation(1)); pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(@"F:\NewDoc.pdf", System.IO.FileMode.Create)); sourceDocument.Open(); //截取1-3页 for (int i = 1; i <= 3; i++) { importedPage = pdfCopyProvider.GetImportedPage(reader, i); pdfCopyProvider.AddPage(importedPage); } pdfCopyProvider.AddPage(importedPage); sourceDocument.Close(); #endregion #region 添加水印 PdfStamper stamper = new PdfStamper(reader, new FileStream(@"F:\NewDoc1.pdf", FileMode.Create)); //内容下层加水印 GetUnderContent(i) PdfContentByte canvas = stamper.GetOverContent(1); ColumnText.ShowTextAligned(canvas, Element.ALIGN_RIGHT, new Phrase("[HaveChanged!!!]", simheiFont), pagesize.Right-20, pagesize.Top-40, 0); stamper.Close(); reader.Close(); #endregion 添加水印 Process.Start(@"F:\NewDoc.pdf"); Process.Start(@"F:\NewDoc1.pdf"); } /// <summary> /// 合并pdf /// </summary> /// <param name="pdfList"></param> public static void mergePDFFiles(List<string> pdfList) { string mergePDFFiles = @"D:\pdfhebing.pdf"; Document DOC = new Document(); //getInstance PdfWriter writer = PdfWriter.GetInstance(DOC, new FileStream(mergePDFFiles, FileMode.Create)); DOC.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; for (int i = 0; i < pdfList.Count; i++) { PdfReader reader = new PdfReader(pdfList[i]); int iPageNum = reader.NumberOfPages; for (int j = 1; j <= iPageNum; j++) { DOC.NewPage(); //DOC.newPage(); newPage = writer.GetImportedPage(reader, j); //newPage = writer.getImportedPage(reader, j); cb.AddTemplate(newPage, 0, 0); //cb.addTemplate(newPage, 0, 0); } } DOC.Close(); Process.Start(@"D:\pdfhebing.pdf"); } } }