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

C#利用iTextSharp添加PDF水印

程序员文章站 2023-11-25 15:26:34
本文实例为大家分享了使用的是itextsharp添加pdf水印的具体代码,供大家参考,具体内容如下 需要itextsharp.dll, 下载地址 public...

本文实例为大家分享了使用的是itextsharp添加pdf水印的具体代码,供大家参考,具体内容如下

需要itextsharp.dll, 下载地址

public void test()
    {
      watermark(@"e:\日常工作\12084347 config.pdf", @"e:\日常工作\12084347 config wm.pdf", @"e:\日常工作\wm.png");
}    
 public bool addwatermark(string inputpath, string outputpath, string watermarkpath, ref string error)
    {
      try
      {
        pdfreader pdfreader = new pdfreader(inputpath);
        int numberofpages = pdfreader.numberofpages;
        filestream outputstream = new filestream(outputpath, filemode.create);
        pdfstamper pdfstamper = new pdfstamper(pdfreader, outputstream);
        pdfcontentbyte watermarkcontent;

        itextsharp.text.image image = null;
        if (string.isnullorempty(watermarkpath))
        {
          stream s = gettype().assembly.getmanifestresourcestream("watermarktool.wm.png");
          image = itextsharp.text.image.getinstance(s);
        }
        else
        {
          image = itextsharp.text.image.getinstance(watermarkpath);
        }        
        image.setabsoluteposition(100, 100);
        for (int i = 1; i <= numberofpages; i++)
        {
          watermarkcontent = pdfstamper.getundercontent(i);
          watermarkcontent.addimage(image);
        }
        pdfstamper.close();
        pdfreader.close();
        outputstream.close();
        return true;
      }
      catch (exception ex)
      {
        error = ex.stacktrace;
        return false;
      }
    }

 //选择文件夹
    private void textbox2_doubleclick(object sender, eventargs e)
    {
      folderbrowserdialog dialog = new folderbrowserdialog();
      dialog.description = label2.text;
      if (dialog.showdialog() == dialogresult.ok)
      {
        textbox2.text = dialog.selectedpath;
      }
    }

    //选择文件
    private void textbox3_doubleclick(object sender, eventargs e)
    {
      openfiledialog filedialog = new openfiledialog();
      filedialog.multiselect = true;
      filedialog.title = label3.text;
      filedialog.filter = "*.jpg|*.jpg|*.jpeg|*.jpeg|*.bmp|*.bmp|*.gif|*.gif|*.png|*.png|*.tiff|*.tiff|*.wmf|*.wmf";
      if (filedialog.showdialog() == dialogresult.ok)
      {
        textbox3.text = filedialog.filename;
      }
    }

 //启动线程
    private void button1_click(object sender, eventargs e)
    {
      if (directory.exists(textbox1.text) == false )
      {
        messagebox.show(label1.text, "require input", messageboxbuttons.ok, messageboxicon.warning);
        textbox1.focus();
        return;
      }
      if (directory.exists(textbox2.text) == false)
      {
        messagebox.show(label2.text, "require input", messageboxbuttons.ok, messageboxicon.warning);
        textbox2.focus();
        return;
      }
      if ( textbox3.enabled && file.exists(textbox3.text) == false)
      {
        messagebox.show(label3.text, "require input", messageboxbuttons.ok, messageboxicon.warning);
        textbox3.focus();
        return;
      }

      richtextbox1.clear();
      button1.enabled = false;
      thread thread = new thread(new threadstart(this.batchdo));

      thread.isbackground = true;
      thread.start(); 
    }

 public delegate void setcontrolvalue(string message);
    //在线程中修改控件属性
    public void appendrtbtext(string text)
    {
      if (richtextbox1.invokerequired)
      {
        setcontrolvalue cal = delegate(string s) { richtextbox1.appendtext(s); };
        this.invoke(cal, text);
      }
      else
      {
        richtextbox1.appendtext(text);
      }
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。