Hello, I have only been using iText for a few weeks. I am able to extract data
from PDF forms and create an XML file successfully. I am now wanting to do the
reverse and pre-fill forms using these same XML files. I am not able to
pre-fill reliably, though. Below is the XML, followed by my code...Please
suggest if I am taking the right approach here, and if you see anything that is
blatantly wrong. Thanks! The code executes without error, but the fom is blank
when I open it.
<?xml version="1.0" encoding="UTF-8"?>
<pdf_contact_info_form>
<sysobj/>
<document/>
<pdf_form/>
<contact_info_form>
<comments
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">hi</comments>
<cell_phone_number
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">111-111-1111</cell_phone_number>
<home_phone_number
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">111-111-1111</home_phone_number>
<ss_number
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">111-11-1111</ss_number>
<employee_zip_code
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">11111</employee_zip_code>
<employee_state
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">ca</employee_state>
<employee_address
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">123 main
st</employee_address>
<employee_name
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">hi</employee_name>
<form_date
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">2010-12-21</form_date>
</contact_info_form>
</pdf_contact_info_form>
code...
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.XfaForm;
import com.itextpdf.text.pdf.XfaForm.Xml2SomDatasets;
public class fillinpdf {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
// take the xml bits
byte[] xmlBits = getBytesFromFile(new File("C:/temp/xml.xml"));
// XML parse
DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance();
builderFactory.setNamespaceAware(true);
org.w3c.dom.Document doc = builderFactory.newDocumentBuilder() .parse(new
ByteArrayInputStream(xmlBits));
// the result pdf will be in baos
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// take the template
String templatePath = "C:/temp/template.pdf";
File template = new File(templatePath);
if (!template.exists()) {
throw new IllegalArgumentException("error");
} else if (!template.canRead()) {
throw new IllegalArgumentException("error('"+ templatePath);
}
//open the template
PdfReader reader = new PdfReader(templatePath);
XfaForm xfa = new XfaForm(reader);
// is there XFA ?
if (!reader.getAcroFields().getXfa().isXfaPresent()) {
// if not, you can do smt.
}
PdfStamper stamper = new PdfStamper(reader, baos);
Node n = stamper.getAcroFields().getXfa().getDatasetsNode();
System.out.println("n.getNodeName() = " + n.getNodeName());
Xml2SomDatasets som = new Xml2SomDatasets(n);
XfaForm.Xml2SomDatasets data = new
XfaForm.Xml2SomDatasets(doc.getFirstChild());
for (Iterator it = data.getOrder().iterator(); it.hasNext();) {
String name = (String)it.next();
System.out.println("name = " + name);
String text = XfaForm.getNodeText((Node)data.getName2Node().get(name));
Set map= som.getName2Node().keySet();
for (Iterator it2 = map.iterator(); it2.hasNext();) {
String str=it2.next().toString();
if( str.contains(name)){
Node n1 = (Node)som.getName2Node().get(str);
n1.setNodeValue(text);
stamper.getAcroFields().getXfa().setNodeText(n1,text);
System.out.println("text = " + text);
break;
}
}
}
stamper.getAcroFields().getXfa().setChanged(true);
System.out.println("after setChanged(true)");
//stamper.getAcroFields().getXfa().setDatasetsSom(data);
//System.out.println("after seDatasetSom(data)");
XfaForm.setXfa(xfa,stamper.getReader(),stamper.getWriter());
System.out.println("after setXfa(stamper)");
stamper.getAcroFields().mergeXfaData(doc);
System.out.println("after getAcroFields.mergeXfaData(doc)");
stamper.close();
System.out.println("after stamper.close()");
reader.close();
System.out.println("after reader.close())");
try {//lets look at what happened in here
FileOutputStream fos;
fos = new FileOutputStream(new File("C:/temp/filled_out.pdf"));
fos.write(baos.toByteArray() );
fos.flush();
fos.close();
} catch ( Exception e) {
e.printStackTrace();
}
} catch (Throwable ex) {
System.out.println("xml error occur"+ex.getMessage());
}
}//end of main method
public static byte[] getBytesFromFile(File file) throws IOException {
FileInputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();
if (length > Integer.MAX_VALUE) {
// File is too large
}
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
// Close the input stream and return bytes
is.close();
return bytes;
}//
------------------------------------------------------------------------------
Forrester recently released a report on the Return on Investment (ROI) of
Google Apps. They found a 300% ROI, 38%-56% cost savings, and break-even
within 7 months. Over 3 million businesses have gone Google with Google Apps:
an online email calendar, and document program that's accessible from your
browser. Read the Forrester report: http://p.sf.net/sfu/googleapps-sfnew
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php