C#利用iTextSharp添加PDF水印
更新時(shí)間:2017年03月24日 11:37:57 作者:IT-民工
這篇文章主要為大家詳細(xì)介紹了C#利用iTextSharp添加PDF水印的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了使用的是iTextSharp添加PDF水印的具體代碼,供大家參考,具體內(nèi)容如下
需要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); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#線性漸變畫刷LinearGradientBrush用法實(shí)例
這篇文章主要介紹了C#線性漸變畫刷LinearGradientBrush用法,實(shí)例分析了線性漸變畫刷LinearGradientBrush的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06C#使用Winform連接SQL Server數(shù)據(jù)庫的詳細(xì)步驟
Windows Forms是一個(gè)用于構(gòu)建Windows桌面應(yīng)用程序的圖形用戶界面(GUI)庫,它是.NET Framework的一部分,允許開發(fā)者快速創(chuàng)建豐富的交互式界面,本文給大家介紹了C#使用Winforms連接SQL Server數(shù)據(jù)庫的詳細(xì)步驟,需要的朋友可以參考下2024-09-09C#實(shí)現(xiàn)兩個(gè)時(shí)間相減的方法
這篇文章主要介紹了C#實(shí)現(xiàn)兩個(gè)時(shí)間相減的方法,實(shí)例分析了C#針對時(shí)間操作的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01C# WPF實(shí)現(xiàn)讀寫CAN數(shù)據(jù)
這篇文章主要介紹了C# WPF實(shí)現(xiàn)讀寫CAN數(shù)據(jù),文中通過代碼示例給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06