I have used Adobe Actrobat Pro v6 to create a form in a PDF document. Now I am using the iText Java/PDF library to copy this PDF form document to create a new multi-page PDF document in memory based on multiple pages of this form.
In order to re-use the same form (and fields) document over and over again we must flatten the new multi-page PDF document immediately after appending a new page with completed form values. This way when we next add a new page there won't be any collision with the identically named form fields already existing in the new document from the previous time it was appended to. I'm sure this is a common issue with documents consisting of identical repeating forms. Hopefully we've addressed it in a correct and efficient manner.
We have an issue managing the growth in size of the resulting PDF. Our single page PDF form/document has 150 form fields and when it has been filled in with data and stamped/flattened it results in a file size of 42K.
Currently we are building the entire document in memory (rather than writing to disk) as it is more efficient and we only need to email the resulting PDF document. The problem is that we need to generate PDF documents of this type up to 300 pages long. Currently the application runs out of memory after about 40 pages which is a document size of about 2MB. Using the above metrics a 300 page document would result in a file size of approx 12.5MB which is far too big for practical purposes.
Are there any practical approaches to managing/reducing a PDFs growth in the context that I've described above (ie. on-the-fly and in memory)?
Here is the commented pseudo code that shows how we are building this document. Any suggestions on how this may be done better are welcomed.
Thanks,
Henry Watson
// Setup a new reader for each type of form that could be used ...
PdfReader standard3itemRecordFormReader = new PdfReader(formsDirectoryPath + bcdRecord3itemFormName);
// Point the "working" reader to the PDF form that should be used/copied
PdfReader recordFormReader = standard3itemRecordFormReader;
// OutputStream for final PDF document being created
OutputStream pdfBOS = new ByteArrayOutputStream();;
// Byte Array for intermediate pages being created and appended to the main output document
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// Setup a new document and writer for the main PDF document "pdfBOS" that we intend to create
// (copy the page size from another existing form)
PdfReader reader = new PdfReader(headFormReader);
Document bcdDocument = new Document(reader.getPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.getInstance(bcdDocument, pdfBOS);
bcdDocument.open();
// PROCESS MULTIPLE RECORD PAGES
while (recordNumber < totalRecords) {
// 1) Setup a reader for the BCD Record PDF form/document
reader = new PdfReader(recordFormReader);
// 2) Setup a stamper - used to fill in the BCD Record form data (from above reader)
// results will be writen to intermediate Byte Array "bos"
stamper = new PdfStamper(reader, bos);
// 3) Get all the fields in the form, fill them in, and flatten the form
formFields = stamper.getAcroFields();
fillRecordDetails(bcdDataBean, formFields, recordNumber, labelNumber);
stamper.setFormFlattening(true);
stamper.close();
// 4) Setup a reader for the newly created/completed page (stored in the intermediate Byte Array "bos")
reader = new PdfReader(bos.toByteArray());
// 5) Copy page (from above reader) and append to the main output PDF document (writer/"pdfBOS")
page = writer.getImportedPage(reader, 1);
contentByte.addTemplate(page, page.getXTLM(), page.getYTLM());
bcdDocument.newPage();
}
//close the document
bcdDocument.close();
Do you Yahoo!?
vote.yahoo.com - Register online to vote today!