JFreeChart存储为PDF,支持中文显示

来源:互联网 发布:网络运营工作计划 编辑:程序博客网 时间:2024/04/29 16:21
 
下面的代码是从jfreechart的Demo中复制后做了修改,demo中的代码存储pdf时,中文文字不能显示,解决的办法是将
   //DefaultFontMapper mapper = new DefaultFontMapper();语句替换为
   AsianFontMapper mapper = new AsianFontMapper("STSong-Light","UniGB-UCS2-H");
注意在工程中要有iTextAsian.jar包,下面是能生成中文PDF的demo代码:
package demo.pdf;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.SeriesException;
import org.jfree.data.time.Day;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.AsianFontMapper;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.FontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;
//图表存为pdf时支持中文显示
/**
* A simple demonstration showing how to write a chart to PDF format using
* JFreeChart and iText.
* <P>
* You can download iText from http://prdownloads.sourceforge.net/itext/iTextAsian.jar.
*/
public class PDFExportZH {
/**
  * 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.
  */
public static void saveChartAsPDF(File file, JFreeChart chart, int width,
   int height, FontMapper mapper) throws IOException {
  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.
  *
  */
public static void writeChartAsPDF(OutputStream out, JFreeChart chart,
   int width, int height, FontMapper mapper) throws IOException {
  Rectangle pagesize = new Rectangle(width, height);
  Document document = new Document(pagesize, 50, 50, 50, 50);
  try {
   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);
   g2.dispose();
   cb.addTemplate(tp, 0, 0);
  } catch (DocumentException de) {
   System.err.println(de.getMessage());
  }
  document.close();
}
public static void main(String[] args) throws DocumentException {
  try {
   // create a chart...
   TimeSeries series = new TimeSeries("随机数据");
   Day current = new Day(1, 1, 2000);
   double value = 100.0;
   for (int i = 0; i < 1000; i++) {
    try {
     value = value + Math.random() - 0.5;
     series.add(current, new Double(value));
     current = (Day) current.next();
    } catch (SeriesException e) {
     System.err.println("Error adding to series");
    }
   }
   XYDataset data = new TimeSeriesCollection(series);
   JFreeChart chart = ChartFactory.createTimeSeriesChart("Test测试",
     "日期", "值", data, true, false, false);
        String text="你好!";
   Font font = new Font("SansSerif", Font.PLAIN, 12);
   TextTitle subtitle = new TextTitle(text, font);
   chart.addSubtitle(subtitle);
   // DefaultFontMapper mapper = new DefaultFontMapper();
   AsianFontMapper mapper = new AsianFontMapper("STSong-Light",
     "UniGB-UCS2-H");
   mapper.insertDirectory("C://WINDOWS//Fonts");
   File fileName = new File("d://temp//jfreechartZH.pdf");
   saveChartAsPDF(fileName, chart, 400, 300, mapper);
   System.out.println("finished!");
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
}
}
原创粉丝点击