C# PDF文件加盖多个骑缝章

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations.Appearance;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WinFormsConfir
{
    public partial class fmConfir : Form
    {
        public fmConfir()
        {
            InitializeComponent();
        }

        private void btnUplaod_Click(object sender, EventArgs e)
        {
            if (opfdFileUrl.ShowDialog() == DialogResult.OK)
            {
                //多个印章处理
                int num = opfdFileUrl.FileNames.Length;
                string fileName = "";
                for (int i = 0; i < num; i++)
                {
                    fileName += opfdFileUrl.FileNames[i].ToString().Trim() + ";";
                }
                fileName = fileName.Substring(0, fileName.Length - 1);
                if (fileName.IndexOf("png") > 0)
                {
                    this.txtPngUrl.Text = fileName;
                }


            }
        }

        private void btnUploadPdf_Click(object sender, EventArgs e)
        {
            if (opfdFileUrl.ShowDialog() == DialogResult.OK)
            {
                //判断合同是否为pdf文件
                string fileName = opfdFileUrl.FileName.ToString().Trim();
                if (fileName.IndexOf("pdf") > 0)
                {
                    this.txtFileUrl.Text = fileName;
                }
            }
        }

        private void btmAdd_Click(object sender, EventArgs e)
        {
            string fileName = this.txtFileUrl.Text.Trim();
            string pngName = this.txtPngUrl.Text.Trim();

            if (!fileName.Equals(null) && fileName != "" && !pngName.Equals(null) && pngName != "")
            {
                if (PrintPdfFile(fileName, pngName))
                {
                    MessageBox.Show("加盖骑缝章成功!");
                }
                else
                {
                    MessageBox.Show("失败");
                }
            }
            else {
                MessageBox.Show("文件或印章路径不能为空!");
            }
        }
        private static bool PrintPdfFile(string fileUrl, string pngUrl)
        {
            //加载PDF测试文档  
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(fileUrl);
            string[] pngName = pngUrl.Split(";");
            int heightOld = -140;
            int weighOld = 120;
            for (int j = 0; j < pngName.Length; j++)
            {
                //获取分割后的印章图片  
                Image[] images = GetImage(doc.Pages.Count, pngName[j]);
                float x = 0;
                float y = 0;

                PdfUnitConvertor convert = new PdfUnitConvertor();
                PdfPageBase pageBase = null;
                //将图片绘制到PDF页面上的指定位置  
                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    pageBase = doc.Pages[i];
                    x = pageBase.Size.Width - convert.ConvertToPixels(images[i].Width, PdfGraphicsUnit.Point); //- 40;
                    y = pageBase.Size.Height / 2 + heightOld;
                    pageBase.Canvas.SetTransparency(0.8f, 0.8f, PdfBlendMode.Normal); //设置背景透明
                    pageBase.Canvas.DrawImage(PdfImage.FromImage(images[i]), new PointF(x, y));//,new SizeF()

                    if (i == doc.Pages.Count-1) {
                        pageBase.Canvas.SetTransparency(0.8f, 0.8f, PdfBlendMode.Normal); //设置背景透明
                        pageBase.Canvas.DrawImage(PdfImage.FromImage(GetImage(pngName[j])), new PointF(weighOld, pageBase.Size.Height / 2));//,new SizeF()
                        weighOld = weighOld + 200;
                    }
                }
                heightOld = heightOld + 140;
                
            }

            //保存文档  
            //doc.SaveToFile("Result.pdf");
            //System.Diagnostics.Process.Start("Result.pdf");
            string output = @fileUrl;//添加图片后的新pdf文件的路径
            //save pdf file
            doc.SaveToFile(output);
            doc.Close();
            return true;
        }
        //定义GetImage方法,根据PDF页数分割印章图片
        static Image[] GetImage(int num, string pngUrl)
        {
            List<Image> lists = new List<Image>();
            Image image = GetImage(pngUrl);//Image.FromFile(pngUrl);
            int w = image.Width / num;
            Bitmap bitmap = null;
            for (int i = 0; i < num; i++)
            {
                bitmap = new Bitmap(w, image.Height);
                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
                {
                   
                    g.Clear(Color.Transparent);
                   
                    Rectangle rect = new Rectangle(i * w, 0, w, image.Height);
                    g.SmoothingMode = SmoothingMode.AntiAlias;
                    g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), rect, GraphicsUnit.Pixel);
                }
                bitmap.MakeTransparent(System.Drawing.Color.Transparent);//设置背景透明
                string pngName = @"D:\Pictures\Camera Roll\" + DateTime.Now.ToString("yyyyMMddHHmmssfff")+ ".png";
                bitmap.Save(pngName, ImageFormat.Png);
                Image imageNewSave = Image.FromFile(pngName);
                lists.Add(imageNewSave);
            }
            return lists.ToArray();
        }
        //定义GetImage方法,根据PDF页数分割印章图片
        static Image GetImage(string pngUrl)
        {
            Image image = Image.FromFile(pngUrl);

            Bitmap bitmapProxy = new Bitmap(image, new Size(230,230));
            image.Dispose();
            for (int i = 0; i < bitmapProxy.Width; i++)
            {
                for (int j = 0; j < bitmapProxy.Height; j++)
                {
                    System.Drawing.Color c = bitmapProxy.GetPixel(i, j);
                    if (!(c.R < 240 || c.G < 240 || c.B < 240))
                    {
                        bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent);//设置背景透明
                    }
                }
            }
            //bitmapProxy.MakeTransparent(System.Drawing.Color.Transparent);//设置背景透明
            //图片另算为
            string pngName = @"D:\Pictures\Camera Roll\"+DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
           
            bitmapProxy.Save(pngName, ImageFormat.Png);
            Image imageNewSave = Image.FromFile(pngName);
            
            
            return imageNewSave;
        }
    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值