2013-06-27 102 views
2

我想使用JAVA填寫PDF表單,但是當我嘗試使用下面的代碼獲取字段時,列表爲空。無法使用PDFBOX讀取PDF文件

PDDocument pdDoc = PDDocument.load(filename); 
PDAcroForm pdform = pdDoc.getDocumentCatalog().getAcroForm(); 
List<PDField> field = pdform.getFields(); 

然後我試圖讀取使用PDFStripper

PDFTextStripper stripper = new PDFTextStripper(); 
System.out.println(stripper.getText(pdDoc)); 

文件和輸出中是如下

「請稍候...

如果該消息沒有最終取代通過文檔的正確內容,您的PDF 查看器可能無法顯示此類文檔。

您可以通過 訪問http://www.adobe.com/go/reader_download將Windows®,Mac或Linux®的Adobe Reader升級到最新版本。

如需Adobe Reader的更多幫助,訪問http://www.adobe.com/go/acrreader

Windows是微軟公司在美國和/或其他國家的註冊商標或商標。 Mac是Apple Inc.在美國和其他國家註冊的商標 。 Linux是Linus Torvalds在美國和其他 國家的註冊商標。「

但是我可以手動打開文件並填寫字段,我也嘗試過其他工具,例如iText。我沒能拿到領域。 我怎樣才能解決這個問題呢?

回答

0

可能爲時已晚來回答,但無論如何,爲什麼不。你可以空單,如果你的PDF文件具有XFA結構。

PDDocument pdDoc = PDDocument.load(filename); 
PDAcroForm pdform = pdDoc.getDocumentCatalog().getAcroForm(); 
List<PDField> field = pdform.getFields(); 

使用這些代碼行開始使用pdf:

PDXFA xfa = pdform.getXFA(); 
Document xfaDocument = xfa.getDocument(); 
NodeList elements = xfaDocument.getElementsByTagName("SomeElement"); 
+0

如何檢查是否屬於這種情況? – kutschkem

+0

使用下面的if語句:if(pdform.getFields()。isEmpty){// XFA case} else {// AcroForm case} – Paul