java - 将 BufferedImage 添加到 PDFBox 2.0 文档

标签 java jfreechart bufferedimage pdfbox

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

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

PDFBox 已经从整体上排除了 PDJpeg 类和 xobject 部分。

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

最佳答案

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

在版本 2 的开发过程中确实进行了大量的重构(以及重新重构和重新重构等),并且这种重构通常不仅仅是包更改。很多时候某些功能现在在哪里并不明显。

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

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

/**
 * 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)

关于java - 将 BufferedImage 添加到 PDFBox 2.0 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36367622/

相关文章:

java - 如何将 jfree 图表作为小程序运行?

java - 如何在 jFreeChart TimeSeries 中开始和结束值 "0"的每个系列?

java - 加载 bufferedImage 会禁用 JPanel 的 PaintCompontent 方法

java - 将日期字符串与时区进行比较

java - 调度 Swingworker 线程

java - 通缉 : Recurrence Formula of In-Order binary tree output method

Java : Downloading file from a remote location using sftp

java - 如何在饼图图例中仅包含每个类别的详细描述?

java - 保存使用graphics2d、java绘制的缓冲图像

java - 在 BufferedImage 的 int[] 像素数组中工作