【问题标题】:Add BufferedImage to PDFBox 2.0 document将 BufferedImage 添加到 PDFBox 2.0 文档
【发布时间】:2016-04-01 23:55:49
【问题描述】:

第一次发帖,多多包涵……

我有两个问题。首先,我想知道如何使用 BufferedImage 将图像添加到 PDFBox 2.0 文档。问题已在这里提出:Add BufferedImage to PDFBox document

PDFBox 此后将 PDJpeg 类和 xobject 部分作为一个整体排除在外。

其次,如果有人已经问过这个问题并且已经回答了,但该答案已被弃用;更新/连接这两个问题的最佳方法是什么? (我没有任何意见,所以我无法评论)。

【问题讨论】:

标签: java jfreechart bufferedimage pdfbox


【解决方案1】:

PDFBox 已将 PDJpeg 类和 xobject 部分作为一个整体排除在外。

在版本 2 的开发过程中确实有很多重构(以及重构和重构等),而且这种重构通常超出了单纯的包更改。而且很多时候,某些功能现在在哪里并不明显。

但是像向文档中添加BufferedImage 这样的基本功能可以指望不会丢失。

现在有 JPEGFactory,它提供了从 BufferedImage 创建图像 XObject 的方法,特别是:

/**
 * Creates a new JPEG Image XObject from a Buffered Image.
 * @param document the document where the image will be created
 * @param image the buffered image to embed
 * @return a new Image XObject
 * @throws IOException if the JPEG data cannot be written
 */
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image)

/**
 * Creates a new JPEG Image XObject from a Buffered Image and a given quality.
 * The image will be created at 72 DPI.
 * @param document the document where the image will be created
 * @param image the buffered image to embed
 * @param quality the desired JPEG compression quality
 * @return a new Image XObject
 * @throws IOException if the JPEG data cannot be written
 */
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality)

/**
 * Creates a new JPEG Image XObject from a Buffered Image, a given quality and DPI.
 * @param document the document where the image will be created
 * @param image the buffered image to embed
 * @param quality the desired JPEG compression quality
 * @param dpi the desired DPI (resolution) of the JPEG
 * @return a new Image XObject
 * @throws IOException if the JPEG data cannot be written
 */
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality, int dpi)

【讨论】:

  • 非常感谢!如果有人偶然发现并想知道 PNG 版本:PDImageXObject pdImage= LosslessFactory.createFromImage(doc, bufferedImage);
猜你喜欢
  • 2011-10-26
  • 2013-05-12
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 2013-01-19
相关资源
最近更新 更多