2011-10-20 51 views
1

我必須填寫一個pdf表單(用於在線提交數據),它具有xfa字段並使用iText進行此操作。我能夠生成啓用讀取器的pdf文檔,但字段未填充。使用java中的itext填充xfa pdf表單

請建議我如何得到它的工作。

回答

1

所有你需要的是這樣的:

private void fillXmlInPdf(File xmlFile, File inputPdf, File outputPdf) throws IOException, DocumentException, FileNotFoundException, CsmartException { 
    PdfStamper stamper=null; 
    try { 
     PdfReader reader = new PdfReader(inputPdf.getAbsolutePath()); 
     stamper = new PdfStamper(reader, new FileOutputStream(outputPdf), '\0', true); 
     AcroFields afields = stamper.getAcroFields(); 
     XfaForm xfa = afields.getXfa(); 
     xfa.fillXfaForm(new FileInputStream(xmlFile)); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    }finally { 
     try { 
      stamper.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

此代碼工作得很好,我...

+0

能否請您介紹XMLFILE形成..? –

+2

哦..爲此,您需要參考XFA數據中的XSD。更簡單的方法是首先填寫pdf上的數據。然後點擊PDF上的導出數據。學習XML並在java中以您想要的方式創建它.. – sethu

+0

感謝您的幫助。 –