Save and fill pdf data to and from xML using XFA on iText in java

Posted: December 12, 2013 in web

Bbean (Backing Bean)


...
 public void extractXMLFromPDF(byte[] src) throws IOException, ParserConfigurationException, SAXException,
            TransformerFactoryConfigurationError, TransformerException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        PdfReader reader = new PdfReader(src);
        XfaForm xfa = new XfaForm(reader);
        Node node = xfa.getDatasetsNode();
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if ("data".equals(list.item(i).getLocalName())) {
                node = list.item(i);
                break;
            }
        }

        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");

        tf.transform(new DOMSource(node), new StreamResult(out));

	//remove line breaks
        String outString = new String(out.toByteArray(), "UTF-8");
        outString = outString.replaceAll("<(.+?) xfa:dataNode(.+?)>", "").replace("\n", "").replace("\r", "");

        someservice.createQuote(outString.getBytes(), "sdf", contactName, "sdf");
        reader.close();
    }

    public StreamedContent getBrokerQuoteXMLData(BrokerQuote brokerQuote) {
        StreamedContent file;
        file = new DefaultStreamedContent(new ByteArrayInputStream(brokerQuote.getXmlData()), "text/xml", "quote.xml");
        return file;
    }
 public StreamedContent getBrokerQuote(BrokerQuote brokerQuote) throws IOException, DocumentException {
        InputStream inputStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("TextXFA.pdf");

        PdfReader reader = new PdfReader(inputStream);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, out);
        AcroFields form = stamper.getAcroFields();
        XfaForm xfa = form.getXfa();
        xfa.fillXfaForm(new ByteArrayInputStream(brokerQuote.getXmlData()));
        stamper.close();
        reader.close();

        StreamedContent file;
        file = new DefaultStreamedContent(new ByteArrayInputStream(out.toByteArray()), "text/xml", "quote.pdf");
        return file;

    }
...

xhtml

..

                    <p:column headerText="Actions">
                        <p:commandButton value="Download PDF" ajax="false" icon="ui-icon-arrowthickstop-1-s">
                            <p:fileDownload value="#{bBean.getBrokerQuote(brokerQuote)}" />
                        </p:commandButton>
                        <p:commandButton value="Download XML Data" ajax="false" icon="ui-icon-arrowthickstop-1-s">
                            <p:fileDownload value="#{bBean.getBrokerQuoteXMLData(brokerQuote)}" />
                        </p:commandButton>
                    </p:column>

...
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s