生成pdf常用的插件有iReport、和itext,这里将使用itext生成pdf文件。
多于的话不说直接上demo和需要的jar,如果pdf中有图片要画的话可以用jfreeChart画。
package com.pdf;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFReport1 {
int maxWidth = 520;
private static Font keyfont; // 设置字体大小
private static Font textfont; // 设置字体大小
// 建立一个Document对象
Document document = new Document();
static {
BaseFont bfChinese;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
keyfont = new Font(bfChinese, 8, Font.BOLD); // 设置字体大小
textfont = new Font(bfChinese, 8, Font.NORMAL); // 设置字体大小
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置pdf样式
* @param file
*/
public PDFReport1(File file) {
// 设置页面大小
document.setPageSize(PageSize.A4);
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加表格前的说明
* @param value
* @param font
* @param align
* @param colspan
* @param boderFlag
* @return
*/
public PdfPCell createCell(String value, com.lowagie.text.Font font, int align, int colspan, boolean boderFlag) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
cell.setPadding(3.0f);
if (!boderFlag) {
cell.setBorder(0);
cell.setPaddingTop(15.0f);
cell.setPaddingBottom(8.0f);
}
return cell;
}
/**
* 向单元格添加字符串、设置单元格属性
* @param value 字符
* @param font字体
* @param align 对齐方式
* @return
*/
public PdfPCell createCell(String value, com.lowagie.text.Font font, int align) {
PdfPCell cell = new PdfPCell();
//设置单元格对齐方式
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setPhrase(new Phrase(value, font));
//设置边框颜色
cell.setBorderColor(new Color(15, 15, 15));
//设置单元格背景颜色
cell.setBackgroundColor(new Color(118, 59, 167));
return cell;
}
/**
* 向单元格添加字符串、设置单元格属性
* @param value 字符
* @param font字体
* @return
*/
public PdfPCell createCell(String value, com.lowagie.text.Font font) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPhrase(new Phrase(value, font));
cell.setBorderColor(new Color(15, 15, 15));
cell.setBackgroundColor(new Color(59,162,167));
return cell;
}
/**
* 向单元格中添加图片
* @param image 图片
*/
public PdfPCell createCell() {
PdfPCell cell = new PdfPCell();
try {
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Image image = Image.getInstance ("D:\\Arrows_Black_InTable_Down.png");
//设置图片大小
//image.scaleAbsoluteWidth(7);
//image.scaleAbsoluteHeight(11);
image.scaleAbsolute(7,11);
//添加图片
cell.addElement(image);
//设置边框颜色
cell.setBorderColor(new Color(15, 15, 15));
//设置单元格背景颜色
cell.setBackgroundColor(new Color(59,162,167));
} catch (Exception e) {
e.printStackTrace();
}
return cell;
}
public PdfPCell createCell(String value, com.lowagie.text.Font font, int align, int colspan) {
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(align);
cell.setColspan(colspan);
cell.setPhrase(new Phrase(value, font));
return cell;
}
public PdfPTable createTable(int colNumber) {
PdfPTable table = new PdfPTable(colNumber);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public PdfPTable createTable(float[] widths) {
PdfPTable table = new PdfPTable(widths);
try {
table.setTotalWidth(maxWidth);
table.setLockedWidth(true);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setBorder(1);
} catch (Exception e) {
e.printStackTrace();
}
return table;
}
public PdfPTable createBlankTable() {
PdfPTable table = new PdfPTable(1);
table.getDefaultCell().setBorder(0);
table.addCell(createCell("", keyfont));
table.setSpacingAfter(20.0f);
table.setSpacingBefore(20.0f);
return table;
}
public void generatePDF() throws Exception {
PdfPTable table = createTable(5);
table.addCell(createCell("学生信息列表:", keyfont, Element.ALIGN_LEFT, 5, false));
table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));
table.addCell(createCell("升降", keyfont, Element.ALIGN_CENTER));
for (int i = 0; i < 5; i++) {
table.addCell(createCell("姓名" + i, textfont));
table.addCell(createCell(i + 15 + "", textfont));
table.addCell(createCell((i % 2 == 0) ? "男" : "女", textfont));
table.addCell(createCell("地址" + i, textfont));
//添加图片
table.addCell(createCell());
}
document.add(table);
document.close();
}
public static void main(String[] args) throws Exception {
File file = new File("D:\\text.pdf");
file.createNewFile();
new PDFReport1(file).generatePDF();
}
}