PDF盖骑缝章

对于无纸化操作pdf文件如果需要打印,一般为了保证完整有效性用数字签名就可以了,但是需要打印纸质后数字签名就呵呵了,对于人眼的完整行只能靠骑缝章来简单的保证一下了。所以骑缝章完全事为了人类肉眼识别出来的一个东西。对于要打印的pdf加一个骑缝章,无非就是把章按页数切割成等份的图片,合并在一起罢了。

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * 盖骑缝章
 * Created by zhangzhenhua on 2016/11/2.
 */
public class PDFStamperCheckMark {

    /**
     * 切割图片
     * @param imgPath  原始图片路径
     * @param n 切割份数
     * @return  itextPdf的Image[]
     * @throws IOException
     * @throws BadElementException
     */
    public static Image[] subImages(String imgPath,int n) throws IOException, BadElementException {
        Image[] nImage = new Image[n];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BufferedImage img = ImageIO.read(new File(imgPath));
        int h = img.getHeight();
        int w = img.getWidth();

        int sw = w/n;
        for(int i=0;i<n;i++){
            BufferedImage subImg;
            if(i==n-1){//最后剩余部分
                 subImg = img.getSubimage(i * sw, 0, w-i*sw, h);
            }else {//前n-1块均匀切
                 subImg = img.getSubimage(i * sw, 0, sw, h);
            }

            ImageIO.write(subImg,imgPath.substring(imgPath.lastIndexOf('.')+1),out);
            nImage[i] = Image.getInstance(out.toByteArray());
            out.flush();
            out.reset();
        }
        return nImage;
    }

    /**
     *  盖骑缝章
     *
     * @param infilePath    原PDF路径
     * @param outFilePath    输出PDF路径
     * @param picPath    章图片路径
     * @throws IOException
     * @throws DocumentException
     */
    public static void stamperCheckMarkPDF(String infilePath,String outFilePath,String picPath) throws IOException, DocumentException {
        PdfReader reader = new PdfReader(infilePath);//选择需要印章的pdf
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//加完印章后的pdf

        Rectangle pageSize = reader.getPageSize(1);//获得第一页
        float height = pageSize.getHeight();
        float width  = pageSize.getWidth();

        int nums = reader.getNumberOfPages();
        Image[] nImage =  subImages(picPath,nums);//生成骑缝章切割图片


        for(int n=1;n<=nums;n++){
            PdfContentByte over = stamp.getOverContent(n);//设置在第几页打印印章
            Image img = nImage[n-1];//选择图片
//            img.setAlignment(1);
//            img.scaleAbsolute(200,200);//控制图片大小
            img.setAbsolutePosition(width-img.getWidth(),height/2-img.getHeight()/2);//控制图片位置
            over.addImage(img);
        }
        stamp.close();
    }



    public static void main(String[] args) throws IOException, DocumentException {
        String infilePath = "E:\\项目\\paperless\\page.pdf";
        String outfilePaht = "E:\\项目\\paperless\\page_pic.pdf";
        String picPath = "E:\\项目\\paperless\\公章.png";
        stamperCheckMarkPDF(infilePath,outfilePaht,picPath);
    }
}

最后效果
这里写图片描述

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
Python实现PDF骑缝章可以使用PyPDF2库来实现。具体步骤如下: 1. 首先,安装PyPDF2库。可以使用pip命令来安装,命令如下: ``` pip install PyPDF2 ``` 2. 导入PyPDF2库,在Python脚本中使用以下代码: ```python import PyPDF2 ``` 3. 加载PDF文件。使用`PyPDF2.PdfFileReader()`函数加载待章的PDF文件,如下所示: ```python input_pdf = PyPDF2.PdfFileReader(open('input.pdf', 'rb')) ``` 4. 加载骑缝章PDF文件。同样使用`PyPDF2.PdfFileReader()`函数加载骑缝章PDF文件,如下所示: ```python stamp_pdf = PyPDF2.PdfFileReader(open('stamp.pdf', 'rb')) ``` 5. 获取待PDF文件的第一页。使用`input_pdf.getPage()`函数获取PDF文件的第一页,如下所示: ```python input_page = input_pdf.getPage(0) ``` 6. 获取骑缝章PDF文件的第一页。使用`stamp_pdf.getPage()`函数获取骑缝章PDF文件的第一页,如下所示: ```python stamp_page = stamp_pdf.getPage(0) ``` 7. 将骑缝章应用于待PDF文件的第一页。使用`input_page.mergePage()`函数将骑缝章应用于待PDF文件的第一页,如下所示: ```python input_page.mergePage(stamp_page) ``` 8. 创建输出PDF文件。使用`PyPDF2.PdfFileWriter()`函数创建一个输出PDF文件,如下所示: ```python output_pdf = PyPDF2.PdfFileWriter() ``` 9. 将合并后的页添加到输出PDF文件中。使用`output_pdf.addPage()`函数将合并后的页添加到输出PDF文件中,如下所示: ```python output_pdf.addPage(input_page) ``` 10. 保存输出PDF文件。使用`output_pdf.write()`函数保存输出PDF文件,如下所示: ```python with open('output.pdf', 'wb') as f: output_pdf.write(f) ``` 完成以上步骤后,即可实现Python骑缝章的功能,并将结果保存到输出的PDF文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值