I suspect you have a RGB image with a CMYK color profile, the profile must 
match the bytes in the image otherwise you'll have the "insufficient data" 
error. Have a look at 
http://thread.gmane.org/gmane.comp.java.lib.itext.general/52049/.

Paulo
  ----- Original Message ----- 
  From: fhomasp 
  To: itext-questions@lists.sourceforge.net 
  Sent: Friday, August 20, 2010 11:12 AM
  Subject: [iText-questions] PdfContentByteInst.addImage messes up jpg



  Hey,

  I seem to have stumbled upon this peculiar problem and eventhough this might
  easy be a java.awt problem, I am using iText to add pictures and I notice
  this to be a blocking problem in my project.


  An image gets placed on a pdfptable using a PdfPCellEvent subclass.  The
  image has to have restrictions of course so if it is too large it needs to
  be resized.
  So the image gets placed on the document using
  PdfPContentbyteInst.addImage(image,...) (see unit test).
  Of course this addImage() method uses the AffineTransform class from
  java.awt, which might be the culprit, I don't really know.  But in one of my
  outputs one resized picture gets messed up and breaks the document.  Some
  content is placed on the document but it's not always visible and it's
  impossible to print such a document due to "insufficient data for image",
  according to Adobe pdfReader 8.1.

  I'll attach two of my test output pdf files, one with the problem image and
  one with another image that doesn't show the problem.  I'll also attach the
  image that gets broken upon resizing.
  You'll need to add the image to your C:\ (root dir) for this unittest to use
  the pic (obviously).

  public class BanenLadderTest
  {

      private static float MAXIMAGEWIDTH = 10f;

      @Test
      public void testImageResizeProblem() throws FileNotFoundException,
  DocumentException {
          PdfPTable main = new PdfPTable(1);
          main.setTotalWidth(350f);
          main.setSpacingAfter(0);
          main.setSpacingBefore(0);
          main.getDefaultCell().setBorder(Rectangle.NO_BORDER);

          main.setLockedWidth(true);
          for(int i=0; i < 5; i++){
              Paragraph title = new Paragraph(new Phrase(new Chunk("titel
  "+i,FontFactory.getFont(BaseFont.TIMES_ROMAN))));
              Paragraph par = new Paragraph(new Phrase(new Chunk("cell "+i,
  FontFactory.getFont(BaseFont.TIMES_ROMAN))));
              PdfPCell cell = new PdfPCell();
              cell.addElement(title);
              cell.addElement(par);
              Paragraph par2 = new Paragraph(new Phrase(new Chunk("nextline"
  ,FontFactory.getFont(BaseFont.TIMES_ROMAN))));
              applyDefaultCellProps(cell);
              cell.addElement(par2);

              cell.setCellEvent(new
  CellImage("/logo_bedrijf_miniId_32538.jpg"));

              main.addCell(cell);
          }

          Document document = new Document(new Rectangle(350f,
  main.getTotalHeight())); //!!
          File file = new File("c:/tmp/sizeTest.pdf");
          FileOutputStream fos = new FileOutputStream(file);
          PdfWriter writer = PdfWriter.getInstance(document, fos);
          document.setMargins(0f,0f,0f,0f);  
          document.open();

         
  
main.writeSelectedRows(0,main.getRows().size(),0f,main.getTotalHeight(),writer.getDirectContent());
          document.close();
          writer.close();
      }

      private void applyDefaultCellProps(PdfPCell cell){
          cell.setBorder(PdfPCell.NO_BORDER);
          cell.enableBorderSide(Rectangle.LEFT);
          cell.enableBorderSide(Rectangle.RIGHT);
          cell.setPadding(5);
          cell.setPaddingTop(0);
          cell.setPaddingBottom(2);
          cell.setUseAscender(true);
          cell.setUseDescender(true);
      }


      private class CellImage implements PdfPCellEvent {

          private String imageUrl;

          public CellImage(String imageUrl) {
              this.imageUrl = imageUrl;
          }

          public void cellLayout(PdfPCell pdfPCell, Rectangle rectangle,
  PdfContentByte[] pdfContentBytes) {
              if(imageUrl != null && !imageUrl.equals("")) {
                  PdfContentByte cb = pdfContentBytes[PdfPTable.TEXTCANVAS];

                  try {
                      File file = new File(imageUrl);
                      URL url = file.toURI().toURL(); 

                      Image image = Image.getInstance(url);

                      float simulatedImageWidth = image.getWidth();
                      float simulatedImageHeight = image.getHeight();

                      float right;
                      float top;
                      if(simulatedImageWidth > MAXIMAGEWIDTH){
                          simulatedImageWidth = MAXIMAGEWIDTH;
                          simulatedImageHeight =
  berekenHeightMetNieuweBreedte(image.getWidth(),image.getHeight());
                          right = rectangle.getRight() - (simulatedImageWidth
  + pdfPCell.getPaddingRight());
                          top = rectangle.getTop() - (simulatedImageHeight +
  pdfPCell.getPaddingTop() + pdfPCell.getPaddingBottom());
                      }else {
                          right = rectangle.getRight() - (simulatedImageWidth
  + pdfPCell.getPaddingRight());
                          top = rectangle.getTop() - (simulatedImageHeight +
  pdfPCell.getPaddingTop() + pdfPCell.getPaddingBottom());
                      }
                     
  cb.addImage(image,simulatedImageWidth,0,0,simulatedImageHeight,right,top);
                  } catch (DocumentException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
          }

          private float berekenHeightMetNieuweBreedte(float width,float
  height){
              float deler = width / MAXIMAGEWIDTH;
              return height / deler;
          }
      }

  }

  ****  EDIT:  hmm, upon uploading the picture using "insert image" I get
  "Invalid ICC profile data".  This might already mean something to someone
  :-)  After a little googling I understand that it has something to do with
  uncalibrated colours.
  So is there a way for me to check wheather such a picture is being used and
  not insert in my document?

  
http://itext-general.2136553.n4.nabble.com/file/n2332319/logo_bedrijf_miniId_32538.jpg
  logo_bedrijf_miniId_32538.jpg 
  http://itext-general.2136553.n4.nabble.com/file/n2332319/sizeTestProblem.pdf
  sizeTestProblem.pdf 
  http://itext-general.2136553.n4.nabble.com/file/n2332319/sizeTest.pdf
  sizeTest.pdf 







  -- 
  View this message in context: 
http://itext-general.2136553.n4.nabble.com/PdfContentByteInst-addImage-messes-up-jpg-tp2332319p2332319.html
  Sent from the iText - General mailing list archive at Nabble.com.

  ------------------------------------------------------------------------------
  This SF.net email is sponsored by 

  Make an app they can't live without
  Enter the BlackBerry Developer Challenge
  http://p.sf.net/sfu/RIM-dev2dev 
  _______________________________________________
  iText-questions mailing list
  iText-questions@lists.sourceforge.net
  https://lists.sourceforge.net/lists/listinfo/itext-questions

  Buy the iText book: http://www.itextpdf.com/book/
  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/
------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.itextpdf.com/book/
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