zoukankan      html  css  js  c++  java
  • C# PDF添加水印

    需要iTextSharp.dll, 下载地址http://sourceforge.net/projects/itextsharp/

         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);
                }
            }
  • 相关阅读:
    【LintCode题集】Q539
    【LintCode题解】Q407
    【LintCode题集】Q6、Q64
    【Java安全】关于Java中常用加密/解密方法的实现
    【MySQL】MySQL5.7的安装与配置
    理解CSS3 max/min-content及fit-content等width值
    Django和MySQL数据库第一次连接时遇到的若干问题及解决办法
    使用Pycharm社区版启动Django的重要补充
    使用Pycharm社区版新建Python3.7的虚拟环境并安装启动Django的完整步骤
    04-图形化编辑器功能不全?
  • 原文地址:https://www.cnblogs.com/machaofast/p/3267237.html
Copyright © 2011-2022 走看看