title: How to compress images stored in a .pdf
body: <p>I have the following code at a client site:</p>
  
  <pre><code>   private void writeBufferedImage2Pdf(PdfContentByte cb, BufferedImage img)
     {
        com.itextpdf.text.Image BWImage = com.itextpdf.text.Image.getInstance(img, null, false);
  
        BWImage.setAbsolutePosition(0, 0);  // from the lower left
        BWImage.scaleToFit(612,792);   // 8.5x11 (72 per inch)
        cb.addImage(BWImage, false);
     }
  </code></pre>
  
  <p>I have noticed that the size of the .pdf that gets generated larger than I would expect.   For instance, for a .pdf that has 8 .jpg images that totaled 3.3MB will explode to 28MB when the .pdf is generated with those 8 jpgs.</p>
  
  <p>How can I apply a compression scheme to the image that is added to the .pdf?</p>
  
  <p>I want to use a lossless compression algorithm for storing the image even if the original images are .jpg.</p>
  
  <p>It happens that the images are text so we are also looking into converting to b/w, but that is a separate issue that will allow us to compress even further.</p>
  
  <p>[EDIT] Although the image is originally .jpg image it is read into a buffered image because it is manipulated in the application.  Thinks like cropping, splitting, and resizing are done.  Watermark timestamps might also be added.</p>
  
  <p>[EDIT] My hope is there is a lossless compression scheme I can apply when writing the image to the .pdf.  For instance, whatever scheme .png uses may not compress as much as .jpg, but it should still compress.  Or am I assuming it is not compressed and 28MB is a compressed size?</p>