Export to PDF...

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
David Gilbert

Export to PDF...

Post by David Gilbert » Fri Apr 12, 2002 10:59 am

For those interested in exporting charts to Acrobat PDF format, developer Jim Moore has kicked off development of a PDFGraphics2D class using iText. In a very short space of time, he and a few other developers have got something that works pretty well, and it looks like this class will be a part of the standard iText package shortly. As soon as that happens, I will add a demo application to the JFreeChart package that shows how to use JFreeChart with iText.

iText is at: http://www.lowagie.com/iText

By the way, PDFGraphics2D can be used with any code that currently uses Graphics2D (as JFreeChart does), so it will be a pretty useful piece of code for a wide range of applications.

Regards,

DG.

Pradheep

Pls send a sample program to convert in to pdf

Post by Pradheep » Thu Dec 19, 2002 6:15 am

I am a student exploring JFreeChart and i want to convert char t in to pdf. pls reply soon.
with subject : Chart to PDF


expecting your reply

pradheept@hotmail.com
tpradheep@yahoo.com

mhilpert
Posts: 497
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Post by mhilpert » Thu Apr 24, 2003 4:43 pm

Based on David's Demonstration application, here's a method to save a JFreeChart as a PDF file:

Code: Select all

/**
 * Save chart as PDF file. Requires iText library.
 * 
 * @param chart JFreeChart to save.
 * @param fileName Name of file to save chart in.
 * @param width Width of chart graphic.
 * @param height Height of chart graphic.
 * @throws Exception if failed.
 * @see <a href="http://www.lowagie.com/iText">iText</a>
 */
public void saveChartToPDF(JFreeChart chart, String fileName, int width, int height) throws Exception {
    if (chart != null) {
        BufferedOutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(fileName)); 
                
            //convert chart to PDF with iText:
            Rectangle pagesize = new Rectangle(width, height); 
            com.lowagie.text.Document document = new com.lowagie.text.Document(pagesize, 50, 50, 50, 50); 
            try { 
                PdfWriter writer = PdfWriter.getInstance(document, out); 
                document.addAuthor("JFreeChart"); 
                document.open(); 
        
                PdfContentByte cb = writer.getDirectContent(); 
                PdfTemplate tp = cb.createTemplate(width, height); 
                Graphics2D g2 = tp.createGraphics(width, height, new DefaultFontMapper()); 
        
                Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, eight); 
                chart.draw(g2, r2D, null); 
                g2.dispose(); 
                cb.addTemplate(tp, 0, 0); 
            } finally {
                document.close(); 
            }
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }//else: input values not availabel
}//saveChartToPDF()
Have fun! :P

lucas luky
Posts: 14
Joined: Wed Sep 12, 2007 7:05 pm

Post by lucas luky » Tue Sep 18, 2007 8:57 pm

ok, but what if i already have a .jasper template and just want to insert the image inside it with HIGH resolution?

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Post by jwenting » Wed Sep 19, 2007 1:24 pm

create the chart itself at a large pixel size. That's got nothing at all to do with the topic of this 5 year dead post you revived to hijack it.

lucas luky
Posts: 14
Joined: Wed Sep 12, 2007 7:05 pm

found solution.

Post by lucas luky » Wed Sep 19, 2007 8:11 pm

jwenting,

i think i found a better solution:

Code: Select all

JRAbstractRenderer jfcRenderer = new JFreeChartRenderer(chart);
it renders the graphic as a vector image. now my graphics looks pretty on pdf :D

thanks for your reply.

Locked