• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Marco Metting
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Drawing the image with PDPageContentStream#drawXObject and setting width in height in this method resolved the issue.
     
    Paul Clapham
    Sheriff
    Posts: 21322
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for following up with the solution, Marco!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic