We are developing an utility to apply information to PDF as an overlay on the fly when the PDF is request for viewing.
To do this, we read a PDF document (we call this the overlay) with fields, use AcroFields to fill out the form, save the output to a ByteArrayOutputStream.
The PDF that was saved to the ByteArray is imported to every page of the PDF the user requested.
One of the fields on the PDF overlay is the page number. To update this field we repeat the process of filling the form and importing into the PDF document.
Maybe this help me explain better:
// Open a reader for the overlay file
PdfReader reader = new PdfReader(overlay);
// Create a memory buffer to hold the overlay with the fields completed
ByteArrayOutputStream out = new ByteArrayOutputStream();
// Open the PDF stamper from the reader to the memory buffer
PdfStamper stamp = new PdfStamper(reader, out);
// Get a list of the field names on the overlay
AcroFields pdfFields = stamp.getAcroFields();
for ( Iterator fieldNames = pdfFields.getFields().keySet().iterator(); fieldNames.hasNext();) {
String fieldName = (String)fieldNames.next();
// Verify that the field was defined and get the value from the data source
if ( fields.containsKey(fieldName) == true ) {
Field field = (Field)fields.get(fieldName);
pdfFields.setField(fieldName, field.getValue(req));
}
}
// Request to remove the fields and leave the values
stamp.setFormFlattening(true);
// Close the stamper for the overlay
stamp.close();
// Open the overlay in memory to add it to the content
reader = new PdfReader(out.toByteArray());
// Add the overlay to the page
if (req.isPagePortrait() == true )
template = req.getOutStamper().getImportedPage(reader, PAGE_PORTRAIT);
else
template = req.getOutStamper().getImportedPage(reader, PAGE_LANDSCAPE);
I'm concern on the size of the resulting file. It seems that if we generate this overlay once and applied to every page the size only increase by the same size of the overlay. If we generate this overlay for every page (to update page number) then the file size increase quite a lot. For example an original 9MB PDF, increased to 10MB with the static overlay, and 16MB with the dynamic overlay.
Any suggestions to improve this?
I thought about applying two overlays, one with static values (fill out overlay once and apply to every page), and another with dynamic values (fill out overlay and import for every page).
Any ideas?
Saludos,
Orlando M. Amador
Technology Lead
Ortho Biologics LLC
[EMAIL PROTECTED]
(787) 854-1800 x2294
