public static void main(String[] args) {
String FILE_URL = "C:\\Users\\xxx\\Desktop";
com.itextpdf.text.Document document = new com.itextpdf.text.Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FILE_URL + "\\hello01.pdf"));
document.open();
//设置中文样式(不设置,中文将不会显示)
BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font fontChinese_title = new Font(bfChinese, 20, Font.BOLD, BaseColor.BLACK);
Font fontChinese_content = new Font(bfChinese, 10, Font.NORMAL, BaseColor.BLACK);
Paragraph paragraph_title = new Paragraph("这是一个标题", fontChinese_title);
paragraph_title.setAlignment(Paragraph.ALIGN_CENTER);
Paragraph paragraph_title_1 = new Paragraph("01总概括", fontChinese_content);
paragraph_title_1.setAlignment(Paragraph.ALIGN_JUSTIFIED);
Paragraph paragraph_content = new Paragraph("This is a table", fontChinese_content);
paragraph_content.setFirstLineIndent(20);
document.add(paragraph_title);
document.add(paragraph_title_1);
document.add(paragraph_content);
/**
* 生成统计图
*/
PieDataset dataset = pieDataSet();
JFreeChart chart = ChartFactory.createPieChart(
" 项目进度分布", // chart title
dataset,// data
true,// include legend
true,
false
);
PiePlot plot=(PiePlot)chart.getPlot();
//设置Label字体
plot.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
//设置legend字体
chart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 12));
// 图片中显示百分比:默认方式
//plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
// 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
// 设置背景色为白色
chart.setBackgroundPaint(Color.white);
// 指定图片的透明度(0.0-1.0)
plot.setForegroundAlpha(1.0f);
// 指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(true);
// 设置图标题的字体
java.awt.Font font = new java.awt.Font(" 黑体",java.awt.Font.CENTER_BASELINE,20);
TextTitle title = new TextTitle("项目状态分布");
title.setFont(font);
chart.setTitle(title);
BufferedImage bufferedImage = chart.createBufferedImage(328, 370);
Image lcImage = Image.getInstance((java.awt.Image) bufferedImage, null);
// 添加表格,4列
PdfPTable table = new PdfPTable(4);
设置表格宽度比例为%100
table.setWidthPercentage(100);
// 设置表格的宽度
table.setTotalWidth(500);
// 也可以每列分别设置宽度
table.setTotalWidth(new float[] { 160, 70, 130, 100 });
// 锁住宽度
table.setLockedWidth(true);
// 设置表格上面空白宽度
table.setSpacingBefore(10f);
// 设置表格下面空白宽度
table.setSpacingAfter(10f);
// 设置表格默认为无边框
table.getDefaultCell().setBorder(0);
PdfContentByte cb = writer.getDirectContent();
// 构建每个单元格
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
// 边框颜色
cell1.setBorderColor(BaseColor.BLUE);
// 设置背景颜色
cell1.setBackgroundColor(BaseColor.ORANGE);
// 设置跨两行
cell1.setRowspan(2);
// 设置距左边的距离
cell1.setPaddingLeft(10);
// 设置高度
cell1.setFixedHeight(20);
// 设置内容水平居中显示
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
// 设置垂直居中
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
cell2.setBorderColor(BaseColor.GREEN);
cell2.setPaddingLeft(10);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
cell3.setBorderColor(BaseColor.RED);
cell3.setPaddingLeft(10);
// 设置无边框
//cell3.setBorder(Rectangle.OUT_TOP);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell3);
// 在表格添加图片
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 4"));
cell4.setBorderColor(BaseColor.RED);
cell4.setPaddingLeft(10);
cell4.setFixedHeight(30);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell4);
PdfPCell cell5 = new PdfPCell(lcImage, true);
cell5.setPaddingLeft(10);
// 设置占用列数
cell5.setColspan(2);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell5);
PdfPCell cell6 = new PdfPCell(new Paragraph("last cell 0"));
table.addCell(cell6);
// 增加一个条形码到表格
/*Barcode128 code128 = new Barcode128();
code128.setCode("14785236987541");
code128.setCodeType(Barcode128.CODE128);
// 生成条形码图片
Image code128Image = code128.createImageWithBarcode(cb, null, null);
// 加入到表格
PdfPCell cellcode = new PdfPCell(code128Image, true);
cellcode.setHorizontalAlignment(Element.ALIGN_CENTER);
cellcode.setVerticalAlignment(Element.ALIGN_MIDDLE);
cellcode.setFixedHeight(30);
table.addCell(cellcode);*/
document.add(table);
document.add(lcImage);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static PieDataset pieDataSet() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue(" 市场前期", new Double(10));
dataset.setValue(" 立项", new Double(15));
dataset.setValue(" 计划", new Double(10));
dataset.setValue(" 需求与设计", new Double(10));
dataset.setValue(" 执行控制", new Double(35));
dataset.setValue(" 收尾", new Double(10));
dataset.setValue(" 运维",new Double(10));
return dataset;
}