iText-Java

iText-Java,java,pdf,itext,digital-signature,invalidation,Java,Pdf,Itext,Digital Signature,Invalidation,我无法在不使数字签名失效的情况下为PDF文档加盖印花 目前,我成功地在PDF上加盖了印章。但是,如果文件先前已签名,则签名不再有效。我理解为什么会发生这种情况,但如果我使用Acrobat添加文本或使用注释标记,则签名是有效的 我尝试添加注释或注释,但仍然会使签名无效。有没有一种方法可以在不使数字签名无效的情况下使用iText向PDF添加戳记 下面是我用来盖章的代码片段: PdfReader reader = new PdfReader(inputstream);

我无法在不使数字签名失效的情况下为PDF文档加盖印花

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

我尝试添加注释或注释,但仍然会使签名无效。有没有一种方法可以在不使数字签名无效的情况下使用iText向PDF添加戳记

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

        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();          
任何帮助都将不胜感激, 谢谢

我做到了’:)

下面是使用iText将自定义文本添加到文档中而不使数字签名无效的代码

//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();  
编辑:

为了在文档中插入图像戳而不使数字签名无效,我使用了以下代码:

//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();

正如在对的回答中所解释的,您不能像您那样向已签名的PDF添加内容。你声称你可以用Acrobat做到这一点,但事实并非如此。您可能将注释中的文本与添加到内容流中的文本混淆。如果签名允许添加批注,则应在附加模式下使用
PdfStamper
添加文本批注。我只能通过批注中的文本通过Acrobat添加内容,对此产生的误解深表歉意。我成功地添加了注释而没有使签名无效。然而,我无法掌握freeTextAnnotation的诀窍,它仍然会使签名无效。我正在使用
stamp=newpdfstamper(读取器,newfileoutputstream(file),'\0',true);PdfContentByte pcb=新的PdfContentByte(stamp.getWriter());浮点数=12;pcb.setFontAndSize(bf,尺寸);PdfAnnotation annot2=PdfAnnotation.createFreeText(stamp.getWriter(),新矩形(x,y,x1,y1),“A1”,pcb);stamp.addAnnotation(annot2,1)
@请在问题中添加这样的代码说明。是的,现在您正在附加模式下使用
PdfStamper
。您没有更改内容流;您使用的是注释。问题已解决;-)@BrunoLowagie我正在努力为一张包含图像的邮票添加注释。在Acrobat中,选择邮票的PDF图像并将其作为批注插入。现在我知道我很烦人,但我在任何地方都找不到答案。提前谢谢你!你试过这个例子吗?它添加了一个带有iText徽标图像的图章。我必须进行实验,而且我只有有限的时间。你必须(1)希望其他人有时间,或者(2)找时间试验自己,或者(3)如果没有人能为你腾出时间,在iText使用付费支持。如果我能加倍投票,我会的。谢谢你更新你的答案。这将有助于进一步的参考。