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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM