Hello,
I have two pieces of itext code that I need to put
together into one - but I haven't figured out how to
do it.
I have a PDF template that I need to put meta-data AND
a dynamic image into.
***This example works fine for me to merge meta-data
into a PDF template:
http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/copystamp/Register.java
***This example works fine for me to put an image into
a PDF on-the-fly:
http://itextdocs.lowagie.com/examples/com/lowagie/examples/objects/images/Images.java
The problem is, the image example adds the image to a
document object, but the template example uses
PdfReader, Stamper, AcroReader, etc. - but not the
Document object in the same way.
Any thoughts on how I can put an image in a PDF
template? Below is my attempt.
Thanks, Ceily
public void render(OutputStream os) throws
IOException {
ByteArrayOutputStream bos = null;
PdfCopy copy = null;
Document doc = null;
try {
bos = new ByteArrayOutputStream();
PdfReader reader = null;
PdfStamper stamp = null;
Iterator iTemplates =
document.getTemplates().iterator();
while (iTemplates.hasNext()) {
String template = (String)
iTemplates.next();
if (document.isDynamic(template)) {
//dynamic template
reader = new PdfReader(template);
stamp = new PdfStamper(reader,
bos);
AcroFields form =
stamp.getAcroFields();
Iterator iter =
form.getFields().keySet().iterator();
while ( iter.hasNext() ) {
String field =
(String)iter.next();
Object value =
fieldValues.get(field);
if (null == value) {
continue; // ignore null
values
} else if
(value.getClass().isInstance(string) &&
StringUtils.isNotEmpty(value.toString())) {
form.setField(field,
value.toString());
}
}
stamp.setFormFlattening(true);
stamp.close();
doc = new Document();
copy = new PdfCopy(doc, os);
copy.open();
doc.open();
reader = new
PdfReader(bos.toByteArray());
copy.addPage(copy.getImportedPage(reader, 1));
//Get the image.. For debug
purposes - I'm just trying
//to get a plain image on my file system. I can
see
//the image when I use it in the iText example -
but in this
//situation, I'm not getting any errors -
or seeing the image
Image fooImage =
Image.getInstance("/tmp/fooImage.jpg");
if (fooImage != null) {
Image img =
Image.getInstance(fooImage);
img.setAbsolutePosition(200,
250);
doc.add(img);
}
} else { // Must be a static template
reader = new PdfReader(template);
for ( int count = 0; count <
reader.getNumberOfPages(); count++) {
copy.addPage(copy.getImportedPage(reader, count+1));
}
}
}
} catch (DocumentException ex) {
System.out.println(ex.getMessage());
}
if (os!=null)
os.close();
if (doc!=null)
doc.close();
if (copy!=null)
copy.close();
}
-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions