日期:2014-05-20  浏览次数:20731 次

把很多重复出现的代码,做个简单的封装,小白求教..
@SuppressWarnings("unused")
public void PdfTab3() throws Exception
{
Document document = new Document(PageSize.A4, 80, 79, 20, 45);
try
{
// 中文处理
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 其他所有文字字体
Font normal = new Font(bfChinese, 14, Font.COURIER);
// 粗体
Font bold = new Font(bfChinese, 14, Font.BOLD);
// 交易表1小字,四级
Font fifth = new Font(bfChinese, 6, Font.NORMAL);
// 模板抬头的字体
Font titleChinese = new Font(bfChinese, 12, Font.NORMAL);
// 一级
Font first = new Font(bfChinese, 12, Font.COURIER);
// 二级
Font second = new Font(bfChinese, 8, Font.COURIER);
// 三级
Font third = new Font(bfChinese, 6, Font.BOLD);
// 设置pdf生成路径
PdfWriter.getInstance(document, new FileOutputStream("D:\\20121017-3.pdf"));
// 打开文档
document.open();
// ------------------写入数据---------------------
Paragraph title = new Paragraph("招标补遗表", titleChinese);
title.setAlignment(Element.ALIGN_CENTER); // 设置居中
title.setLeading(1f);// 设置行间距
document.add(title);

// 交易表2
title = new Paragraph("                                                                                                                                                                                                                                                                                                                                          交易表3", fifth);
title.setSpacingBefore(1f);
title.setLeading(10f);
document.add(title);

float[] widths = { 10f, 25f, 10f, 25f };
PdfPTable table = new PdfPTable(widths);
table.setSpacingBefore(5f);
table.setTotalWidth(500);