iText Stamping - Java [英] iText Stamping - Java

查看:150
本文介绍了iText Stamping - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在不使数字签名无效的情况下盖章PDF文件。

I am having trouble with stamping PDF documents without invalidating digital signatures.

目前,我成功标记了PDF。但是,如果文档先前已签名,则签名不再有效。我理解为什么会发生这种情况,但如果我使用 Acrobat 添加文本或使用注释标记它,则签名有效。

Current, I succeeded stamping a PDF. However, if the document is previously signed the signature is no longer valid. I understand why that happens, but if I use Acrobat to add text or stamp it using annotation, the signature is valid.

我尝试添加注释或注释但它仍然使签名无效。有没有办法使用 iText 为PDF添加图章而不会使数字签名无效?

I tried adding annotations or comments but it still invalidates the signature. Is there a way to add stamp to a PDF using iText without invalidating digital signatures?

这是我用来盖章的代码片段:

Here is a snippet of the code i use to stamp:

        PdfReader reader = new PdfReader(inputstream);

        stamp = new PdfStamper(reader, new FileOutputStream(file));


        PdfContentByte pcb;
        BaseFont bf = BaseFont.createFont("Courier", BaseFont.CP1250,BaseFont.EMBEDDED);

        Rectangle r = reader.getPageSizeWithRotation(1);

        pcb = stamp.getOverContent(1);

        // set the font and size
        float size = 12;
        pcb.setFontAndSize(bf, size);

        float width = 90;
        float centerX = 0, startY = 0;
        centerX = r.getWidth() - (width / 2) - 20;
        startY = r.getHeight() - (15 * 2) - 145;

        pcb.beginText();
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, stampText, centerX, startY, 0);

        pcb.setFontAndSize(bf, 10);
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, date, centerX-9, startY-8, 0);
        pcb.endText();
        stamp.close();          

我们非常感谢任何帮助,
谢谢

Any help will be much appreciated, Thanks

推荐答案

我实现了':)

以下是用于向文档添加自定义文本的代码 iText ,不会使数字签名失效。

Here is the code used to add custom text to document using iText without invalidating digital signatures.

//Read the source PDF
PdfReader reader = new PdfReader(inputstream);
//Create PdfStamp object
stamp = new PdfStamper(reader, new FileOutputStream(file), '\0', true);

//Create the proper annotation
PdfAnnotation annot = PdfAnnotation.createFreeText(stamp.getWriter(),  new Rectangle(150, 150, 200, 200), "Annotation 1", pcb);
annot.setFlags(PdfAnnotation.FLAGS_PRINT);

//Insert the annotation         
stamp.addAnnotation(annot, 1);
//Close the stamp
stamp.close();  

编辑:

为了在不使数字签名无效的情况下将图像标记插入到文档中,我使用了以下代码:

For inserting image stamp to the document without invalidating the digital signatures I used this code:

//Read the pdf 
PdfReader reader = new PdfReader(inputstream);
//Use PdfStamper in append mode
stamp = new PdfStamper(reader, new FileOutputStream(file), '\0', true); 

//Read the image
Image img = Image.getInstance(ImageIO.read(imgStream), null);

float w = img.getScaledWidth();
float h = img.getScaledHeight();
Rectangle location = new Rectangle(70, 770 - h, 70 + w, 770);

//Create stamp annotation           
PdfAnnotation stampAnnot = PdfAnnotation.createStamp(stamp.getWriter(), location, null, "ITEXT");
img.setAbsolutePosition(0, 0);
//Create new PdfContentByte from the stamp writer
//If you use cd = stamp.getOverContent(1) - you'll invalidate the signatures
PdfContentByte cb = new PdfContentByte(stamp.getWriter());
PdfAppearance app = cb.createAppearance(w, h);
app.addImage(img);
stampAnnot.setAppearance(PdfName.N, app);
stampAnnot.setFlags(PdfAnnotation.FLAGS_PRINT);

stamp.addAnnotation(stampAnnot, 1);
reader.close();

这篇关于iText Stamping - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆