string sFileIn = "D:\\pdftmp\\102\\09\\10209240001.pdf";
PdfReader reader = new PdfReader(sFileIn);
MemoryStream ms = new MemoryStream();
PdfStamper stamper = new PdfStamper(reader, ms);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i);
PdfContentByte pdfPageContents = stamper.GetUnderContent(i);
pdfPageContents.BeginText();
BaseFont bfChinese = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\kaiu.ttf", BaseFont.IDENTITY_V, BaseFont.NOT_EMBEDDED); //字體 標楷體直書
//BaseFont bfChinese = BaseFont.createFont("C:\\windows\\fonts\\KAIU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //字體 標楷體橫書
pdfPageContents.SetFontAndSize(bfChinese, 14);//14 字型大小
pdfPageContents.SetRGBColorFill(0, 0, 0); //文字顏色
float textAngle = 0.0f; //旋轉角度
float left = 570; //PageSize.Width / 2;
float bottom = 700; // PapeSize.A4.Hight / 2 + 120f;
pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "版權所有翻印必究。", left, bottom, textAngle);
//pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "本內容之版權由相關機構擁有", left + 15, bottom, textAngle);
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
pdfPageContents.EndText();
}
//stamper.FormFlattening = true;
//stamper.Close();
FileStream fs = new FileStream("D:\\pdftmp\\102\\09\\watermarked-output.pdf", FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(ms.ToArray());
fs.Close();
bw.Close();
用此方式如產生
『此頁上有錯誤。Acrobat可能無法正確顯示頁面。請聯繫
PDF文件的建立者來更正問題。』
請移除舊版檢視軟體(Adobe Reader),移除後請至「ADOBE READER」網站,下載並安裝Adobe Reader(須10.1.4以上版本)。