欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的pdf生成图片文字水印类实例

程序员文章站 2023-12-12 14:59:58
本文实例讲述了c#实现的pdf生成图片文字水印类。分享给大家供大家参考,具体如下: public class pdfsetwatermark { //...

本文实例讲述了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#相关内容感兴趣的读者可查看本站专题:《c#图片操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。

上一篇:

下一篇: