C#中pdf產生圖片文字浮水印類的實現執行個體

來源:互聯網
上載者:User
這篇文章主要介紹了C#實現的pdf產生圖片文字浮水印類,結合完整執行個體形式分析了C#針對pdf檔案的建立、添加文字、浮水印等相關操作技巧,需要的朋友可以參考下

本文執行個體講述了C#實現的pdf產生圖片文字浮水印類。分享給大家供大家參考,具體如下:


public class PDFSetWaterMark{    /// <summary>    /// 建立一個顯示指定圖片的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對象關聯        PdfWriter.GetInstance(doc, new FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite));        //開啟一個文檔        doc.Open();        //向文檔中新增內容        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//旋轉        //image.RotationDegrees//旋轉角度        //浮水印的位置        if (left < 0)        {          left = width / 2 - image.Width + left;        }        //image.SetAbsolutePosition(left, (height - image.Height) - top);        image.SetAbsolutePosition(left, (height / 2 - image.Height) - top);        //每一頁加浮水印,也可以設定某一頁加浮水印        for (int i = 1; i <= numberOfPages; i++)        {          //waterMarkContent = pdfStamper.GetUnderContent(i);//內容下層加浮水印          waterMarkContent = pdfStamper.GetOverContent(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>    /// 添加普通偏轉角度文字浮水印    /// </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);//在內容上方加浮水印          //content = pdfStamper.GetUnderContent(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));        // 設定密碼        //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);//在內容上方加浮水印          //content = pdfStamper.GetUnderContent(i);//在內容下方加浮水印          content.BeginText();          content.SetColorFill(BaseColor.DARK_GRAY);          content.SetFontAndSize(font, 50);          // 設定浮水印文字字型傾斜 開始          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;            }          }          // 字型設定結束          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();      }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.