将 BufferedImage 添加到 PDFBox 2.0 文档

Add BufferedImage to PDFBox 2.0 document

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

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

PDFBox 已经排除了 PDJpeg class 和整个 xobject 部分。

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

PDFBox has since excluded the PDJpeg class and the xobject section as a whole.

在版本 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)