Spire.PDF是一個專業的PDF組件,可以獨立地建立、編寫、編輯、操做和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API擁有豐富的功能,如安全設置(包括數字簽名)、PDF文本/附件/圖片提取、PDF文件合併/拆分、元數據更新、章節和段落優化、圖形/圖像描繪和插入、表格建立和處理、數據導入等等。安全
【下載Spire.PDF最新試用版】學習
騎縫章常見於合同等比較重要的文件上,具備防止在文件內增減頁碼的做用。本文將介紹如何使用Spire.PDF給PDF格式的合同文件添加騎縫章。優化
原PDF文件以下:spa
using System.Collections.Generic; using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace PagingSeal { class Program { static void Main(string[] args) { //加載PDF文檔 PdfDocument doc = new PdfDocument(); doc.LoadFromFile("Input.pdf"); PdfUnitConvertor convert = new PdfUnitConvertor(); PdfPageBase pageBase = null; //獲取分割後的印章圖片 Image[] images = GetImage(doc.Pages.Count); float x = 0; float y = 0; //將圖片畫到PDF頁面上的指定位置 for (int i = 0; i < doc.Pages.Count; i++) { pageBase = doc.Pages[i]; x = pageBase.Size.Width - convert.ConvertToPixels(images[i].Width, PdfGraphicsUnit.Point) - 40; y = pageBase.Size.Height / 2; pageBase.Canvas.DrawImage(PdfImage.FromImage(images[i]), new PointF(x, y)); } //保存PDF文件 doc.SaveToFile("Output.pdf"); doc.Close(); } //定義GetImage方法,根據PDF頁數分割印章圖片 static Image[] GetImage(int num) { List lists = new List(); Image image = Image.FromFile("SealImage.jpg"); int w = image.Width / num; Bitmap bitmap = null; for (int i = 0; i < num; i++) { bitmap = new Bitmap(w, image.Height); using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) { g.Clear(Color.White); Rectangle rect = new Rectangle(i * w, 0, w, image.Height); g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), rect, GraphicsUnit.Pixel); } lists.Add(bitmap); } return lists.ToArray(); } } }
添加騎縫章效果:orm
接下來咱們來學習如何查找和高亮PDF中的文本。對於跨行的文本,在使用FindText方法查找時須要將TextFindParameter參數設置爲CrossLine。blog
//加載PDF文檔 PdfDocument doc = new PdfDocument(@"C:\Users\Administrator\Desktop\input.pdf"); //在第一頁查找字符串全部出處,即便字符串是跨行顯示 PdfTextFind[] findResults = doc.Pages[0].FindText("Spire.PDF for .NET還支持PDF數字簽名",TextFindParameter.CrossLine).Finds; //高亮第一個查找結果 findResults[0].ApplyHighLight(Color.Yellow); //保存文檔 doc.SaveToFile("output.pdf", FileFormat.PDF);
效果圖以下:教程
若是你有任何問題或意見,可在下方評論區留言,點擊資源列表查看更多教程資源~圖片