C#利用iTextSharp添加PDF水印
本文实例为大家分享了使用的是iTextSharp添加PDF水印的具体代码,供大家参考,具体内容如下
需要iTextSharp.dll,下载地址http://sourceforge.net/projects/itextsharp/
publicvoidTest()
{
Watermark(@"E:\日常工作\12084347config.pdf",@"E:\日常工作\12084347configwm.pdf",@"E:\日常工作\wm.png");
}
publicboolAddWatermark(stringinputPath,stringoutputPath,stringwatermarkPath,refstringerror)
{
try
{
PdfReaderpdfReader=newPdfReader(inputPath);
intnumberOfPages=pdfReader.NumberOfPages;
FileStreamoutputStream=newFileStream(outputPath,FileMode.Create);
PdfStamperpdfStamper=newPdfStamper(pdfReader,outputStream);
PdfContentBytewaterMarkContent;
iTextSharp.text.Imageimage=null;
if(string.IsNullOrEmpty(watermarkPath))
{
Streams=GetType().Assembly.GetManifestResourceStream("WatermarkTool.wm.png");
image=iTextSharp.text.Image.GetInstance(s);
}
else
{
image=iTextSharp.text.Image.GetInstance(watermarkPath);
}
image.SetAbsolutePosition(100,100);
for(inti=1;i<=numberOfPages;i++)
{
waterMarkContent=pdfStamper.GetUnderContent(i);
waterMarkContent.AddImage(image);
}
pdfStamper.Close();
pdfReader.Close();
outputStream.Close();
returntrue;
}
catch(Exceptionex)
{
error=ex.StackTrace;
returnfalse;
}
}
//选择文件夹
privatevoidtextBox2_DoubleClick(objectsender,EventArgse)
{
FolderBrowserDialogdialog=newFolderBrowserDialog();
dialog.Description=label2.Text;
if(dialog.ShowDialog()==DialogResult.OK)
{
textBox2.Text=dialog.SelectedPath;
}
}
//选择文件
privatevoidtextBox3_DoubleClick(objectsender,EventArgse)
{
OpenFileDialogfileDialog=newOpenFileDialog();
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;
}
}
//启动线程
privatevoidbutton1_Click(objectsender,EventArgse)
{
if(Directory.Exists(textBox1.Text)==false)
{
MessageBox.Show(label1.Text,"Requireinput",MessageBoxButtons.OK,MessageBoxIcon.Warning);
textBox1.Focus();
return;
}
if(Directory.Exists(textBox2.Text)==false)
{
MessageBox.Show(label2.Text,"Requireinput",MessageBoxButtons.OK,MessageBoxIcon.Warning);
textBox2.Focus();
return;
}
if(textBox3.Enabled&&File.Exists(textBox3.Text)==false)
{
MessageBox.Show(label3.Text,"Requireinput",MessageBoxButtons.OK,MessageBoxIcon.Warning);
textBox3.Focus();
return;
}
richTextBox1.Clear();
button1.Enabled=false;
Threadthread=newThread(newThreadStart(this.BatchDo));
thread.IsBackground=true;
thread.Start();
}
publicdelegatevoidSetControlValue(stringmessage);
//在线程中修改控件属性
publicvoidAppendRTBText(stringtext)
{
if(richTextBox1.InvokeRequired)
{
SetControlValuecal=delegate(strings){richTextBox1.AppendText(s);};
this.Invoke(cal,text);
}
else
{
richTextBox1.AppendText(text);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。