Java写入PDF-表格、列表、图片

Java生成PDF文档(表格、列表、添加图片等)

jar包下载地址:https://download.csdn.net/download/mayue318/15445480

代码如下:

复制代码
1 import java.awt.Color;
2 import java.io.FileOutputStream;
3 import com.lowagie.text.Cell;
4 import com.lowagie.text.Chapter;
5 import com.lowagie.text.Document;
6 import com.lowagie.text.Font;
7 import com.lowagie.text.Image;
8 import com.lowagie.text.List;
9 import com.lowagie.text.ListItem;
10 import com.lowagie.text.PageSize;
11 import com.lowagie.text.Paragraph;
12 import com.lowagie.text.Section;
13 import com.lowagie.text.Table;
14 import com.lowagie.text.pdf.BaseFont;
15 import com.lowagie.text.pdf.PdfWriter;
16 public class ITextDemo {
17 public boolean iTextTest() {
18 try {
19 /** 实例化文档对象 /
20 Document document = new Document(PageSize.A4, 50, 50, 50, 50);
21 /
* 创建 PdfWriter 对象 /
22 PdfWriter.getInstance(document,// 文档对象的引用
23 new FileOutputStream(“d://ITextTest.pdf”));//文件的输出路径+文件的实际名称
24 document.open();// 打开文档
25 /
* pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包 /
26 BaseFont bfChinese = BaseFont.createFont(“STSong-Light”,
27 “UniGB-UCS2-H”, BaseFont.NOT_EMBEDDED);
28 Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//加入document:
29 /
* 向文档中添加内容,创建段落对象 /
30 document.add(new Paragraph(“First page of the document.”));// Paragraph添加文本
31 document.add(new Paragraph(“我们是害虫”, FontChinese));
32 /
* 创建章节对象 /
33 Paragraph title1 = new Paragraph(“第一章”, FontChinese);
34 Chapter chapter1 = new Chapter(title1, 1);
35 chapter1.setNumberDepth(0);
36 /
* 创建章节中的小节 /
37 Paragraph title11 = new Paragraph(“表格的添加”, FontChinese);
38 Section section1 = chapter1.addSection(title11);
39 /
* 创建段落并添加到小节中 /
40 Paragraph someSectionText = new Paragraph(“下面展示的为3 X 2 表格.”,
41 FontChinese);
42 section1.add(someSectionText);
43 /
* 创建表格对象(包含行列矩阵的表格) /
44 Table t = new Table(3, 2);// 2行3列
45 t.setBorderColor(new Color(220, 255, 100));
46 t.setPadding(5);
47 t.setSpacing(5);
48 t.setBorderWidth(1);
49 Cell c1 = new Cell(new Paragraph(“第一格”, FontChinese));
50 t.addCell(c1);
51 c1 = new Cell(“Header2”);
52 t.addCell(c1);
53 c1 = new Cell(“Header3”);
54 t.addCell(c1);
55 // 第二行开始不需要new Cell()
56 t.addCell(“1.1”);
57 t.addCell(“1.2”);
58 t.addCell(“1.3”);
59 section1.add(t);
60 /
* 创建章节中的小节 /
61 Paragraph title13 = new Paragraph(“列表的添加”, FontChinese);
62 Section section3 = chapter1.addSection(title13);
63 /
* 创建段落并添加到小节中 /
64 Paragraph someSectionText3 = new Paragraph(“下面展示的为列表.”, FontChinese);
65 section3.add(someSectionText3);
66 /
* 创建列表并添加到pdf文档中 /
67 List l = new List(true, true, 10);// 第一个参数为true,则创建一个要自行编号的列表,
68 // 如果为false则不进行自行编号
69 l.add(new ListItem(“First item of list”));
70 l.add(new ListItem(“第二个列表”, FontChinese));
71 section3.add(l);
72 document.add(chapter1);
73 /
* 创建章节对象 /
74 Paragraph title2 = new Paragraph(“第二章”, FontChinese);
75 Chapter chapter2 = new Chapter(title2, 1);
76 chapter2.setNumberDepth(0);
77 /
* 创建章节中的小节 /
78 Paragraph title12 = new Paragraph(“png图片添加”, FontChinese);
79 Section section2 = chapter2.addSection(title12);
80 /
* 添加图片 */
81 section2.add(new Paragraph(“图片添加: 饼图”, FontChinese));
82 Image png = Image.getInstance(“D:/pie.png”);//图片的地址
83 section2.add(png);
84 document.add(chapter2);
85 document.close();
86 return true;
87 } catch (Exception e2) {
88 System.out.println(e2.getMessage());
89 }
90 return false;
91 }
92 public static void main(String args[]) {
93 System.out.println(new ITextDemo().iTextTest());
94 }
95 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值