/** |
* 写PDF文件,展示了PDF文档、章节、小节、字体、段落、表格、列表的使用 |
* 最后展示如何使用写入中文。 |
* @param fileName |
*/ |
public void writePDF(String fileName) { |
File file = new File(fileName); |
FileOutputStream out = null ; |
try { |
//(1)实例化文档对象 |
//第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。 |
Document document = new Document(PageSize.A4, 50 , 50 , 50 , 50 ); |
//(2)创建写入器 |
//第一个参数是对文档对象的引用,第二个参数是输出的文件,将out和document连接起来 |
out = new FileOutputStream(file); |
PdfWriter writer = PdfWriter.getInstance(document, out); |
//打开文档准备写入内容 |
document.open(); |
|
//(3)下面创建章节对象 |
//首先创建段落对象,作为章节的标题。FontFactory用于指定段落的字体。 |
Font font = FontFactory.getFont(FontFactory.HELVETICA, |
18 , Font.BOLDITALIC, new Color( 0 , 0 , 255 )); |
Paragraph chapter1_title = new Paragraph( "Chapter 1" ,font); |
//创建了一个章节对象,标题为"Chapter 1" |
Chapter chapter1 = new Chapter(chapter1_title, 1 ); |
//将编号级别设为 0 就不会在页面上显示章节编号 |
chapter1.setNumberDepth( 0 ); |
//(4)创建小节对象 |
//创建小节对象的标题 |
font = FontFactory.getFont(FontFactory.HELVETICA, 16 , |
Font.BOLD, new Color( 255 , 0 , 0 )); |
Paragraph section1_title1 = new Paragraph( "Section 1 of Chapter 1" , font); |
//创建一个小节对象,标题为"This is Section 1 in Chapter 1",属于chapter1。 |
Section section1 = chapter1.addSection(section1_title1); |
//(5)往小节中写文本内容 |
Paragraph text = new Paragraph( "This is the first text in section 1 of chapter 1." ); |
section1.add(text); |
text = new Paragraph( "Following is a 5×5 table:" ); |
section1.add(text); |
|
//(6)往小节中写表格 |
//创建表格对象 |
Table table = new Table( 5 , 5 ); |
//设置表格边框颜色 |
table.setBorderColor( new Color( 220 , 255 , 100 )); |
//设置单元格的边距间隔等 |
table.setPadding( 1 ); |
table.setSpacing( 1 ); |
table.setBorderWidth( 1 ); |
//单元格对象 |
Cell cell = null ; |
//添加表头信息 |
for ( int colNum= 0 ; colNum< 5 ; colNum++){ |
cell = new Cell( "header-" + colNum); |
cell.setHeader( true ); |
table.addCell(cell); |
} |
table.endHeaders(); |
//添加表的内容 |
for ( int rowNum= 1 ; rowNum< 5 ; rowNum++){ |
for ( int colNum= 0 ; colNum< 5 ; colNum++){ |
cell= new Cell( "value-" + rowNum + "-" + colNum); |
table.addCell(cell); |
} |
} |
//将表格对象添加到小节对象中 |
section1.add(table); |
|
//(7)添加列表 |
// 列表包含一定数量的 ListItem。可以对列表进行编号,也可以不编号。 |
// 将第一个参数设置为 true 表明想创建一个进行编号的列表; |
// 第二个参数设置为true表示列表采用字母进行编号,为false则用数字进行编号; |
// 第三个参数为列表内容与编号之间的距离。 |
List list = new List( true , false , 20 ); |
ListItem item = new ListItem( "First item of list;" ); |
list.add(item); |
item = new ListItem( "Second item of list;" ); |
list.add(item); |
item = new ListItem( "Third item of list." ); |
list.add(item); |
//将列表对象添加到小节对象中 |
section1.add(list); |
|
//(8)添加中文 |
//允许在PDF中写入中文,将字体文件放在classPath中。 |
//simfang.ttf是仿宋的字体文件 |
BaseFont bfChinese = BaseFont.createFont( "simfang.ttf" , BaseFont.IDENTITY_H, BaseFont.EMBEDDED); |
//中文大小为20,加粗 |
font = new Font(bfChinese, 20 , Font.BOLD); |
text = new Paragraph( "PDF中文测试" , font); |
section1.add(text); |
|
//(9)将章节对象加入到文档中 |
document.add(chapter1); |
|
//(10)关闭文档 |
document.close(); |
System.out.println( "PDF文件生成成功,PDF文件名:" + file.getAbsolutePath()); |
} catch (DocumentException e) { |
System.out.println( "PDF文件" + file.getAbsolutePath() + "生成失败!" + e); |
e.printStackTrace(); |
} catch (IOException ee) { |
System.out.println( "PDF文件" + file.getAbsolutePath() + "生成失败!" + ee); |
ee.printStackTrace(); |
} finally { |
if (out != null ){ |
try { |
//关闭输出文件流 |
out.close(); |
} catch (IOException e1) { |
} |
} |
} |
} |
中级设计师
by: 程序猿style 发表于:2012-11-22 23:22:36 顶(0) | 踩(0) 回复
需要PDFBox的jar包
回复评论