本文實(shí)例講述了C#實(shí)現(xiàn)的pdf生成圖片文字水印類(lèi)。分享給大家供大家參考,具體如下:
成都服務(wù)器托管,創(chuàng)新互聯(lián)公司提供包括服務(wù)器租用、成都棕樹(shù)電信機(jī)房、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、域名注冊(cè)等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢(xún):028-86922220public class PDFSetWaterMark { ////// 創(chuàng)建一個(gè)顯示指定圖片的pdf /// /// /// ///public static bool CreatePDFByPic(string picPdfPath, string picPath) { //新建一個(gè)文檔 Document doc = new Document(); try { //建立一個(gè)書(shū)寫(xiě)器(Writer)與document對(duì)象關(guān)聯(lián) PdfWriter.GetInstance(doc, new FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite)); //打開(kāi)一個(gè)文檔 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(); } } } /// /// 加圖片水印 /// /// /// /// /// /// ///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); //每一頁(yè)加水印,也可以設(shè)置某一頁(yè)加水印 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(); } } /// /// 添加普通偏轉(zhuǎn)角度文字水印 /// /// /// /// /// 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); //開(kāi)始寫(xiě)入文本 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(); } } ////// 添加傾斜水印 /// /// /// /// /// /// /// 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è)置水印文字字體傾斜 開(kāi)始 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(); // 畫(huà)一個(gè)圓 //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(); } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。