Hi,

I am extracting the XML from an XFA form, processing it in a different 
application and now would like to stuff it back into the PDF.

Unfortunately nothing I do to the data sticks.

Here is my code:

public static void main(String[] args) {
        try {
                if (args.length==0) {
                        throw new Exception("Filename not specified");
                }
                        
                String filename = args[0];
                File infile = new File(filename);
                        
                PdfReader reader = new PdfReader(infile.getAbsolutePath());
        
                XfaForm xfa = new XfaForm(reader);
                
                if (!xfa.isXfaPresent()) {
                        throw new Exception("isXfaPresent() returns false");
                }
                
                Document doc = xfa.getDomDocument();
                        
                File newxmlfile = new 
File(changeExtension(infile.getAbsolutePath(),".new.xfa"));
                if (newxmlfile.exists()) {
                        DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
                        
                        dbf.setNamespaceAware(true);
                        DocumentBuilder db = dbf.newDocumentBuilder();          
        
                        Document newdoc = db.parse(newxmlfile);
                
                        while 
(doc.getDocumentElement().getChildNodes().getLength()>0) {
                         
doc.getDocumentElement().removeChild(doc.getDocumentElement().getChildNodes().item(0));
                        }
                        
                        NodeList newChildren = 
newdoc.getDocumentElement().getChildNodes();
                        for(int i=0 ; i<newChildren.getLength() ; i++ ) {
                                Node copy = doc.importNode(newChildren.item(i), 
true);
                                doc.getDocumentElement().appendChild(copy);
                        }
                        
                        PdfStamper stamper = new PdfStamper(reader, 
System.out,'\0',true);
                        xfa.setDomDocument(doc);
                        xfa.setChanged(true);
                                                
                        XfaForm.setXfa(
                                xfa,
                                stamper.getReader(),
                                stamper.getWriter());
                
                        stamper.close();
                } else {
                        throw new Exception("replacement xfa not found in \"" + 
newxmlfile.getAbsolutePath() + "\"");                           
                }
                
                reader.close();
                
         } catch (Exception de) {
                de.printStackTrace();
         }
     }

The funny clear document/importnode/appendchild logic is a desperate 
attempt. I also tried the much simpler xfa.setDomDocument(newdoc).

The program runs and creates a copy of the input. It is changed, the 
size is not exactly the same, but any changes to the content of the xml 
are lost.

I have even tried the extreme case of emptying the new xml file of 
everything but the <xdp:xdp> root-element. No change.

Any idea what I am doing wrong?

Ciao, MM

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT 
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, & 
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian 
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com 
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to