如何在iText7中的签名pdf之后添加分页印章?

时间:2019-04-27 12:04:47

标签: java itext7

我想在pdf文件中添加分页印章,所以我只分割了一张完整的图像,并在每页中添加了每一幅,最后进行了签名。我看起来很好。

但是现在我在pdf之前有一个签名,我想第二次使用分页封条对其进行签名,显然我失败了。根据Adobe Acrobat Reader DC的显示,当我修改签名文件时。

那么我该如何实现呢?

 protected boolean doPagingSealStamp(byte[] doc, byte[] sealImg, SignOption option, String outPath) {
        this.originDoc = doc
        this.signOption = option

        //generate a temp file to add paging seal
        File tempFile = new File("temp.pdf")

        PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(originDoc))
        PdfWriter pdfWriter = new PdfWriter(tempFile)

        //use append mode
        PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter , new StampingProperties().useAppendMode())
        Document document = new Document(pdfDocument)

        int totalPages = pdfDocument.getNumberOfPages()

        float pageWidth = pdfDocument.getPage(1).getPageSize().getWidth()
        float pageHeight = pdfDocument.getPage(1).getPageSize().getHeight()

        Image[] images = getSubImages(sealImg, totalPages)

        int index = 0

        //add paging seal in every page
        for (int i = 1; i <= totalPages; i++) {
            images[index].setFixedPosition(i, (pageWidth - images[index].getImageWidth()) as float,
                    (pageHeight / 2 - images[index].getImageHeight()) as float)
            document.add(images[index])
            index++
        }

        document.close()
        pdfDocument.close()


        //close the document and do the sign operation
        PdfSigner pdfSigner = new PdfSigner(new PdfReader(tempFile), new FileOutputStream(outPath), signOption.append)

        return doSign(pdfSigner)
    }

//split the image, and return the image array
 private Image[] getSubImages(byte[] sealImg, int count) {


        BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(sealImg))
        int imageWidth = bufferedImage.getWidth()
        int imageHeight = bufferedImage.getHeight()

        int perImageWidth = (imageWidth / count) as int
        int x = 0
        int y = 0

        ByteArrayOutputStream baos = new ByteArrayOutputStream()
        Image[] images = new Image[count]
        Image image
        BufferedImage subImage

        for (int i = 0; i < count; i++) {
            subImage = bufferedImage.getSubimage(x, y, perImageWidth as int, imageHeight as int)
            x += perImageWidth
            ImageIO.write(subImage, "png", baos)
            image = new Image(ImageDataFactory.create(baos.toByteArray()))
            images[i] = image
            baos.flush()
            baos.reset()
        }

        return images
    }


//do the operation of signature
private boolean doSign(PdfSigner pdfSigner) {

        boolean isSuccess = false

        while (!isSuccess) {
            try {

                P7Sha1Container container = new P7Sha1Container(api)
                pdfSigner.signExternalContainer(container, mEstimatedSize)
                isSuccess = true
            } catch (IOException e) {

                mEstimatedSize += 1000
            }
        }

        return isSuccess
    }

```java

0 个答案:

没有答案