C#實現(xiàn)的pdf生成圖片文字水印類實例
本文實例講述了C#實現(xiàn)的pdf生成圖片文字水印類。分享給大家供大家參考,具體如下:
public class PDFSetWaterMark { /// <summary> /// 創(chuàng)建一個顯示指定圖片的pdf /// </summary> /// <param name="picPdfPath"></param> /// <param name="picPath"></param> /// <returns></returns> public static bool CreatePDFByPic(string picPdfPath, string picPath) { //新建一個文檔 Document doc = new Document(); try { //建立一個書寫器(Writer)與document對象關(guān)聯(lián) PdfWriter.GetInstance(doc, new FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite)); //打開一個文檔 doc.Open(); //向文檔中添加內(nèi)容 Image img = Image.GetInstance(picPath); //img.SetAbsolutePosition(); doc.Add(img); return true; } catch (Exception ex) { return false; throw ex; } finally { if (doc != null) { doc.Close(); } } } /// <summary> /// 加圖片水印 /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="ModelPicName"></param> /// <param name="top"></param> /// <param name="left"></param> /// <returns></returns> public static bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left) { //throw new NotImplementedException(); PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); 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(outputfilepath, FileMode.Create)); PdfContentByte waterMarkContent; iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName); image.GrayFill = 20;//透明度,灰色填充 //image.Rotation//旋轉(zhuǎn) //image.RotationDegrees//旋轉(zhuǎn)角度 //水印的位置 if (left < 0) { left = width / 2 - image.Width + left; } //image.SetAbsolutePosition(left, (height - image.Height) - top); image.SetAbsolutePosition(left, (height / 2 - image.Height) - top); //每一頁加水印,也可以設(shè)置某一頁加水印 for (int i = 1; i <= numberOfPages; i++) { //waterMarkContent = pdfStamper.GetUnderContent(i);//內(nèi)容下層加水印 waterMarkContent = pdfStamper.GetOverContent(i);//內(nèi)容上層加水印 waterMarkContent.AddImage(image); } //strMsg = "success"; return true; } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) pdfStamper.Close(); if (pdfReader != null) pdfReader.Close(); } } /// <summary> /// 添加普通偏轉(zhuǎn)角度文字水印 /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="waterMarkName"></param> /// <param name="permission"></param> public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create)); int total = pdfReader.NumberOfPages + 1; iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1); float width = psize.Width; float height = psize.Height; PdfContentByte content; BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); PdfGState gs = new PdfGState(); for (int i = 1; i < total; i++) { content = pdfStamper.GetOverContent(i);//在內(nèi)容上方加水印 //content = pdfStamper.GetUnderContent(i);//在內(nèi)容下方加水印 //透明度 gs.FillOpacity = 0.3f; content.SetGState(gs); //content.SetGrayFill(0.3f); //開始寫入文本 content.BeginText(); content.SetColorFill(BaseColor.LIGHT_GRAY); content.SetFontAndSize(font, 100); content.SetTextMatrix(0, 0); content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55); //content.SetColorFill(BaseColor.BLACK); //content.SetFontAndSize(font, 8); //content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0); content.EndText(); } } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) pdfStamper.Close(); if (pdfReader != null) pdfReader.Close(); } } /// <summary> /// 添加傾斜水印 /// </summary> /// <param name="inputfilepath"></param> /// <param name="outputfilepath"></param> /// <param name="waterMarkName"></param> /// <param name="userPassWord"></param> /// <param name="ownerPassWord"></param> /// <param name="permission"></param> public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission) { PdfReader pdfReader = null; PdfStamper pdfStamper = null; try { pdfReader = new PdfReader(inputfilepath); pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create)); // 設(shè)置密碼 //pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission); int total = pdfReader.NumberOfPages + 1; PdfContentByte content; BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); PdfGState gs = new PdfGState(); gs.FillOpacity = 0.2f;//透明度 int j = waterMarkName.Length; char c; int rise = 0; for (int i = 1; i < total; i++) { rise = 500; content = pdfStamper.GetOverContent(i);//在內(nèi)容上方加水印 //content = pdfStamper.GetUnderContent(i);//在內(nèi)容下方加水印 content.BeginText(); content.SetColorFill(BaseColor.DARK_GRAY); content.SetFontAndSize(font, 50); // 設(shè)置水印文字字體傾斜 開始 if (j >= 15) { content.SetTextMatrix(200, 120); for (int k = 0; k < j; k++) { content.SetTextRise(rise); c = waterMarkName[k]; content.ShowText(c + ""); rise -= 20; } } else { content.SetTextMatrix(180, 100); for (int k = 0; k < j; k++) { content.SetTextRise(rise); c = waterMarkName[k]; content.ShowText(c + ""); rise -= 18; } } // 字體設(shè)置結(jié)束 content.EndText(); // 畫一個圓 //content.Ellipse(250, 450, 350, 550); //content.SetLineWidth(1f); //content.Stroke(); } } catch (Exception ex) { throw ex; } finally { if (pdfStamper != null) pdfStamper.Close(); if (pdfReader != null) pdfReader.Close(); } } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#圖片操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》及《C#程序設(shè)計之線程使用技巧總結(jié)》
希望本文所述對大家C#程序設(shè)計有所幫助。
相關(guān)文章
C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解
這篇文章主要介紹了C# 總結(jié)QueueUserWorkItem傳參幾種方式案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09快速了解如何在.NETCORE中使用Generic-Host建立主機
這篇文章主要介紹了如何在.NETCORE中使用Generic-Host建立主機,文中代碼非常詳細,可供大家參考,感興趣的朋友不妨閱讀完2020-05-05C#連接操作 MySQL 數(shù)據(jù)庫實例(使用官方驅(qū)動)
這篇文章主要介紹了C#連接操作 MySQL 數(shù)據(jù)庫實例(使用官方驅(qū)動),本文講解了C#中的Mysql連接方法和SQL操作方法,需要的朋友可以參考下2015-02-02C# 結(jié)合 Javascript 測試獲取天氣信息
本文將介紹如何使用 C# 并結(jié)合 JavaScript 獲取天氣信息,獲取的數(shù)據(jù)來源于360瀏覽器首頁數(shù)據(jù),對C# 獲取天氣信息示例代碼感興趣的朋友一起看看吧2024-08-08Silverlight文件上傳下載實現(xiàn)方法(下載保存)
這篇文章主要介紹了Silverlight文件上傳下載實現(xiàn)方法(下載保存) ,需要的朋友可以參考下2015-11-11在Winform和WPF中注冊全局快捷鍵實現(xiàn)思路及代碼
如果注冊快捷鍵,RegisterHotKey中的fsModifiers參數(shù)為0,即None選項,一些安全軟件會警報,可能因為這樣就可以全局監(jiān)聽鍵盤而造成安全問題,感興趣的你可以參考下本文2013-02-02