使用iText7操作PDF


遇到一個需求,給PDF增加一個印章,使用iText7可以很方便地實現這個需求,通過Nuget添加iText7的引用。

 1 string sourceFileName = txtFileName.Text;
 2             string destFileName = Path.GetDirectoryName(sourceFileName) + @"\" + Path.GetFileNameWithoutExtension(sourceFileName) + "_帶印章." + Path.GetExtension(sourceFileName);
 3             PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFileName), new PdfWriter(destFileName));        
 4             iText.Layout.Document document = new iText.Layout.Document(pdfDocument);        
 5             ImageData imageData = ImageDataFactory.Create(System.Windows.Forms.Application.StartupPath + @"\PASS.png");
 6             for (int i = 1; i <= pdfDocument.GetNumberOfPages(); i++)
 7             {
 8                 iText.Layout.Element.Image image = new iText.Layout.Element.Image(imageData).ScaleAbsolute(55, 55).SetFixedPosition(i, 90, 50);
 9                 document.Add(image);
10             }
11             document.Close();

PdfReader讀取源文件,PdfWriter將改動寫入新文件。
通過Layout命名空間里的Document對Pdf進行修改。
ScaleAbsolute設置圖片的大小。
SetFixedPosition設置圖片在PDF頁面中的位置,這個方法有多個重載,這里用的是第一個是頁碼,第二個參數是距離左邊的位置,第三個參數是距離頁面底部的位置。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM