2009-10-23 35 views
2

我使用BIRT報告庫創建pdf文件。後來我需要對這些文件進行數字簽名。我正在使用iText對文檔進行數字簽名。使用iText的pdf文件中元素的座標

我面臨的問題是,我需要將簽名放在不同報告中的不同位置。我已經有了數字簽名文件的代碼,現在我總是將簽名放在每個報告最後一頁的底部。

最終我需要每個報告來說明我需要放置簽名的位置。然後,我必須使用iText讀取位置,然後將簽名放在該位置。

這可能使用BIRT和iText的

感謝

+0

這請求聽起來不太漂亮。你想要做什麼呢? –

+0

爲什麼不使用簽名表單字段來指定簽名位置? – JonMR

+0

pdf文件是使用BIRT報告工具創建的。我怎樣才能做到這一點? –

回答

3

如果你願意作弊,你可以使用一個鏈接...... BIRT支持的鏈接,根據我現在對我的小文檔的深入研究。

鏈接是註釋。不幸的是,iText不支持在高級別檢查註釋,只生成註釋,因此您必須使用低級對象調用。

的代碼以提取它可能是這個樣子:

// getPageN is looking for a page number, not a page index 
PdfDictionary lastPageDict = myReader.getPageN(myReader.getNumberOfPages()); 

PdfArray annotations = lastPageDict.getAsArray(PdfName.ANNOTS); 
PdfArray linkRect = null; 
if (annotations != null) { 
    int numAnnots = annotations.size(); 
    for (int i = 0; i < numAnnots; ++i) { 
    PdfDictionary annotDict = annotations.getAsDict(i); 
    if (annotDict == null) 
     continue; // it'll never happen, unless you're dealing with a Really Messed Up PDF. 

    if (PdfName.LINK.equals(annotDict.getAsName(PdfName.SUBTYPE))) { 
     // if this isn't the only link on the last page, you'll have to check the URL, which 
     // is a tad more work. 
     linkRect = annotDict.getAsArray(PdfName.RECT); 

     // a little sanity check here wouldn't hurt, but I have yet to come across a PDF 
     // that was THAT screwed up, and I've seen some Really Messed Up PDFs over the years. 

     // and kill the link, it's just there for a placeholder anyway. 
     // iText doesn't maintain any extra info on links, so no need for other calls. 
     annotations.remove(i); 
     break; 
    } 
    } 
} 

if (linkRect != null) { 
    // linkRect is an array, thusly: [ llx, lly, urx, ury ]. 
    // you could use floats instead, but I wouldn't go with integers. 
    double llx = linkRect.getAsNumber(0).getDoubleValue(); 
    double lly = linkRect.getAsNumber(1).getDoubleValue(); 
    double urx = linkRect.getAsNumber(2).getDoubleValue(); 
    double ury = linkRect.getAsNumber(3).getDoubleValue(); 

    // make your signature 
    magic(); 
} 

如果BIRT生成頁面內容的一些文字鏈接下的可視化表示,這只是一個小問題。你的簽名應該完全覆蓋它。

如果您可以直接從BIRT生成簽名,那麼您絕對會更好,但是我對他們的文檔的一點檢查並不完全滿足我對他們的PDF定製能力的信心......儘管坐在iText自己的頂部。這是一個報告生成器,恰好能夠生成PDF ...我不應該期望太多。 `

編輯:如果您需要尋找特定的網址,你會想看看部分PDF參考的「12.5.6.5鏈接註釋」,它可以在這裏找到: http://www.adobe.com/content/dam/Adobe/en/devnet/pdf/pdfs/PDF32000_2008.pdf

1

我不知道什麼BIRT,並且只有與iText的一點點的熟悉來實現。但也許這工作...

BIRT可以生成簽名框的輪廓作爲一個具有給定字段名稱的常規表單字段嗎?如果是這樣,那麼你應該能夠:

  1. 在iText的AcroFields哈希映射中使用getField查找該字段的名稱;
  2. 使用pdf壓模創建一個新簽名,並根據舊字段對象的值設置其幾何圖形;和
  3. 使用removeField刪除舊字段。
+0

感謝您的建議,讓我檢查BIRT是否可以將表單字段添加到PDF中。 –