Eec Forms Using I Text
Eec Forms Using I Text
First example is how you can create and populate pdf form. It really sets only couple of fields (look
for acroFields.setField lines), but it shows you how iText is used to merge data in pdf form.
----------------public void doPostUsingiText(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
String cmdStr = "CMD:XFDFdata";
if (SSNdoubleEntry) //SSN double entry
cmdStr = cmdStr + ":RequestSSNreentry";
if (SSNextenedValidation) //SSN extended validation
cmdStr = cmdStr + ":UseExtendedSSNvalidation";
if (useWorkSheetResults) //use work sheet results
cmdStr = cmdStr + ":UseWSresults";
if (showPrintDialog) //shows print dialog first
cmdStr = cmdStr + ":ShowPrintDialog";
if (returnURL == null)
throw new ServletException(
"Please supply returnURL parameter in web.xml file");
if (formDirectoryURL == null)
throw new ServletException(
"Please supply formDirectoryURL parameter in web.xml file");
if (req.getParameter("file") != null) {
String formPath = formDirectoryURL + req.getParameter("file");
PdfStamper stamp = null;
PdfReader reader = new PdfReader(new URL(formPath));
----------------------------------This second piece of code shows, how to recreate the form using data coming from the form. I
actually commented this one, so this might be easier to read than the first one.
PdfStamper stamp = null;
PdfReader reader = new PdfReader(new URL(formPath));
//this is the empty pdf form from the given URL, formpath points to the pdf that was displayed to the user
to fill,
//so the fdf data that are coming back will match the fields on the form
baosPDFDocument = new ByteArrayOutputStream();
try {stamp= new PdfStamper( reader, baosPDFDocument );
//new stamper using the pdf reader created above
} catch (DocumentException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
int offset = 0;
int numRead = 0;
ServletInputStream is = request.getInputStream();
byte bytes[] = new byte[request.getContentLength()];