C# 通过iTextSharp实现关键字签字盖章(通过在内容中插入盖章图片的形式)

此功能通过 iTextSharp 读取PDF文档信息,并循环查找每一页PDF文件,在整个PDF中只要是符合条件的地方都会盖章,如只需要在最后一页盖章,请将方法中For循环去掉,并将

PdfContentByte contentByte = pdfStamper.GetUnderContent(i);
parser.ProcessContent<PdfLocation>(i, location);

改为

// 获得文档页数
int pdfPageSize = pdfReader.NumberOfPages;
//文档最后一页,如需要第一页则直接将pdfPageSize改为1
PdfContentByte contentByte = pdfStamper.GetUnderContent(pdfPageSize);
//读取文档最后一页内容
parser.ProcessContent<PdfLocation>(pdfPageSize, location);

这几句改掉之后即可精确到第几页盖章

以下为具体代码实现:

1、具体盖章实现,引用 iTextSharp.text.pdf

     /// <summary>
    /// pdf签字签章,以图片形式
    /// </summary>
    public class PDFSealHelper
    {
   
        /// <summary>
        /// 图片最大宽度
        /// </summary>
        private static float ReSizeMaxWidth = 50;
        /// <summary>
        /// 图片最大高度
        /// </summary>
        private static float ReSizeMaxHeight = 50;
        /// <summary>
        /// 签字盖章(文件流和Base64格式图片)
        /// </summary>
        /// <param name="bytePdf">需要签字盖章byte数组的pdf文件</param>
        /// <param name="outPdfPath">签字盖章后输出pdf路径</param>
        /// <param name="SignImgBase64">Base64格式图片</param>
        /// <param name="SignKeyWord">关键字</param>
        public static void SignBase64Img(byte[] bytePdf, string outPdfPath, string SignImgBase64, string SignKeyWord)
        {
   
            System.IO.Stream outputPdfStream = new System.IO.FileStream(outPdfPath, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
            // 创建一个PdfReader对象
            PdfReader pdfReader = new PdfReader(bytePdf);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, outputPdfStream);
            {
   
                // 获得文档页数
                int pdfPageSize = pdfReader.NumberOfPages;
                for (int i = 1; i <= pdfPageSize; i++)
                {
   
                    //获取当前页
                    PdfContentByte contentByte = pdfStamper.GetUnderContent(i);
                    PdfLocation location = new PdfLocation();
                    iTextSharp.text.pdf.parser.PdfReaderContentParser parser = new iTextSharp.text.pdf.parser.PdfReaderContentParser(pdfReader);
                    parser.ProcessContent<PdfLocation>(i, location);
                    //查找当前页的关键字
                    location.SearchKeywords(SignKeyWord);
                    if (location.TextLocationInfo.Count > 0)
                    {
   
                        //坐标是从左下角往上,左下角为(0,0)零点
                        XTextInfo o = location.TextLocationInfo[0];
                        //获取关键字左上角开始坐标
                        var ltLocation = o.TopLeft.ToString().Split(',');//272.15,766.46,1
                        var leftX = float.Parse(ltLocation[0]);
                        var topY = float.Parse(ltLocation[1]);
                        //获取关键字右下角结束坐标
                        var rbLocation = o.BottomRight.ToString().Split(',');//305.15,755.46,1
                        var rightX = float.Parse(rbLocation[0]);
                        var bottomY = float.Parse(rbLocation[1]);
                        //计算得到关键字的中心点
                        float x = (rightX - leftX) / 2 + leftX;
                        float y = (topY - bottomY) / 2 + bottomY;
                        var imageByByte = ConvertBase64ToImage(SignImgBase64)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值