惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - IT-民工

Sqlsever Kill locked process Linked Server Dependencies Excel Vlookup多条件查询 , 列转行 C# 编写ActiveX Microsoft.Office.Interop.Excel error: 80070005 C# Linq 分页查询模板 SQLServer 查询 Excel ISO 纸张尺寸定义 检测SQLSERVER 连接 刷新SQLserver 视图 Sql Server 邮件日志 操作 常用连接 导出Excel Asp.net 多国语言-注意点 SqlServer 行列互转 SqlServer 备份数据库语法 SqlServer To SqlServer 建立 链接服务器 Linked Server sqlserver 根据内容,查询表和列名字 Sqlserver 2005 修改数据库默认排序
C# PDF添加水印
IT-民工 · 2013-08-19 · via 博客园 - IT-民工

需要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);
            }
        }