项目要求,没办法,生成的jsp页面要打印。
于是要生成pdf大概查了一下itext主站的实例如下 :
/**/
/*
* $Id: JFreeChartExample.java 1778 2005-06-02 11:05:39Z blowagie $
* $Name$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* itext-questions@lists.sourceforge.net
*/
package
com.lowagie.examples.directcontent.graphics2D;
import
java.awt.Graphics2D;
import
java.awt.geom.Rectangle2D;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
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.general.DefaultPieDataset;
import
org.jfree.data.xy.XYSeries;
import
org.jfree.data.xy.XYSeriesCollection;
import
com.lowagie.text.Document;
import
com.lowagie.text.DocumentException;
import
com.lowagie.text.Rectangle;
import
com.lowagie.text.pdf.DefaultFontMapper;
import
com.lowagie.text.pdf.PdfContentByte;
import
com.lowagie.text.pdf.PdfTemplate;
import
com.lowagie.text.pdf.PdfWriter;

/** */
/**
* JFreeChart example.
*/

public
class
JFreeChartExample
...
{

/** *//**
* Creates some PDFs with JFreeCharts.
* @param args no arguments needed
*/
public static void main(String[] args) ...{
System.out.println("JFreeChart example");
/** *//** the following line is a workaround for JDK 1.5 (fix by Adriaan Joubert) */
org.jfree.text.TextUtilities.setUseDrawRotatedStringWorkaround(false);
convertToPdf(getBarChart(), 400, 600, "barchart.pdf");
convertToPdf(getPieChart(), 400, 600, "piechart.pdf");
convertToPdf(getXYChart(), 400, 600, "xychart.pdf");
}

/** *//**
* Converts a JFreeChart to PDF syntax.
* @param filename the name of the PDF file
* @param chart the JFreeChart
* @param width the width of the resulting PDF
* @param height the height of the resulting PDF
*/
public static void convertToPdf(JFreeChart chart, int width, int height, String filename) ...{
// step 1
Document document = new Document(new Rectangle(width, height));
try ...{
// step 2
PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
}
catch(DocumentException de) ...{
de.printStackTrace();
}
catch (FileNotFoundException e) ...{
e.printStackTrace();
}
// step 5
document.close();
}

/** *//**
* Gets an example barchart.
* @return a barchart
*/
public static JFreeChart getBarChart() ...{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.setValue(40, "hits/hour", "index.html");
dataset.setValue(20, "hits/hour", "download.html");
dataset.setValue(15, "hits/hour", "faq.html");
dataset.setValue(8, "hits/hour", "links.html");
dataset.setValue(31, "hits/hour", "docs.html");
return ChartFactory.createBarChart("Popularity of iText pages",
"Page", "hits/hour", dataset,
PlotOrientation.VERTICAL, false, true, false);
}

/** *//**
* Gets an example piechart.
* @return a piechart
*/
public static JFreeChart getPieChart() ...{
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("iText", 60);
dataset.setValue("cinema.lowagie.com", 10);
dataset.setValue("tutorial", 30);
return ChartFactory.createPieChart(
"Website popularity",
dataset,
true,
true,
false);
}

/** *//**
* Gets an example XY chart
* @return an XY chart
*/
public static JFreeChart getXYChart() ...{
XYSeries series = new XYSeries("XYGraph");
series.add(1, 5);
series.add(2, 7);
series.add(3, 3);
series.add(4, 5);
series.add(5, 4);
series.add(6, 5);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series);
return ChartFactory.createXYLineChart(
"XY Chart", "X-axis", "Y-axis", dataset,
PlotOrientation.VERTICAL, true, true, false);
}
}
不过这里遇到了一些问题。一个是日文显示的问题。一个是分页后图片坐标的处理:
生成Chart 之前的一个blog有记载。直接copy。生成了之后关键是如何把它加载在pdf文件里。
Map tempmap
=
new
HashMap();
tempmap.put(
"
_LocaleUtil
"
,
new
LocaleUtil(Locale.JAPANESE));
MessageUtil mu
=
new
MessageUtil(
tempmap,
"
jp.co.uss.cares100.message.bill.CaresRcdWeekPdf
"
);
//
pdfマージン 取得
int
marginleft
=
Integer.parseInt(mu.get(
"
marginleft
"
));
int
marginright
=
Integer.parseInt(mu.get(
"
marginright
"
));
int
margintop
=
Integer.parseInt(mu.get(
"
margintop
"
));
int
marginbottom
=
Integer.parseInt(mu.get(
"
marginbottom
"
));
String pageSize
=
mu.get(
"
pageSize
"
);
SimpleDateFormat df
=
new
SimpleDateFormat(
"
yyyyMMddHHmmss
"
);
//
厳密な日付判定
df.setLenient(
false
);
String now
=
df.format(
new
Date());
String fileName
=
PDF_FILE_PREFIX
+
now
+
"
.pdf
"
;
DateFormat dateFormat
=
DateFormat.getDateTimeInstance(
DateFormat.MEDIUM,
DateFormat.MEDIUM,
Locale.JAPAN);
res.reset();
res.setContentType(
"
application/pdf
"
);
res.setHeader(
"
Content-Disposition
"
,
"
inline; filename="
"
+
fileName
+
"
"
"
);
res.setHeader(
"
Cache-Control
"
,
""
);
res.setHeader(
"
Pragma
"
,
""
);
BufferedOutputStream bufOutStream
=
new
BufferedOutputStream(res.getOutputStream());
Document document;
//
ページサイズを設定する

if
(pageSize.equalsIgnoreCase(
"
A4
"
))
...
{
document =
new Document(
PageSize.A4,
marginleft,
marginright,
margintop,
marginbottom);
// ページサイズを設定する 
}
else
...
{
document =
new Document(
PageSize.A3,
marginleft,
marginright,
margintop,
marginbottom);
}
PdfWriter pdfwriter
=
PdfWriter.getInstance(document, bufOutStream);
//
ドキュメントオープン
document.open();
//
加载一个封面
document.add(CaresRecordDayPdfProcess.getFaceTable(req,document,(List)map.get(
"
baseInfoList
"
)));
document.newPage();
//
加载头信息
document.add(
getHeadTable(
map.get(
"
userName
"
).toString(),
map.get(
"
baseInfo
"
).toString(),
map.get(
"
dateInfo
"
).toString()));
document.add(getPhrase(
"
● 介護記録
"
));
//
加载数据table
document.add(
getDataTable(
(List) map.get(
"
title1List
"
),
(List) map.get(
"
data1List
"
)));
//
compute the height of the chart
int
height
=
405
, dataCount
=
0
;
//
调节chart图片的显示Y坐标
dataCount
=
((List) map.get(
"
data1List
"
)).size();
height
=
height
-
dataCount
*
20
;
//
table的row高度,每加一row那么图片坐标就要低20

while
(
true
)
...
{//分页后应该显示的Y坐标,A4纸的高度是800
if(height < 0)...{
height += 40*20;
}else...{
if(height > 500)...{
document.newPage();
height = 510;
}
height -= 10;
break;
}
}
document.add(getPhrase(
"
● バイタル
"
));
PdfContentByte cb
=
pdfwriter.getDirectContent();
PdfTemplate tp
=
cb.createTemplate(
500
,
300
);
//
这里很重要,解决了东方语言显示的问题。
Graphics2D g2d
=
tp.createGraphics(
500
,
300
,
new
AsianFontMapper(
AsianFontMapper.JapaneseFont_Min,
AsianFontMapper.JapaneseEncoding_H));
Rectangle2D r2d
=
new
Rectangle2D.Double(
0
,
0
,
500
,
300
);
JFreeChart chart
=
getChart(map);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp,
48
, height);
//
ドキュメントクローズ
document.close();
bufOutStream.flush();
bufOutStream.close();
关键是以下几段代码:
a)生成pdf

public
static
void
convertToPdf(JFreeChart chart,
int
width,
int
height, String filename)
...
{
// step 1
Document document = new Document(new Rectangle(width, height));
try ...{
// step 2
PdfWriter writer;
writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(width, height);
Graphics2D g2d = tp.createGraphics(width, height, new DefaultFontMapper());
Rectangle2D r2d = new Rectangle2D.Double(0, 0, width, height);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp, 0, 0);
}
catch(DocumentException de) ...{
de.printStackTrace();
}
catch (FileNotFoundException e) ...{
e.printStackTrace();
}
// step 5
document.close();
}
b)汉语,日语和其他东方语言的支持:
PdfContentByte cb
=
pdfwriter.getDirectContent();
PdfTemplate tp
=
cb.createTemplate(
500
,
300
);
Graphics2D g2d
=
//
关键部分
tp.createGraphics(
500
,
300
,
new
AsianFontMapper(
AsianFontMapper.JapaneseFont_Min,
AsianFontMapper.JapaneseEncoding_H));
Rectangle2D r2d
=
new
Rectangle2D.Double(
0
,
0
,
500
,
300
);
JFreeChart chart
=
getChart(map);
chart.draw(g2d, r2d);
g2d.dispose();
cb.addTemplate(tp,
48
, height);
需要加载的包是iTextAsian.jar
夏在地址: http://sourceforge.net/project/downloading.php?groupname=itextpdf&filename=iTextAsian.jar&use_mirror=nchc
本文档介绍如何在项目中利用JFreeChart生成图表,并结合iText库将这些图表嵌入到PDF文件中,解决日文显示和分页后图片坐标处理的问题。通过实例代码展示关键步骤,包括生成PDF和添加语言支持。
652

被折叠的 条评论
为什么被折叠?



