java 将 BufferedImage 添加到 PDFBox 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7055485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:28:40  来源:igfitidea点击:

Add BufferedImage to PDFBox document

javajfreechartbufferedimagepdfbox

提问by Oglop

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

在我当前的项目中,我尝试将 a 添加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();
}

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?

任何线索我做错了什么?

采纳答案by Oglop

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.

谢谢你帮我解决垃圾神。昨晚花了几个小时,今天对 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();
}

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

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

回答by Kasas

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

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

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

Without any stream ;)

没有任何流;)

回答by trashgod

Two things stand out:

有两点很突出:

  • Do not swallow exceptions.

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