• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

C# PDF加盖电子章

武飞扬头像
tiegenZ
帮助1

winform界面

学新通

 1.选择加签pdf按钮代码实现

  1.  
    private void button1_Click(object sender, EventArgs e)
  2.  
    {
  3.  
    OpenFileDialog op = new OpenFileDialog();
  4.  
    op.Filter = "PDF文件(*.pdf)|*.pdf";
  5.  
    bool flag = op.ShowDialog() == DialogResult.OK;
  6.  
    if (flag)
  7.  
    {
  8.  
    string pdfPath = Path.GetFullPath(op.FileName);
  9.  
    fileName = op.SafeFileName;
  10.  
    label2.Text = pdfPath;
  11.  
    }
  12.  
    }

每页加签按钮代码实现

  1.  
    private void button2_Click(object sender, EventArgs e)
  2.  
    {
  3.  
    string path = Application.StartupPath;
  4.  
    SignEachPage(label2.Text.ToString(), path "/PDF/" fileName, path @"\InspectionSeal\电子章.png"); // 参数(原pdf地址,加签后pdf地址,电子章地址)
  5.  
    Process.Start("explorer", Application.StartupPath "\\PDF");
  6.  
    label4.Text = path "/PDF/" fileName;
  7.  
    }

SignEachPage方法代码实现

  1.  
    public static void SignEachPage(string pdfPath, string outPdfPath, string imagePath)
  2.  
    {
  3.  
    //读取pdf
  4.  
    iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfPath);
  5.  
    //读取页数
  6.  
    int pdfPageSize = pdfReader.NumberOfPages;
  7.  
    //创建新pdf
  8.  
    System.IO.FileStream outputStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
  9.  
    iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, outputStream);
  10.  
    //文件内容
  11.  
    iTextSharp.text.pdf.PdfContentByte waterMarkContent;
  12.  
    //读取第一页尺寸
  13.  
    iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
  14.  
    //读取盖章图片
  15.  
    iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagePath);
  16.  
    //图片缩放到一定百分比
  17.  
    image.ScalePercent(60);
  18.  
    //设置图片位置,位置偏移方向为:左到右,下到上
  19.  
    image.SetAbsolutePosition(psize.Width / 10 * 7, psize.Height / 10);
  20.  
    //循环给每页盖章
  21.  
    for (int i = 1; i <= pdfPageSize; i )
  22.  
    {
  23.  
    //GetUnderContent 加在内容下层
  24.  
    //GetOverContent 加在内容上层
  25.  
    waterMarkContent = pdfStamper.GetUnderContent(i);
  26.  
    //添加
  27.  
    waterMarkContent.AddImage(image);
  28.  
    }
  29.  
    pdfStamper.Close();
  30.  
    pdfReader.Close();
  31.  
    //直接打开盖章后文件
  32.  
    // System.Diagnostics.Process.Start(outPdfPath);
  33.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiabbbh
系列文章
更多 icon
同类精品
更多 icon
继续加载