Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6324397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:41:45+00:00 2026-05-24T16:41:45+00:00

In my current project, I try to add a BufferedImage to a PDFBox document.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:41:46+00:00Added an answer on May 24, 2026 at 4:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)
My current project involves writing Perl code inside a Solaris VMWare appliance (hosted on
I reference another eclipse project from the current project I am working on. Everything
in my current project I'm using code first approach. I have a type called
my current project is based on Entity Framwork code-first. I have three types: Task,
I ran 'analyze in xcode on a current iOS project to try to track
In my current project I see a lot of this type of code: public
My current project uses NUnit for unit tests and to drive UATs written with
My current project involves deploying an upgraded .exe file that runs as a Windows
My current project is broken down into 3 parts: Website, Desktop Client, and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.