2015-05-14 62 views
3

我无法在不使数字签名失效的情况下盖印PDF文档。iText冲压 - Java

目前,我成功地加盖了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();   

任何帮助将非常感激,感谢

+1

正如在回答解释[如何添加空白页在数字签名pdf使用Java?](http://stackoverflow.com/questions/16710439/how-to-add-blank-page-in-digitally-signed-pdf-using-java)你不能添加内容到一个按照您的方式签署PDF。你声称你可以用Acrobat做到这一点,但事实并非如此。您可能会在注释中混淆文本,并将文本添加到内容流中。如果签名允许添加注释,则应该在* append mode *中使用'PdfStamper'添加文本注释。 –

+0

我只能通过注释中的文本通过Acrobat添加内容,我很抱歉误会。我成功添加了评论而不会使签名无效。但是我无法获得freeTextAnnotation的挂起,它仍然使签名无效。 我使用 'stamp = new PdfStamper(reader,new FileOutputStream(file),'\ 0',true); PdfContentByte pcb = new PdfContentByte(stamp.getWriter()); float size = 12; pcb.setFontAndSize(bf,size); PdfAnnotation annot2 = PdfAnnotation.createFreeText(stamp.getWriter(),new Rectangle(x,y,x1,y1),“A1”,pcb); stamp.addAnnotation(annot2,1)' –

+0

@请在问题中加上这样的代码说明。 – mkl

回答

3

我提出这一点” :)

这里是用于向文档添加自定义文本的代码,使用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(); 
+1

是的,现在你在追加模式下使用'PdfStamper'。您不会更改内容流;您正在使用注释。问题解决了;-) –

+0

@BrunoLowagie我很努力地把包含图像的邮票注释。如在** Acrobat **中选择邮票的PDF图像并将其作为注释插入。 现在我知道我很讨厌,但我无法在任何地方找到答案。 提前谢谢! –

+0

您是否尝试过[AddStamp](http://itextpdf.com/sandbox/annotations/AddStamp)示例?它添加了一张带有iText徽标图像的邮票。 –