这篇文章主要为大家详细介绍了C#利用iTextSharp添加PDF水印的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了使用的是iTextSharp添加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);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:C#利用iTextSharp添加PDF水印
基础教程推荐
猜你喜欢
- C# 调用WebService的方法 2023-03-09
- C# List实现行转列的通用方案 2022-11-02
- C# windows语音识别与朗读实例 2023-04-27
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- 一个读写csv文件的C#类 2022-11-06
- C#控制台实现飞行棋小游戏 2023-04-22
- unity实现动态排行榜 2023-04-27
- ZooKeeper的安装及部署教程 2023-01-22
- C#类和结构详解 2023-05-30
- winform把Office转成PDF文件 2023-06-14