Hi,
first thanks to those that answered my previous question - I am using a
different method as I have a heap space error, so must go down the
BatchFormFill route when using forms rather than PDFSmartCopy etc.
My problem is that I have a base PDF, and I must add an Image at runtime
to it. Now that I have this PDF with the Image, I must run about 700
times populating data.
I am using the following code, but the image does not appear - but if I
add it to the direct content it does, on the first page (as you would
expect). So my Image works and is not null - but it does not appear on
the template document.
I can add the Image to each individual page, but as you would imagine
the resulting document size is much too big. I must add the Image to the
document once to keep the document size small.
Please can you tell me what I'm doing wrong - am I adding the image to
the wrong object??
public class PDFPayslipListener extends PdfPageEventHelper {
private static final Logger LOG =
Logger.getLogger(PDFPayslipListener.class);
private PdfImportedPage template;
private PdfContentByte canvas;
private String pdfFileName;
private String logoPath;
private AcroFields pdfForm;
public void setPdfFileName(final String filePath) {
pdfFileName = filePath;
}
public void setLogoPath(final String logoPath) {
this.logoPath = logoPath;
}
public AcroFields getPdfForm() {
return pdfForm;
}
@Override
public void onOpenDocument(final PdfWriter writer, final Document
doc) {
try {
PdfReader reader = new PdfReader(pdfFileName);
pdfForm = reader.getAcroFields();
canvas = writer.getDirectContentUnder();
insertLogo();
template = writer.getImportedPage(reader, 1);
} catch(IOException e) {
LOG.error("Exception caught in PDF Listener.", e);
}
}
@Override
public void onEndPage(final PdfWriter writer, final Document doc) {
canvas.addTemplate(template, 0, 0);
}
private void insertLogo() {
float[] logoPos = pdfForm.getFieldPositions("LOGO");
if(logoPos != null) {
try {
//float fieldPage = logoPos[0];
float fieldLx = logoPos[1];
float fieldLy = logoPos[2];
float fieldUx = logoPos[3];
float fieldUy = logoPos[4];
Rectangle rect = new Rectangle(fieldLx, fieldLy,
fieldUx, fieldUy);
Image img = Image.getInstance(logoPath);
float absPosWidth = fieldLx;
float absPosHeight = fieldLy;
img.scaleToFit(rect.getWidth(), rect.getHeight());
img.setAbsolutePosition(absPosWidth, absPosHeight);
canvas.addImage(img, rect.getWidth(), 0, 0,
rect.getHeight(), absPosWidth, absPosHeight);
} catch(DocumentException ex) {
LOG.error("Document Error adding image to template.",
ex);
} catch(IOException ex) {
LOG.error("IOException adding image to template.", ex);
}
}
}
}
--
Matthew Hawkins
[email protected]
------------------------------------------------------------------------------
The NEW KODAK i700 Series Scanners deliver under ANY circumstances! Your
production scanning environment may not be a perfect world - but thanks to
Kodak, there's a perfect scanner to get the job done! With the NEW KODAK i700
Series Scanner you'll get full speed at 300 dpi even with all image
processing features enabled. http://p.sf.net/sfu/kodak-com
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/