Printing a JChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
flyby11
Posts: 2
Joined: Mon Jun 20, 2005 9:42 pm

Printing a JChart

Post by flyby11 » Tue Jun 21, 2005 2:26 pm

I was wondering if anybody could tell me how I would print a chart that I created. Should I use the print class that java has available or is there something better with JFreeChart. Any direction and/or code would be very useful. Thanks

iceox
Posts: 5
Joined: Fri Apr 08, 2005 4:30 pm

Post by iceox » Tue Jun 21, 2005 4:19 pm

have you consider using iText alone with jFreechart to make it a pdf for user to print?

flyby11
Posts: 2
Joined: Mon Jun 20, 2005 9:42 pm

Post by flyby11 » Tue Jun 21, 2005 7:38 pm

No I haven't considered that. In fact I didn't even know that was possible. I've been going through the jfreechart source code but there are so many programs. I have never heard of iText either. Is there anyway someone could let me in on the secret? Thanks

iceox
Posts: 5
Joined: Fri Apr 08, 2005 4:30 pm

Post by iceox » Tue Jun 21, 2005 8:15 pm

with iText to PDF:

1. read http://www.jfree.org/phpBB2/viewtopic.p ... echart+jsp

I have learned alot from that

1. Modify web.xml
2. test out following code just by iText.jar by itself. it works cause i tested yesterday.

create servlet:

Code: Select all

// http://www.softhouse.com.cn/html/200410/2004102610123400001359.html

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public void doGet(HttpServletRequest request,
                               HttpServletResponse response)
 throws IOException,ServletException
{
  Document document = new Document(PageSize.A4, 36,36,36,36);
  ByteArrayOutputStream ba = new ByteArrayOutputStream();
  try
  {
     PdfWriter writer = PdfWriter.getInstance(document, ba);
     document.open();
     document.add(new Paragraph("Hello World"));
  }
  catch(DocumentException de)
  {
    de.printStackTrace();
    System.err.println("A Document error:" +de.getMessage());
   }
  document.close();
  response.setContentType("application/pdf");
  response.setContentLength(ba.size());
  ServletOutputStream out = response.getOutputStream();
  ba.writeTo(out);
  out.flush();
}

3. Combine iText & jfreechart using pmarsollier' s codes for PDF, and read thoughly of what psupa wrote.

4. in jsp, all i did, was just using a jsp:forward to my servlet page, and it worked.

Locked