1、引用一个dll--itextsharp.dll;
2、复制一个font包--fonts;
3、添加using引用--using iTextSharp.text;
using iTextSharp.text.pdf;
4、代码如下
private void PDFStamp(string inputPath, string outputPath, string watermarkPath)
{
PdfReader pdfReader = new PdfReader(inputPath);
int numberOfPages = pdfReader.NumberOfPages;
FileStream outputStream = new FileStream(outputPath, FileMode.Create);
PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
PdfContentByte waterMarkContent;
string watermarkimagepath = watermarkPath;
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(watermarkimagepath);
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
//设置水印的位置,大小
//设置水印透明度,如果是png的透明图片则不需要再设置透明了
image.SetAbsolutePosition(0, 0);
image.ScaleAbsoluteHeight(width);
image.ScaleAbsoluteWidth(height);
//image.Transparency = new int[] { 0xF0, 0xFF };
for (int i = 1; i <= numberOfPages; i++)
{
// waterMarkContent = pdfStamper.GetUnderContent(i);//设置水印在页面之下
waterMarkContent = pdfStamper.GetOverContent(i);//设置水印在页面之上
waterMarkContent.AddImage(image);
}
pdfStamper.Close();
pdfReader.Close();
}
private void dayin()
{
string name;
int a = 0;
int index;
//index = superTabControl1.SelectedTabIndex;
//name = superTabControl1.Tabs[index].Text;
name = "";// panelEx1.Text;
//for (int i = 0; i < superTabControl1.Controls.Count; i++)
//{
// if (superTabControl1.Controls[i].Text == name)
// a = i;
// break;
//}
//DevComponents.DotNetBar.SuperTabControlPanel panel = (DevComponents.DotNetBar.SuperTabControlPanel)superTabControl1.Controls[a];
//Panel panel = (Panel)superTabControl1.Controls[a];
Panel panel = panelEx1;
int x = panel.Width+ 10;
int y = panel.Height;
iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A4.Rotate());//打印pdf,step1
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(Application.StartupPath + "\\data\\tempfile\\" + name + ".pdf", FileMode.Create));//打印pdf,step2
doc.Open();//打印pdf,step3
//加入文字说明/
//载入字体
BaseFont baseFont1 = BaseFont.CreateFont(
AppDomain.CurrentDomain.BaseDirectory + "\\fonts\\SIMHEI.TTF", //黑体 //AppDomain.CurrentDomain.BaseDirectory获取程序的基目录
BaseFont.IDENTITY_H, //横向字体
BaseFont.NOT_EMBEDDED);
BaseFont baseFont2 = BaseFont.CreateFont(
AppDomain.CurrentDomain.BaseDirectory + "\\fonts\\simkai.ttf", //楷体 //AppDomain.CurrentDomain.BaseDirectory获取程序的基目录
BaseFont.IDENTITY_H, //横向字体
BaseFont.NOT_EMBEDDED);
BaseFont baseFont3 = BaseFont.CreateFont(
AppDomain.CurrentDomain.BaseDirectory + "\\fonts\\times.ttf", // //AppDomain.CurrentDomain.BaseDirectory获取程序的基目录
BaseFont.IDENTITY_H, //横向字体
BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font1 = new iTextSharp.text.Font(baseFont1, 28);
iTextSharp.text.Font font2 = new iTextSharp.text.Font(baseFont2, 16);
iTextSharp.text.Font font3 = new iTextSharp.text.Font(baseFont3, 10);
//写入一个段落, Paragraph
Paragraph paragraph1 = new Paragraph("海底管道完整性", font1);
paragraph1.Alignment = Element.ALIGN_CENTER;
doc.Add(paragraph1);
doc.Add(new Paragraph(name, font2));
Bitmap bitmap = new Bitmap(x, y);
Graphics graphic = panelEx1.CreateGraphics();
Size s = panelEx1.Size;
bitmap = new Bitmap(s.Width, s.Height, graphic);
Graphics g = Graphics.FromImage(bitmap);
g.Clear(System.Drawing.Color.White);
g.CopyFromScreen(panel.PointToScreen(new Point(0, 0)), new Point(0, 0), new Size(x, y));
//bitmap.Save(@"a.jpg");
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Jpeg);
jpg.ScalePercent(40, 40);
doc.Add(jpg); //add an image to the created pdf document
//在指定位置加入时间
PdfContentByte cb = wri.DirectContent;
cb.BeginText();
BaseFont bfont = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设定字体:黑体
cb.SetFontAndSize(baseFont3, 10);//设定字号
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, DateTime.Now.ToShortDateString(), 800, 30, 0);//后三个参数中前两个分别是x,y的位置,最后一个是角度
cb.EndText();
doc.Close();//打印pdf,step5
PDFStamp(Application.StartupPath + "\\data\\tempfile\\" + name + ".pdf", Application.StartupPath + "\\data\\tempfile\\" + name + "(水印).pdf", Application.StartupPath + "\\data\\1.png");//加水印。
Process.Start(Application.StartupPath + "\\data\\tempfile\\" + name + "(水印).pdf");
}