mmetting
mmetting

Reputation: 91

PDFBox: PDJpeg gets drawn in wrong colors when resized -> Adobe Reader -> Windows XP & 7

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.

Samples:

Screenshot PDF in Adobe Reader
Same PDF in Mac Preview

This is what I do in code:

Method for resizing the PDJpegs:

private void preparePDFIconCache(List<AbstractDataItem> list) throws IOException {

    iconCache = new HashMap<String, PDJpeg>();

    for (AbstractDataItem item : list) {
        String iconResourcePath = "/com/graphics/icons/" + item.getIconName();
        URL iconURL = this.getClass().getResource(iconResourcePath);

        BufferedImage icon = null;
        if (iconURL != null) {

            icon = ImageIO.read(iconURL);

        } else {

            String myIconResourcePath = SettingsDataModel.getInstance().getMyIconsPath() + File.separator + item.getIconName();

            File iconFile = new File(myIconResourcePath);
            if (iconFile.exists()) {

                URL myIconURL = iconFile.toURI().toURL();

                if (myIconURL != null) {

                    icon = ImageIO.read(myIconURL);
                }
            }

        }
        if (icon != null) {

            PDJpeg pdfIcon = new PDJpeg(currentDocument, icon);

            pdfIcon.setHeight(iconWidthXHeight);
            pdfIcon.setWidth(iconWidthXHeight);

            iconCache.put(item.getIconName(), pdfIcon);
        }
    }
}

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?

Upvotes: 3

Views: 1807

Answers (1)

mmetting
mmetting

Reputation: 91

Drawing the image with PDPageContentStream#drawXObject and setting width in height in this method resolved the issue.

Upvotes: 1

Related Questions