将 BufferedImage 添加到 PDFBox 文档

发布于 2024-11-29 09:00:12 字数 783 浏览 3 评论 0原文

在我当前的项目中,我尝试将 BufferedImage 添加到 PDFBox 文档中。更具体地说,我使用来自 JFreeChart 的图像。我的代码如下所示:

public void exportToPDF(JFreeChart chart, String filePath){
    PDDocument doc = null;
    PDPage page = null;
    PDXObjectImage ximage = null;

    try {
        doc = new PDDocument();
        page = new PDPage();
        doc.addPage(page);
        PDPageContentStream content = new PDPageContentStream(doc, page);
        BufferedImage image = chart.createBufferedImage(300, 300);
        ximage = new PDJpeg(doc, image);
        content.drawImage(ximage, 20, 20);
        content.close();
    } catch(IOException ie) {
    }
    doc.save(filePath);
    doc.close();
}

文档被创建;我可以添加文本,但收到一条错误消息,指出图像没有足够的信息可显示。

有什么线索表明我做错了什么吗?

In my current project, I try to add a BufferedImage to a PDFBox document. More specificly, I use an image from a JFreeChart. My code looks like this:

public void exportToPDF(JFreeChart chart, String filePath){
    PDDocument doc = null;
    PDPage page = null;
    PDXObjectImage ximage = null;

    try {
        doc = new PDDocument();
        page = new PDPage();
        doc.addPage(page);
        PDPageContentStream content = new PDPageContentStream(doc, page);
        BufferedImage image = chart.createBufferedImage(300, 300);
        ximage = new PDJpeg(doc, image);
        content.drawImage(ximage, 20, 20);
        content.close();
    } catch(IOException ie) {
    }
    doc.save(filePath);
    doc.close();
}

The document gets created; I can add text, but I get an error stating the the image does not have enough information to be shown.

Any clue to what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱*していゐ 2024-12-06 09:00:12

谢谢你帮我摆脱垃圾之神。昨晚和今天花了几个小时对 PipedIn/OutStreams 感到困惑。想不通。不过,我让它工作了。事实证明这根本不是很困难。

public void exportToPDF(JFreeChart chart, String filePath){
    PDDocument doc = null;
    PDPage page = null;
    PDXObjectImage ximage = null;
    try {
        doc = new PDDocument();
        page = new PDPage();
        doc.addPage(page);
        PDPageContentStream content = new PDPageContentStream(doc, page);

        //create a new outStream
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ChartUtilities.writeChartAsJPEG(out, chart, 300, 300);//write to outstream
        //create a new inputstream
        InputStream in = new ByteArrayInputStream(out.toByteArray());
        ximage = new PDJpeg(doc, in);
        content.drawImage(ximage, 5, 300);
        content.close();
    }
    catch (IOException ie){
        //handle exception
    }
    //save and close
    doc.save(filePath);
    doc.close();
}

我确信这可以做得更好,但它确实有效。

Thanks for helping me out trashgod. Spent last night and a few hours today beeing confused about PipedIn/OutStreams. Can´t figure it out. However, i got it to work. Turns out it wasn´t very difficult at all.

public void exportToPDF(JFreeChart chart, String filePath){
    PDDocument doc = null;
    PDPage page = null;
    PDXObjectImage ximage = null;
    try {
        doc = new PDDocument();
        page = new PDPage();
        doc.addPage(page);
        PDPageContentStream content = new PDPageContentStream(doc, page);

        //create a new outStream
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ChartUtilities.writeChartAsJPEG(out, chart, 300, 300);//write to outstream
        //create a new inputstream
        InputStream in = new ByteArrayInputStream(out.toByteArray());
        ximage = new PDJpeg(doc, in);
        content.drawImage(ximage, 5, 300);
        content.close();
    }
    catch (IOException ie){
        //handle exception
    }
    //save and close
    doc.save(filePath);
    doc.close();
}

I´m sure this can be done better but it works.

夏天碎花小短裙 2024-12-06 09:00:12

有一种简单的方法可以使用 pdfbox 将 JFreeChart 插入 pdf:

BufferedImage bufferedImage = source.getChart().createBufferedImage(source.getWidth(),
        source.getHeight(), BufferedImage.TYPE_INT_RGB, null);
PDXObjectImage ximage = new PDJpeg(doc, bufferedImage);

没有任何流;)

There is an easy way to insert a JFreeChart into a pdf with pdfbox:

BufferedImage bufferedImage = source.getChart().createBufferedImage(source.getWidth(),
        source.getHeight(), BufferedImage.TYPE_INT_RGB, null);
PDXObjectImage ximage = new PDJpeg(doc, bufferedImage);

Without any stream ;)

独夜无伴 2024-12-06 09:00:12

有两点值得注意:

Two things stand out:

  • Do not swallow exceptions.

  • Do use ChartUtilities to render the image in a suitable format, as suggested here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文