91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#中如何實現pdf生成圖片文字水印類

發布時間:2020-10-24 16:46:43 來源:億速云 閱讀:168 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關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();
      }
    }
}

感謝各位的閱讀!關于C#中如何實現pdf生成圖片文字水印類就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

洛扎县| 盘锦市| 辽阳市| 耒阳市| 禹州市| 崇仁县| 新干县| 清镇市| 仁寿县| 霍邱县| 广元市| 湘潭市| 嘉祥县| 徐汇区| 花莲市| 盐津县| 漯河市| 西城区| 云南省| 梅河口市| 额尔古纳市| 原平市| 梁平县| 金山区| 遵义县| 积石山| 绵竹市| 依安县| 洪湖市| 泾源县| 周至县| 柳林县| 山西省| 曲水县| 孟州市| 芮城县| 昌吉市| 兰考县| 出国| 噶尔县| 肇东市|