2015-05-14 30 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徽標圖像的郵票。 –