|
View:
New views
3 Messages
—
Rating Filter:
Alert me
|
|
|
Post same form multiple times with different dataCould somebody show some working example of iText-Java code or point to a url which demonstrates the following:
1. Post the same form multiple times with different data. 2. Is it a good idea to keep the form in memory, instead of reading the form every time for every posting? The data changes for every submission, but the form is the same for every posting. Thanks for your time in advance. |
|
|
Re: Post same form multiple times with different dataI sought out a forum like this so I could ask the same question.
I've been through chapter 14 of the book twice, but I'm still missing something. It gives XML examples, and uses a class provided with the library to parse an XML file multiple times and insert different data into it. I've got an AcroForm with fields, and I have code that replaces the fields with my own data, and that works for one document. I read in the book that PDF supports having a form placed in the document one time, and data provided multiple times for that form, without repeating the form information for each new set of data. That's what I want to find out more about. I don't know what call to make to say "here is a new set of data for a form you already have". Can someone indicate which call to look into? Thanks. I think this would answer the original poster's question, as well as my own. In case it's interesting to someone, below is a skeleton of the code I use to do what I do so far: PdfReader reader = new PdfReader(pdfInputFilename); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFilename); AcroFields fields = stamper.getAcroFields(); fields.setField(fieldName, fieldValue); // repeat above line for any number of fields for which you have name and value stamper.setFormFlattening(true); stamper.close(); rc |
|
|
Re: Post same form multiple times with different datawell, I've gotte a version of what I want to work starting from an example at http://www.lowagie.com/itextwiki/doku.php/forms:learningagreement - it requires version 2 of iText, and I'm still interested in learning another method that I've seen mentioned, but this will probably serve for the application I'm writing now.
// outputSpec is a string specifying the filepath for the output file // (i.e., the PDF that is produced by this code). inputSpec is // the path to the PDF form with fields that I'm going to fill. Document document = new Document(); // this is an iText class PdfSmartCopy outputCopy // PdfSmartCopy is a v2 iText class = new PdfSmartCopy(document, new FileOutputStream(outputSpec)); document.open(); for (int i=0; i<list.size(); i++) // my list has one element per set of data, // and therefore one elemenet per PDF page. { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // starting here, example logic in "fillAgreementForm(x,y)" PdfReader reader = new PdfReader(inputSpec); PdfStamper stamper = new PdfStamper(reader, outputStream); AcroFields pdfFieldsForm = stamper.getAcroFields(); // fieldValueMap: each key is the name of a field on the form, // each value is a key allowing retrieval of the data to be put // on that field. Iterator keys = fieldValueMap.keySet().iterator(); while (keys.hasNext()) { String docKey = (String)keys.next(); String valueKey = (String)fieldValueMap.get(docKey); String value = getValueForField(valueKey); // skipping all error handling pdfFieldsForm.setField(docKey, value); } stamper.setFormFlattening(true); stamper.close(); PdfReader resultReader = new PdfReader(outputStream.toByteArray()); outputCopy.addPage(outputCopy.getImportedPage(resultReader,1)); // both the example I copied and my own have one page in the document // that is filled with data; I don't know what to do if it has more than that. } document.close(); So, this is working and appears to produce a 7-page PDF that is one-third the size of one produced by concatenating separate PDF files. I'm still interested in the method I've seen mentioned of reading the file once to get the positions of the fields, storing those, then flattening the form and writing the data to the positions instead of using pdfFieldsForm.setField(). I don't know how to do that one yet -- I do not yet understand the "outputCopy.addPage()" in this example, and I don't know how to do the equivalent in that version. If someone can point me/us to that, I'd appreciate it. |
| Free Forum Powered by Nabble | Forum Help |