//本專案使用 iText PDF 函式庫,其為 AGPL v3 授權版本: //https://github.com/itext/itext-dotnet //依據 AGPL 授權,本專案亦以 AGPL v3 授權釋出,所有原始碼公開且可自由使用、修改、散佈。 //以下程式使用.net framework 4.0,搭配iTextSharp using System; using System.Collections.Generic; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; class Program { static void Main(string[] args) { //單一資料匣轉檔 寫入另一資料匣 trandpdf(); } // 單一資料匣 public static void trandpdf() { #region iTextSharp string watermark = @"D:\TRANSFILE\LOGO\logo44.jpg"; // 浮水印圖片 #endregion List ls = new List(); string sourcepath = @"D:\TRANSFILE\PDF\source\"; //原始檔路徑 string outpath = @"D:\TRANSFILE\PDF\makeing\"; //新寫入檔路徑 FileSearch(sourcepath, "*.pdf", ref ls); string tempname, newfullname; foreach (string item in ls) { tempname = System.IO.Path.GetFileName(item); //取得檔名 newfullname = outpath + tempname; //轉檔後的全檔名 string isSurrcess = PDFWatermark(item, newfullname, watermark, 387, 295, 0, 0, 1,1); Console.WriteLine("轉出{0} , 轉檔結果:{1}", newfullname, isSurrcess); } } private static void FileSearch(string sDir, string filetype, ref List ls) { try { foreach (string f in Directory.GetFiles(sDir, filetype)) { ls.Add(f); } } catch (System.Exception excpt) { Console.WriteLine(excpt.Message); } } /// /// 浮水印功能 /// /// 輸入檔案地址 /// 輸出檔案地址 /// 圖片檔案地址 /// 圖片高度(可選) /// 圖片寬度(可選) /// 圖片在PDF中的位置Top(可選) /// 圖片在PDF中的位置Left(可選) /// 要不要加列印保護 1加 0不加(可選) /// 要不要重寫作者等meatdata資訊 1改寫 0不改寫(可選) /// public static string PDFWatermark(string infilepath, string outfilepath, string picName, float picHeight = 0, float picWidth = 0, float top = 0, float left = 0,int setencryption = 0,int setauthor=0) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; //PdfReader.unethicalreading = true; try { pdfReader = new PdfReader(infilepath); int numberOfPages = pdfReader.NumberOfPages; iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1); float width = psize.Width; float height = psize.Height; pdfStamper = new PdfStamper(pdfReader, new FileStream(outfilepath, FileMode.Create)); //加列印限制 if (setencryption==1) pdfStamper.SetEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowScreenReaders); //重寫作者等meatdata資訊 if (setauthor == 1) { Dictionary info = pdfReader.Info; info.Remove("Author"); info.Add("Author", "作者資訊"); info.Remove("Subject"); info.Add("Subject", "主旨"); info.Remove("Title"); info.Add("Title", "抬頭"); info.Remove("Keywords"); info.Add("Keywords", "關鍵字"); pdfStamper.MoreInfo = info; } PdfContentByte waterMarkContent; iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(picName); if (picHeight != 0) image.ScaleAbsoluteHeight(picHeight); else picHeight = image.Height; if (picWidth != 0) image.ScaleAbsoluteWidth(picWidth); else picWidth = image.Width; image.GrayFill = 20; // 透明度,灰色填充 // image.Rotation // 旋轉 // image.RotationDegrees // 旋轉角度 // 水印的位置 if (left == 0) left = (width - picWidth) / 2; if (top == 0) top = (height - picHeight) / 2; image.SetAbsolutePosition(left, top); // 每一頁加水印,也可以設定某一頁加水印 for (int i = 1; i <= numberOfPages; i++) { waterMarkContent = pdfStamper.GetUnderContent(i);//水印在最底層 //這邊注意,如果想要水印在最頂層,這邊改成 pdfStamper.GetOverContent waterMarkContent.AddImage(image); } return "轉檔完成"; } catch (Exception ex) { return ex.Message.Trim(); } finally { if (pdfStamper != null) pdfStamper.Close(); if (pdfReader != null) pdfReader.Close(); } } /* Copyright (C) <2020> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */