Hi,
There recently was a question on how to do this. I have a solution that
works. Maybe this is of interest to someone:
Hubert
import java.io.FileOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import com.lowagie.text.pdf.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.apache.xerces.dom.CoreDOMImplementationImpl;
public class ChangeXFASubmitURL {
public static final String FORM = "d:\\test\\Lead-Rapport_V1a.pdf";
public static final String RESULT = "d:\\test\\result.pdf";
static final String url =
"http://hostname:40000/flw/pdf/1/?id=1858&sid=9e42d43a-96f4-11de-a9c0-5dc15587b754";
public static final String buttonName = "submit"; // you have to
know the name of the submit button field and this better be unique
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader(FORM);
PdfDictionary root = reader.getCatalog();
PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
PdfArray xfa = acroform.getAsArray(PdfName.XFA);
for (int i = 0; i < xfa.size(); i += 2) {
if ("template".equals(xfa.getAsString(i).toString())) {
PRStream s = (PRStream) xfa.getAsStream(i + 1);
byte[] b = PdfReader.getStreamBytes(s);
// use xerces
System.setProperty(DOMImplementationRegistry.PROPERTY,
"org.apache.xerces.dom.DOMImplementationSourceImpl");
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new
ByteArrayInputStream(b));
((Node)
document.getElementsByTagName(buttonName).item(0)).getAttributes().getNamedItem("target").setNodeValue(url);
// make an LSOutput
DOMImplementationRegistry registry =
DOMImplementationRegistry.newInstance();
CoreDOMImplementationImpl impl = (CoreDOMImplementationImpl)
registry.getDOMImplementation("LS");
org.w3c.dom.ls.LSSerializer lsSerializer =
impl.createLSSerializer();
LSOutput lsOutput = impl.createLSOutput();
ByteArrayOutputStream btOutputStream = new
ByteArrayOutputStream();
lsOutput.setByteStream(btOutputStream);
lsSerializer.write(document, lsOutput);
byte[] bytes = ((ByteArrayOutputStream)
lsOutput.getByteStream()).toByteArray();
// the xml writer always puts an xml header on top of the
file
// this has to be removed!!
String result = new String(bytes);
int ur = result.indexOf('>'); // remove <?xml
version="1.0" encoding="UTF-8"?>
result = result.substring(ur + 1);
s.setData(result.getBytes());
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(RESULT));
stamper.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
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/