📄 exportpdf.java
字号:
package com.y2.hr.human.common;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.List;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
/**
*
* 利用开源组件IText动态导出PDF文档 转载时请保留以下信息,注明出处!
*
* 请注意务必要保证path指定处有图片
*
* @author Rocky
*
* @version v1.0
*
* @param <T>
*
* 应用泛型,代表任意一个符合javabean风格的类
*
* 注意这里为了简单起见,boolean型的属性xxx的get器方式为getXxx(),而不是isXxx()
*
* byte[]表示图片数据,注意合适的大小
*
*/
public class ExportPdf<T> {
/**
* 根据指定的列导出数据
*
* @param title
* 导出的文件标题
* @param headers
* 导出的列名
* @param dataset
* 导出的数据集合
* @param out
* 输出流
*/
public void exportPdf(String title, String[] headers, List dataset,
OutputStream out) {
String pattern = "yyyy-MM-dd";
// 作为报表的PDF文件,一定要适合打印机的输出打印
Rectangle rectPageSize = new Rectangle(PageSize.A4);// 定义A4页面大小
rectPageSize = rectPageSize.rotate();// 加上这句可以实现A4页面的横置
Document document = new Document(rectPageSize, 50, 50, 50, 50);// 其余4个参数,设置了页面的4个边距
try {
// 将PDF文档写出到out所关联IO设备上的书写对象
PdfWriter.getInstance(document, out);
// 添加文档元数据信息
document.addTitle(StrHelp.toCN(title));
document.addSubject("export information");
document.addAuthor("Rocky");
document.addCreator("Rocky");
document.addKeywords("pdf itext");
// 定义页头和页尾
HeaderFooter header = new HeaderFooter(new PdfParagraph(title, 20,
true), false);
header.setAlignment(Element.ALIGN_CENTER);
HeaderFooter footer = new HeaderFooter(new Phrase(
"-Page "), new Phrase(" -"));
footer.setAlignment(Element.ALIGN_CENTER);
document.setHeader(header);
document.setFooter(footer);
// 打开PDF文档
document.open();
PdfPTable table = new PdfPTable(headers.length);
// table.setHorizontalAlignment(Element.ALIGN_CENTER);
table.setWidthPercentage(16 * headers.length);
// 产生表格标题行
for (int i = 0; i < headers.length; i++) {
PdfPCell cell = new PdfPCell(new PdfParagraph(headers[i]
.split("_")[1], 14, true));
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
cell.setBackgroundColor(Color.cyan);
cell.setBorderColor(Color.green);
table.addCell(cell);
}
for (int i = 0; i < dataset.size(); i++) {
Object obj = dataset.get(i);
for (int j = 0; j < headers.length; j++) {
String s = headers[j].split("_")[0];
String methodName = "get" + s.substring(0, 1).toUpperCase()
+ s.substring(1);
try {
Method method = obj.getClass().getMethod(methodName,
new Class[] {});
Object value = method.invoke(obj, new Class[] {});
PdfPCell cell = null;
try {
if ("NULL".equals(value.toString()))
throw new Exception();
cell = new PdfPCell(new PdfParagraph(value
.toString()));
} catch (Exception e) {
cell = new PdfPCell(new PdfParagraph(" "));
}
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);
cell.setBorderColor(Color.green);
table.addCell(cell);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
document.add(table);
document.close();
out.flush();
out.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class PdfParagraph extends Paragraph {
private static final long serialVersionUID = -244970043180837974L;
public PdfParagraph(String content) {
super(content, getChineseFont(12, false));
}
public PdfParagraph(String content, int fontSize, boolean isBold) {
super(content, getChineseFont(fontSize, isBold));
}
// 设置字体-返回中文字体
protected static Font getChineseFont(int nfontsize, boolean isBold) {
BaseFont bfChinese;
Font fontChinese = null;
try {
bfChinese = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
if (isBold) {
fontChinese = new Font(bfChinese, nfontsize, Font.BOLD);
} else {
fontChinese = new Font(bfChinese, nfontsize, Font.NORMAL);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fontChinese;
}
// 转化中文
protected Cell ChangeCell(String str, int nfontsize, boolean isBold)
throws IOException, BadElementException, DocumentException {
Phrase ph = ChangeChinese(str, nfontsize, isBold);
Cell cell = new Cell(ph);
// cell.setBorderWidth(3);
return cell;
}
// 转化中文
protected Chunk ChangeChunk(String str, int nfontsize, boolean isBold)
throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize, isBold);
Chunk chunk = new Chunk(str, FontChinese);
return chunk;
}
// 转化中文
protected Phrase ChangeChinese(String str, int nfontsize, boolean isBold)
throws IOException, BadElementException, DocumentException {
Font FontChinese = getChineseFont(nfontsize, isBold);
Phrase ph = new Phrase(str, FontChinese);
return ph;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -