java pdf 用系统字体大小_java 写PDF文件(章节、小节、字体、段落、表格、列表的使用)...

这篇博客演示了如何使用Java来创建PDF文件,包括设置文档、章节、小节,以及添加字体、段落、表格和列表。还展示了如何在PDF中插入中文并设置字体。
摘要由CSDN通过智能技术生成

[java]代码库/**

* 写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) {

}

}

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值