Hi Paulo,

that worked a treat - the file size is even less than I predicted.

A massive thanks for replying so quickly as well.

Kind regards,

Matt


Paulo Soares-3 wrote:
> 
> 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;
>     private Image img;
> 
>     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();
>             img = Image.getInstance(logoPath);
>             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);
>         insertLogo();
>     }
> 
>     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);
>                 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);
>             }
>         }
>     }
> }
> 
> The image will only be added once.
> 
> Paulo
> 
>> -----Original Message-----
>> From: Matthew Hawkins [mailto:hawk...@4email.net] 
>> Sent: Monday, May 11, 2009 11:19 AM
>> To: itext-questions@lists.sourceforge.net
>> Subject: [iText-questions] Adding Image to template
>> 
>> 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
>>   hawk...@4email.net
>> 
>> 
>> --------------------------------------------------------------
>> ----------------
>> 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
>> iText-questions@lists.sourceforge.net
>> 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/
>> 
> 
> Aviso Legal:
> 
> Esta mensagem é destinada exclusivamente ao destinatário. Pode conter
> informação confidencial ou legalmente protegida. A incorrecta transmissão
> desta mensagem não significa a perca de confidencialidade. Se esta
> mensagem for recebida por engano, por favor envie-a de volta para o
> remetente e apague-a do seu sistema de imediato. É proibido a qualquer
> pessoa que não o destinatário de usar, revelar ou distribuir qualquer
> parte desta mensagem. 
> 
> 
> 
> Disclaimer:
> 
> This message is destined exclusively to the intended receiver. It may
> contain confidential or legally protected information. The incorrect
> transmission of this message does not mean the loss of its
> confidentiality. If this message is received by mistake, please send it
> back to the sender and delete it from your system immediately. It is
> forbidden to any person who is not the intended receiver to use,
> distribute or copy any part of this message.
> 
> 
> 
> 
> ------------------------------------------------------------------------------
> 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
> iText-questions@lists.sourceforge.net
> 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/
> 

-- 
View this message in context: 
http://www.nabble.com/Adding-Image-to-template-tp23480726p23481414.html
Sent from the iText - General mailing list archive at Nabble.com.


------------------------------------------------------------------------------
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
iText-questions@lists.sourceforge.net
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/

Reply via email to