title: Writing image into pdf file in java
body: <p>I'm writing a code to convert Microsoft power-point(ppt) slides into images and to write the generated images into pdf file. Following code generates and writes the images into pdf file but the problem i'm facing is, when i write image into pdf file it's size is exceeding the pdf page size and i can view only 75% of the image rest is invisible. One more thing to notice here is, written images in pdf file look like zoomed or expanded. Take a look at the following snippet of code:</p>
  
  <pre><code>for (int i = 0; i &lt; slide.length; i++) {
      BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,   BufferedImage.TYPE_INT_RGB);
      Graphics2D graphics = img.createGraphics();
      graphics.setPaint(Color.white);
      graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height));
      slide[i].draw(graphics);
      fileName="C:/DATASTORE/slide-"+(i+1)+".png";
      FileOutputStream out = new FileOutputStream(fileName);
      javax.imageio.ImageIO.write(img, "png", out);
  out.flush();
  out.close();
  com.lowagie.text.Image image =com.lowagie.text.Image.getInstance(fileName);
              image.setWidthPercentage(40.0f);
      doc.add((image));
      }
  
  doc.close();
  } catch(DocumentException de) {
            System.err.println(de.getMessage());
      }
  </code></pre>
  
  <p>If anybody knows the solution please help me to rectify. Thank you. </p>
  
  <p>Here is the code it accomplishes the task i wished. Now i'm getting the desired results after following Bruno Lowagie recommendations.</p>
  
  <p>But, as Bruno Lowagie pointed out earlier, their is a problem in generated png image. The generated png image is not correct because shape or image in the slide overlaps with the texts of the slide. Can you help me to identify and rectify the error?</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics2D;
  import java.awt.Rectangle;
  import com.itextpdf.text.Image;
  import java.awt.image.BufferedImage;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import org.apache.poi.hslf.model.Slide;
  import org.apache.poi.hslf.usermodel.SlideShow;
  import com.itextpdf.text.Document;
  import com.itextpdf.text.DocumentException;
  import com.itextpdf.text.PageSize;
  import com.itextpdf.text.pdf.PdfWriter;
  public class ConvertSlidesIntoImages {
      public static void main(String[] args){
      try {
          FileInputStream is = new FileInputStream("C:/DATASTORE/testPPT.ppt");
          SlideShow ppt = new SlideShow(is);
          is.close();
          String fileName;
          Dimension pgsize = ppt.getPageSize();
          Slide[] slide = ppt.getSlides();
          Document doc=new Document();
          PdfWriter.getInstance(doc, new  FileOutputStream("c:/DATASTORE/convertPPTSlidesIntoPDFImages.pdf"));
          doc.open();
  
          for (int i = 0; i &lt; slide.length; i++) {
              BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
              Graphics2D graphics = img.createGraphics();
              graphics.setPaint(Color.white);
              graphics.fill(new Rectangle(0, 0, pgsize.width, pgsize.height));
              slide[i].draw(graphics);
              fileName="C:/DATASTORE/slide-"+(i+1)+".png";
              FileOutputStream out = new FileOutputStream(fileName);
              javax.imageio.ImageIO.write(img, "png", out);
              out.flush();
              out.close();
              com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(fileName);
              doc.setPageSize(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
              doc.newPage();
              image.setAbsolutePosition(0, 0);
              doc.add(image);
              }
      doc.close();
  }catch(DocumentException de) {
      System.err.println(de.getMessage());
      }
      catch(Exception ex) {
      ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Thank you</p>