Paulo Soares wrote:

iText can only place the image as-is. It will still have 1000x1000 and it won't be sub-sampled to be smaller or to fit your desidered dpi. In other words, the decode/encode to jpeg must be done somewhere else.

I received the question in a more detailed version,
so I can give a more detailed answer.

I have added two extra methods to class Image.
With these methods, you can convert any java.awt.Image to a PDF
as a JPG with a given quality between 0 and 1. You also have to
pass the writer object (for newly created PDFs) or the direct content
object (if you're using PdfStamper) to which you are adding the image.
If you can't wait for the next release, just copy/paste the code that is
added below.

I defined some rectangular areas.
       Rectangle[] areas = new Rectangle[8];
       areas[0] = new Rectangle(36, 36, 252, 180);
       areas[1] = new Rectangle(36, 252, 252, 396);
       areas[2] = new Rectangle(36, 468, 252, 612);
       areas[3] = new Rectangle(36, 684, 252, 828);
       areas[4] = new Rectangle(264, 36, 480, 180);
       areas[5] = new Rectangle(264, 252, 480, 396);
       areas[6] = new Rectangle(264, 468, 480, 612);
       areas[7] = new Rectangle(264, 684, 480, 828);
I wrote some code that creates a PDF with these (empty) rectangles:
       Document document = new Document();
       try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("empty.pdf"));
           document.open();
           PdfContentByte cb = writer.getDirectContent();
           for (int i = 0; i < 8; i++) {
               areas[i].setBorder(Rectangle.BOX);
               areas[i].setBorderWidth(3);
               cb.rectangle(areas[i]);
           }
       }
       catch(Exception e) {
           e.printStackTrace();
       }
       document.close();

Now I will read this empty.pdf and add the images (I used the images you have sent me privately):
       try {
           PdfReader reader = new PdfReader("empty.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("with_images.pdf"));
           float quality;
           java.awt.Image awtImage;
           for (int i = 0; i < 8; i++) {
awtImage = Toolkit.getDefaultToolkit().createImage("../resources/image_" + (i+1) + ".jpg"); addImage(stamper.getOverContent(1), areas[i], awtImage, 0.2f);
           }
           stamper.close();
       } catch (Exception e) {
           e.printStackTrace();
       }

The method addImage answers your original question:
private static void addImage(PdfContentByte cb, Rectangle rectangle, java.awt.Image awtImage, float quality) throws DocumentException, IOException { com.lowagie.text.Image img = com.lowagie.text.Image.getInstance(cb, awtImage, quality);
       img.scaleToFit(rectangle.width(), rectangle.height());
       img.setAbsolutePosition(rectangle.left(), rectangle.bottom());
       cb.addImage(img);
   }

This is the code of the extra methods in Image:

public static Image getInstance(PdfWriter writer, java.awt.Image awtImage, float quality) throws BadElementException, IOException {
       return getInstance(writer.getDirectContent(), awtImage, quality);
   }

public static Image getInstance(PdfContentByte cb, java.awt.Image awtImage, float quality) throws BadElementException, IOException { java.awt.image.PixelGrabber pg = new java.awt.image.PixelGrabber(awtImage,
               0, 0, -1, -1, true);
       try {
           pg.grabPixels();
       } catch (InterruptedException e) {
           throw new IOException(
                   "java.awt.Image Interrupted waiting for pixels!");
       }
       if ((pg.getStatus() & java.awt.image.ImageObserver.ABORT) != 0) {
throw new IOException("java.awt.Image fetch aborted or errored");
       }
       int w = pg.getWidth();
       int h = pg.getHeight();
       PdfTemplate tp = cb.createTemplate(w, h);
       Graphics2D g2d = tp.createGraphics(w, h, true, quality);
       g2d.drawImage(awtImage, 0, 0, null);
       return getInstance(tp);
   }


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to