gpt4 book ai didi

java - PDFBox:PDJpeg 在调整大小时以错误的颜色绘制 -> Adob​​e Reader -> Windows XP & 7

转载 作者:行者123 更新时间:2023-11-30 11:34:10 26 4
gpt4 key购买 nike

使用 PDFBox 创建 PDF 并向其绘制 PDJpeg 时,在将 PDJpeg 绘制到 PDF 之前调整 PDJpeg 的大小时,图像的颜色会发生变化/反转。此问题仅在 Windows XP 和 Windows 7 上可见,例如使用Adobe 阅读器。 Mac 上的预览或 Windows 8 中新的 PDF 预览构建不受此影响。

示例:

Screenshot PDF in Adobe Reader
Same PDF in Mac Preview

这是我在代码中所做的:

  • 创建 PDDocument
  • 创建 PDJpegs 的 HashMap(用于缓存目的):
    • 通过 ImageIO.read() 初始化的 BufferedImage 创建 PDJpegs
    • 通过在 PDJpeg 上调用 setHeight() 和 setWidth() 来调整 PDJpeg 的大小
    • 将 PDJpeg 添加到 HashMap
  • 创建PDPage并将其添加到PDDocument
  • 创建 PDPageContentStream
  • 在PDPa​​ge上画一些PDJpegs
  • 关闭 PDPageContentStream
  • 保存 PDDocument
  • 关闭PDDocument

调整 PDJpeg 大小的方法:



private void preparePDFIconCache(List
    
      list) 抛出 IOException {
     

iconCache = new HashMap ();

对于(抽象数据项项目:列表){
String iconResourcePath = "/com/graphics/icons/"+ item.getIconName();
网址 iconURL = this.getClass().getResource(iconResourcePath);

BufferedImage 图标 = null;
如果(iconURL != null){

icon = ImageIO.read(iconURL);

} 别的 {

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

文件图标文件 = 新文件(myIconResourcePath);
如果(iconFile.exists()){

网址 myIconURL = iconFile.toURI().toURL();

如果(myIconURL!= null){

icon = ImageIO.read(myIconURL);
}
}

}
如果(图标!= null){

PDJpeg pdfIcon = new PDJpeg(currentDocument, icon);

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

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

如果在初始化 PDJpeg 之前调整 BufferedImage 的大小,则一切正常,但它们看起来不那么清晰。

有没有人有好的解决方案或遇到过同样的问题?

最佳答案

使用 PDPageContentStream#drawXObject 绘制图像并在此方法中设置宽度高度解决了这个问题。

关于java - PDFBox:PDJpeg 在调整大小时以错误的颜色绘制 -> Adob​​e Reader -> Windows XP & 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15789954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com