aspose file tools*
The moose likes Other Open Source Projects and the fly likes PDFBox: PDJpeg gets drawn in wrong colors when resized -> Adobe Reader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "PDFBox: PDJpeg gets drawn in wrong colors when resized -> Adobe Reader" Watch "PDFBox: PDJpeg gets drawn in wrong colors when resized -> Adobe Reader" New topic
Author

PDFBox: PDJpeg gets drawn in wrong colors when resized -> Adobe Reader

Marco Metting
Greenhorn

Joined: Apr 11, 2013
Posts: 3
When creating a PDF using PDFBox and drawing PDJpegs to it, the images' colors are changed / inverted when resizing the PDJpeg(s) before drawing them to the PDF. This issue is only visible on Windows XP and Windows 7 using e.g. Adobe Reader. Preview on Mac or the new PDF preview build in Windows 8 are somehow not affected by this.

I tried it with PNGs and JPEGs, Colorspaces sRGB and CMYK, with and without alpha channels. But the issue is the same with all image types.

Samples:

Screenshot PDF in Adobe Reader
Same PDF in Mac Preview

This is what I do in code:

  • Create PDDocument
  • Create HashMap of PDJpegs (for caching purpose):
  • - Create PDJpegs by BufferedImage initialised through ImageIO.read()
  • - Resize PDJpegs by calling setHeight() and setWidth() on PDJpegs
  • - Adding PDJpeg to HashMap
  • Create PDPage and add it to PDDocument
  • Create PDPageContentStream
  • Draw some PDJpegs on the PDPage
  • Close PDPageContentStream
  • Save PDDocument
  • Close PDDocument


  • Method for resizing the PDJpegs:



    If the BufferedImages are resized before initialising the PDJpegs, everything works fine, but they don't look that sharp.

    Does anyone have a good solution or experienced the same issue?
    Marco Metting
    Greenhorn

    Joined: Apr 11, 2013
    Posts: 3
    Drawing the image with PDPageContentStream#drawXObject and setting width in height in this method resolved the issue.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18907
        
        8

    Thanks for following up with the solution, Marco!
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: PDFBox: PDJpeg gets drawn in wrong colors when resized -> Adobe Reader