繁体   English   中英

使用PDFBox将图像写入pdf,发现左上角有一些奇怪的像素

[英]Write image to pdf with PDFBox, found there are some strange pixels on the left top corner

我正在使用PDFBox将图像写入pdf。 图像只是一个纯红色矩形。

图像是:

在此输入图像描述

我的代码是:

PDDocument doc = new PDDocument();
PDRectangle pageSize = new PDRectangle(CARD_WIDTH, CARD_HEIGHT);
PDPage page1 = new PDPage(pageSize);
doc.addPage(page1);

File imageFile = getRedImageFile();
PDXObjectImage pdImage = new PDPixelMap(doc, ImageIO.read(imageFile));

// write front image
PDPageContentStream contentStream = null;
try {
    contentStream = new PDPageContentStream(doc, page1);
    pdImage = parseImage(backImage, doc);
    contentStream.drawXObject(pdImage, 0, 0, CARD_WIDTH / 2, CARD_HEIGHT / 2);
} finally {
    if (contentStream != null) {
        contentStream.close();
    }
}

最终图片是:

在此输入图像描述

目前尚不清楚,所以我将其放大并上传另一个:

在此输入图像描述

为什么那里有奇怪的像素?

这是由于内容流错误造成的。 您可以通过关闭然后在写入图像后再次打开流来绕过它,但这可能会导致更多错误(例如覆盖所有字体)。

我使用的解决方法是使用页面右侧50000像素的占位符图像启动每个页面。 由于这个图像被破坏而不是其他图像,一切都很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM