Antialiasing in pdf export

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
fgeisler
Posts: 4
Joined: Mon Jan 07, 2008 2:04 pm

Antialiasing in pdf export

Post by fgeisler » Mon Jan 07, 2008 2:10 pm

Hello,

I have a problem with antialiasing. When I try to write a chart into a PDF with the jfreechart.draw() method the line in the PDF are not smooths.

Is it possible to have antialiasing in the PDF (using itext) because even with the simplest sample the line are not smooths.

Thanks.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Jan 07, 2008 2:36 pm

Sounds like you are embedding a PNG image in the PDF. You don't have to do that - you can use iText to write PDF (vector) output...see the example in the JFreeChart Developer Guide or Bruno Lowagie's excellent iText in Action.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

fgeisler
Posts: 4
Joined: Mon Jan 07, 2008 2:04 pm

Post by fgeisler » Mon Jan 07, 2008 3:20 pm

unfortunatly even with this simple sample code it doesn't work :

Code: Select all

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYDataset;

import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.FontMapper;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.BaseFont;


public class TestChart {
	/**
	* Saves a chart to a PDF file.
	*
	* @param file The file.
	* @param chart The chart.
	* @param width The chart width.
	* @param height The chart height.
	 * @throws DocumentException 
	*/
	public static void saveChartAsPDF(File file,
		JFreeChart chart,
		int width, int height,
		FontMapper mapper) throws IOException, DocumentException
	{
		OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
		writeChartAsPDF(out, chart, width, height, mapper);
		out.close();
	}
	/**
	* Writes a chart to an output stream in PDF format.
	*
	* @param out The output stream.
	* @param chart The chart.
	* @param width The chart width.
	* @param height The chart height.
	 * @throws DocumentException 
	*/
	public static void writeChartAsPDF(OutputStream out,
		JFreeChart chart,
		int width, int height,
		FontMapper mapper) throws IOException, DocumentException
	{
		Rectangle pagesize = new Rectangle(width, height);
		Document document = new Document(pagesize, 50, 50, 50, 50);
		PdfWriter writer = PdfWriter.getInstance(document, out);
		document.addAuthor("JFreeChart");
		document.addSubject("Demonstration");
		document.open();
		PdfContentByte cb = writer.getDirectContent();
		PdfTemplate tp = cb.createTemplate(width, height);
		Graphics2D g2 = tp.createGraphics(width, height, mapper);
		Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
		chart.draw(g2, r2D, null);
		g2.dispose();
		cb.addTemplate(tp, 0, 0);
		document.close();
	}
	/**
	* Starting point for the demonstration application.
	45
	 * @throws DocumentException 
	*/
	public static void main(String[] args) throws DocumentException {
		try {
		// create a chart...
		
			DefaultCategoryDataset dataset = new DefaultCategoryDataset();
			for(int i = 0 ; i < 30 ; i ++)
			{
		    	dataset.setValue(Math.ceil(Math.random() * 100), "vague1", "tranche_"+i);
		    	dataset.setValue(Math.ceil(Math.random() * 100), "vague2", "tranche_"+i);
			}
		
			
			JFreeChart chart = ChartFactory.createLineChart("PDF Test Chart 1",
					"X", "Y",
					dataset, PlotOrientation.VERTICAL, true, true, false);
			// write the chart to a PDF file...
			File fileName = new File("c:/tmp/pdf/ChartTest.pdf");
			saveChartAsPDF(fileName, chart, 400, 300, new DefaultFontMapper());
		}
		catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
}
The lines in the pdf are not smooth. I try with the lastest version of itext. What can I do ? Maybe it's the best result in pdf that I can obtain ?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Jan 07, 2008 3:30 pm

I ran this and the output looks fine to me. What PDF viewer are you using?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

fgeisler
Posts: 4
Joined: Mon Jan 07, 2008 2:04 pm

Post by fgeisler » Mon Jan 07, 2008 4:02 pm

I'm using Acrobat Reader 5.0. I'm downloading the version 8.1 to test.
Can you send your pdf result to my mail to see if it's a viewer problem or maybe a library problem. (fgeisler@yahoo.fr)

Thanks

fgeisler
Posts: 4
Joined: Mon Jan 07, 2008 2:04 pm

Post by fgeisler » Mon Jan 07, 2008 4:06 pm

Erf, thanks for your advice :)

With Acrobat Reader 8.1, the lines are perfects !

Thanks again.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Mon Jan 07, 2008 4:07 pm

OK. Ignore the e-mail I just sent you.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Javed123
Posts: 7
Joined: Tue Dec 11, 2007 12:14 pm
Location: India

Want Information on Editing Features

Post by Javed123 » Tue Jan 08, 2008 9:11 am

Hi Sir,

Thanks in Advance............

I am using JFreechart in my project. I have some problems in using it.
I am using BarChart to show data but here requirement is that user should be able to edit selected portion of perticular Bar. Say ....I have to change color of selected portion of perticular bar.........

Is it possible to implement these features using JFreeChart latest version?
Is there any guidelines in Latest JFreeChart Developer Guide?

Its very urgent..........Please Reply soon

Regards
Javed

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 08, 2008 9:55 am

You should have started a new thread for this topic, it doesn't have anything to do with the original question.

Do you want to edit the data or just change the colour of the selected bar? If it is the latter, take a look at MouseOverDemo1.java, which is included for download with the JFreeChart Developer Guide.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked