C# PDF加盖电子章

winform界面

C# PDF加盖电子章_第1张图片

 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)
        {
            //读取pdf
            iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);
            //读取页数
            int pdfPageSize = pdfReader.NumberOfPages;
            //创建新pdf
            System.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);
        }

你可能感兴趣的:(C#,代码实现合集,C#,pdf文件加盖电子章,PDF文件加盖电子章,C#,加盖电子章)