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());
}
}
}
//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());
}
}
}
- JFreeChart存储为PDF,支持中文显示
- JFreechart存储为PDF不能显示中文文字的解决办法
- JFreechart存储为PDF不能显示中文文字的解决办法
- JFreechart存储为PDF不能显示中文文字的解决办法
- JFreechart存储为PDF不能显示中文文字的解决办法
- 一劳永逸让jfreechart支持中文显示
- jfreeChart中文显示为方框解决办法
- ITEXT-PDF彩色字体显示-支持中文
- JFreeChart中文支持
- jfreechart一&二维报表导出为word&pdf&html-中文乱码和字体模糊问题(一)
- iReport与JasperReport中如何支持pdf中文显示
- jfreechart一&二维报表导出为word&pdf&html-报表上显示百分比或数字(二)
- jFreeChart中文显示成乱码
- Linux下的JFreeChart中文乱码解决(图片生成后中文显示为“口”)
- Linux下的JFreeChart中文乱码解决(图片生成后中文显示为“口”)
- Linux下的JFreeChart中文乱码解决(图片生成后中文显示为“口”)
- 使用TCPDF生成pdf,中文显示为问号
- PDF的显示有了头绪了。现在可以把PDF转化成BMP了。支持中文
- 软件技术术语和词典
- dos、批处理、学习
- 简易的JS验证FORM表单特殊字符,防范SQL语句漏洞
- 用spring MVC 生成Excel和PDF
- 将SQL Server2005设置成可在网络中访问
- JFreeChart存储为PDF,支持中文显示
- 图片文件合法性校验
- C#_如何在TextBox获得焦点的时候,将焦点放在textBox文字的最后
- 恢复weblogic密码
- 怎么一时这么多人加我???
- 有活抢着干未必是好事,那要分是什么事情
- NTFS文件格式
- log4j日志使用详解
- 世界品牌价值实验室:锻造一把国美“屠刀”