Hi,
I faced up with a problem while working with dynamic PDF which I want to do
the same thing that AcroFields.mergeXfaData() did.After couple of days on a
hard work:-) on this problem I could solve at last and I want to share it
with you..Hope it will be helpfull to someone.. Because the ones which I
looked were always need some other classes which I could not find.Here is
the solution.. if anyone who knows the shorter ways to do this pls help me
to improve the solution.
public static void fillPdf( ) {
try {
// take the xml bits
byte[] xmlBits = getBytesFromFile(new
File("C:/dfg.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:\\efatura\\sablon\\rekffo012s6100-ru1gfnpz3uwc00-r1ptfl90muhb00-template_
subform.pdf";
File template = new File(templatePath);
if (!template.exists()) {
throw new IllegalArgumentException("Verilen
PDF şablonu ('"
+ templatePath + "')
bulunamadı.");
} else if (!template.canRead()) {
throw new IllegalArgumentException("Verilen
PDF şablonu ('"
+ templatePath + "') bulundu
ama okunamıyor.");
}
//open the template
PdfReader reader = new PdfReader(templatePath);
// 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();
Xml2SomDatasets som = new Xml2SomDatasets(n);
XfaForm.Xml2SomDatasets data = new
XfaForm.Xml2SomDatasets(doc.getFirstChild());
for (Iterator it = data.getOrder().iterator();
it.hasNext();) {
/* here maps the xml to fields
my xml file is like this:
<?xml version="1.0" encoding="UTF-8"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<TABLO>
<SATIR>
<D>D</D>
A
<C>C</C>
B
</SATIR>
<SATIR>
<D>DD</D>
AA
<C>CC</C>
BB
</SATIR>
<SATIR>
<D>DDD</D>
AAA
<C>CCC</C>
BBB
</SATIR>
<SATIR>
<D>DDDD</D>
AAAA
<C>CCCC</C>
BBBB
</SATIR>
<SATIR>
<D>DDDDD</D>
AAAAA
<C>CCCCC</C>
BBBBB
</SATIR>
<SATIR>
<D>DDDDDD</D>
AAAAAA
<C>CCCCCC</C>
BBBBBB
</SATIR>
</TABLO>
<isim>CYBERSOFT LTD STI</isim>
<adres>ADRES</adres>
</form1>
</xfa:data>
</xfa:datasets>
</xdp:xdp>
node =
data[0].xdp[0].TABLO[0].SATIR[5].D[0]
name =
datasets[0].data[0].form1[0].TABLO[0].SATIR[0].D[0]
* */
String name = (String)it.next(); // in the name
there is a value like: datasets[0].data[0].form1[0].TABLO[0].SATIR[0].D[0]
String text = XfaForm.getNodeText((
Node)data.getName2Node().get(name));
String name2 =
name.substring(name.indexOf("form1")+9);
//in the name2 there is a value like:
TABLO[0].SATIR[0].D[0]
Set<HashMap> map=
som.getName2Node().keySet();
for (Iterator it2 = map.iterator();
it2.hasNext();) {
String str=it2.next().toString();
if( str.contains(name2)){//here puts the value
to the related field
//Node n1 =
(Node)som.getName2Node().get("data[0].xdp[0].TABLO[0].SATIR[x].D[y]");
Node n1 = (Node)som.getName2Node().get(str );
n1.setNodeValue(text);
stamper.getAcroFields().getXfa().setNodeText(n1,text);
}
}
}
stamper.getAcroFields().getXfa().setChanged(true);
stamper.getAcroFields().getXfa().setDatasetsSom( som
);
XfaForm.setXfa(stamper.getAcroFields().getXfa(),stamper.getReader(),stamper.
getWriter());
stamper.getAcroFields().mergeXfaData(doc);
stamper.close();
reader.close();
try {//lets look at what happened in here
FileOutputStream fos;
fos = new FileOutputStream(new
File("C:/dfg.pdf"));
fos.write(baos.toByteArray() );
fos.flush();
fos.close();
} catch ( Exception e) {
e.printStackTrace();
}
} catch (Throwable ex) {
Logger.getLogger(XmlFormArchitectureServer.class.getName()).log(
Level.SEVERE, null, ex);
}
}
public static byte[] getBytesFromFile(File file) throws IOException
{
InputStream 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;
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
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