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 3616708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:31:55+00:00 2026-05-18T22:31:55+00:00

I am trying to set position to image which i added to PDF but

  • 0

I am trying to set position to image which i added to PDF but it always positions to 0,0.
I searched a lot but could not find the solution. I think i could not understand well about positioning.

Here is the code that always postions to 0,0 but it should be 200,300!

Thanks a lot for your help,

DefaultPieDataset dataset = new DefaultPieDataset();

dataset.setValue(String.format("%s, %s", "pie1", "pie1"),20);
dataset.setValue(String.format("%s, %s", "pie2", "pie2"),80);

JFreeChart chart = ChartFactory.createPieChart("testPie", dataset, true, true, false);

Document document = new Document();
document.addCreationDate();
document.setPageSize(PageSize.A4);

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf")); 
document.open(); 

PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(300, 300); 
Graphics2D g2 = cb.createGraphics(300, 300, new DefaultFontMapper()); 

Rectangle2D r2D = new Rectangle2D.Double(0, 0, 300, 300); 
chart.draw(g2, r2D, null); 
g2.dispose(); 
cb.addTemplate(tp, 200, 300); 
document.close(); 
  • 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-18T22:31:56+00:00Added an answer on May 18, 2026 at 10:31 pm

    Your template is empty… you’re getting the PdfGraphics2D directly from the writer’s direct content (CB.createGraphics instead of what you probably intended, TP.createGraphics).

    There are several solutions:

    option 1: get the Graphics2D from the template

     Graphics2D g2 = tp.createGraphics(...)
    

    option 2: ditch the template, move the chart directly in the contentByte. The graphics2D interface is a bit clunky, so you should generally prefer to do things directly in the contentByte whenever possible. It works fine, but the content stream it builds isn’t as efficient as it could be. In this particular case, I don’t think it’ll matter, but that’s a good rule of thumb.

     cb.saveState();
     cb.concatMatrix(1, 0, 0, 1, 200, 300);
     Graphics2D g2 = cb.createGraphics(...);
     ...
     g2.dispose();
     cb.restoreState();
     document.close();
    

    Option three: Ditch the template and move the chart from within the Graphics2D instance:

     g2.transform(AffineTransform.getTranslateInstance(200, 300));
     chart.draw(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to set the caret position in a contenteditable div layer, and after
I am trying to absolutely position a TinyMCE editor at a set position using
I am trying to set my gridview (asp.net) position to absolute when the gridview
I am trying to set the Media Elements position by following code: MediaElement musicPlayer
I am having a real headache trying to set a node's local position to
I'm trying to place a button. I have its position set to absolute, so
I'm trying to set my root LinearLayout element (which is set to fill_parent in
I'm trying to animate a background image to move down a set amount of
I am trying to render a set of photos, positioned in 3-dimensional space, which
I'm trying to position two TextView's to the right of an image view (note

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.