Example usage for com.lowagie.text.pdf XfaForm setDomDocument

List of usage examples for com.lowagie.text.pdf XfaForm setDomDocument

Introduction

In this page you can find the example usage for com.lowagie.text.pdf XfaForm setDomDocument.

Prototype

public void setDomDocument(org.w3c.dom.Document domDocument) 

Source Link

Document

Sets the top DOM document.

Usage

From source file:questions.forms.FillDynamicXfa2.java

public static void main(String[] args) {
    try {// w  w w. j  av a 2s  .  c  o  m
        // getting new data from a "datasets" XML snippet
        File file = new File(RESOURCE_DATA);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document newdoc = db.parse(file);
        Element element = newdoc.getDocumentElement();
        NodeList nodelist = element.getElementsByTagNameNS("http://www.xfa.org/schema/xfa-data/1.0/", "data");
        Node newdata = nodelist.item(0);

        // replacing the XFA in an existing document
        PdfReader reader = new PdfReader(RESOURCE_PDF);
        XfaForm xfa = new XfaForm(reader);
        Document doc = xfa.getDomDocument();
        NodeList list = doc.getElementsByTagNameNS("http://www.xfa.org/schema/xfa-data/1.0/", "datasets");
        list.item(0).replaceChild(doc.importNode(newdata, true), list.item(0).getFirstChild());
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        xfa.setDomDocument(doc);
        xfa.setChanged(true);
        XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter());
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }
}