Вы находитесь на странице: 1из 4

Here is sample code that shows how to work with EEC forms using iText. 2 examples are here.

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));

ByteArrayOutputStream baosPDFDocument = new ByteArrayOutputStream();


try {
stamp = new PdfStamper(reader, baosPDFDocument);
stamp.setEncryption(true, null, "YourSecretPWD",
PdfWriter.AllowPrinting | PdfWriter.AllowFillIn
| PdfWriter.AllowScreenReaders);
AcroFields acroFields = stamp.getAcroFields();
if (printOnly)
acroFields.setField("Control.printOnly", "true");
acroFields.setField("Control.serverURL", returnURL);
acroFields.setField("Control.miscA", cmdStr);
stamp.close();
} catch (DocumentException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
// let the browser know it is a pdf
res.setContentType("application/pdf");
// The Content-length header must be set to the number of bytes
// in the PDF file.
res.setContentLength(baosPDFDocument.size());
// let the browser know how to display it and what to name the
// file
res.setHeader("Content-disposition",
" inline; filename=yourW4form.pdf");
ServletOutputStream sos = res.getOutputStream();
baosPDFDocument.writeTo(sos);

// send all bytes to the client.


sos.flush();
} else {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("Not enough parameters to continue ...");
}
}

----------------------------------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()];

//data coming from client

while (offset < bytes.length


&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead; }
AcroFields acroFields = stamp.getAcroFields();
//get a reference to the fields
from stamper, these are empty fields in the PdfReader "reader"
XfdfReader fdfDoc = new XfdfReader( bytes );
// create xfdf reader using input
data, we know that statew4.com forms are sending data back in the XFDF format (we passed :XFDFdata
into miscA field)
acroFields.setFields(fdfDoc);
//set fields in the "reader"
using "fdfDoc"
acroFields.setField("Control.previewMode","true");
// we want to display this form in a
read only preview mode
acroFields.setField("Control.signatureLine", "Electronically signed on " +
acroFields.getField("Control.dateTime"));
// at this point "Control.dateTime" field has already value
from fdfDoc stamp.close();

Вам также может понравиться