winform界面
1.选择加签pdf按钮代码实现
private void button1_Click(object sender, EventArgs e){OpenFileDialog op = new OpenFileDialog();op.Filter = "PDF文件(*.pdf)|*.pdf";bool flag = op.ShowDialog() == DialogResult.OK;if (flag){string pdfPath = Path.GetFullPath(op.FileName);fileName = op.SafeFileName;label2.Text = pdfPath;}}
每页加签按钮代码实现
private void button2_Click(object sender, EventArgs e){string path = Application.StartupPath;SignEachPage(label2.Text.ToString(), path + "/PDF/" + fileName, path + @"\InspectionSeal\电子章.png"); // 参数(原pdf地址,加签后pdf地址,电子章地址)Process.Start("explorer", Application.StartupPath + "\\PDF");label4.Text = path + "/PDF/" + fileName;}
SignEachPage方法代码实现
public static void SignEachPage(string pdfPath, string outPdfPath, string imagePath){//读取pdfiTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);//读取页数int pdfPageSize = pdfReader.NumberOfPages;//创建新pdfSystem.IO.FileStream outputStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputStream);//文件内容iTextSharp.text.pdf.PdfContentByte waterMarkContent;//读取第一页尺寸iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);//读取盖章图片iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);//图片缩放到一定百分比image.ScalePercent(60);//设置图片位置,位置偏移方向为:左到右,下到上image.SetAbsolutePosition(psize.Width / 10 * 7, psize.Height / 10);//循环给每页盖章for (int i = 1; i <= pdfPageSize; i++){//GetUnderContent 加在内容下层//GetOverContent 加在内容上层waterMarkContent = pdfStamper.GetUnderContent(i);//添加waterMarkContent.AddImage(image);}pdfStamper.Close();pdfReader.Close();//直接打开盖章后文件// System.Diagnostics.Process.Start(outPdfPath);}