iText实现Java生成PDF文件文档

转:https://blog.csdn.net/weixin_37848710/article/details/89522862

一、主要步骤(5个):

1.新建document对象,可通过一下三种任意一种
Document document =new Document(); // 默认页面大小是A4
Document document =new Document(PageSize.A4); // 指定页面大小为A4
Document document =new Document(PageSize.A4,50,50,30,20); // 指定页面大小为A4,且自定义页边距(marginLeft、marginRight、marginTop、marginBottom)
其中页面大小PageSize也可自定义大小,例:new Document(new Rectangle(400, 500));

2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径
PdfWriter writer =PdfWriter.getInstance(document,new FileOutputStream(filePath));

3.打开文档
写入数据之前要打开文档
document.open();

4.向文档中添加内容
document.add();

5.关闭文档
document.close();
 

二、字体

新建一个字体,iText的方法
BaseFont bfChinese;
bfChinese=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//jar包
bfChinese=BaseFont.createFont("C:/Windows/Fonts/msyh.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //系统字体,其实5.0版以后的iText加入字体还是很方便的。
STSongStd-Light 是字体,在jar 中以property为后缀
UniGB-UCS2-H 是编码,在jar 中以cmap为后缀
H 代表文字版式是横版,相应的 V 代表竖版


字体设置
参数一:新建好的字体;参数二:字体大小,参数三:字体样式,多个样式用“|”分隔
Font topfont = new Font(bfChinese,14,Font.BOLD);
Font textfont =new Font(bfChinese,10,,Font.BOLD|Font.UNDERLINE);
 

三、添加文本的对象:块、短句和段落

Chunk:块(Chunk)是能被添加到文档的文本的最小单位。
Phrase:短句(Phrase)是一系列以特定间距(两行之间的距离)作为参数的块。
Paragraph:段落是一系列块和(或)短句。同短句一样,段落有确定的间距。用户还可以指定缩排;在边和(或)右边保留一定空白,段落可以左对齐、右对齐和居中对齐。添加到文档中的每一个段落将自动另起一行。
 

四、步骤2书写器创建之后,步骤3文档打开之前
以下项只可在文档关闭状态执行 ,包括水印、页眉、页脚

水印
Watermark内部类,需要继承 PdfPageEventHelper类
writer.setPageEvent(new Watermark());

页眉/页脚
iText5中并没有之前版本HeaderFooter对象设置页眉和页脚,可以利用PdfPageEvent来完成页眉页脚的设置工作。
PdfPageEvent提供了几个pdf在创建时的事件,页眉页脚就是在每页加载完写入的。
每一页加个页码还是很简单的,但是总页码就麻烦了,iText是流模式的写入内容,只有写到最后,才能知道有多少页,那么显示总页数就麻烦了,不过麻烦不代表不可能。
其实iText仅在调用释放模板方法后才将PdfTemplate写入到OutputStream中,否则对象将一直保存在内存中,直到关闭文档。
所以我们可以在最后关闭文档前,使用PdfTemplate写入总页码。可以理解成先写个占位符,然后统一替换。
 

五、设置文档属性  (与文档是否打开没有关联)

document.addTitle("Title@PDF-Java");// 标题
document.addAuthor("Author@umiz");// 作者
document.addSubject("Subject@iText pdf sample");// 主题
document.addKeywords("Keywords@iTextpdf");// 关键字
document.addCreator("Creator@umiz`s");// 创建者
 

六、文档内容

段落Paragraph
Paragraph pt=new Paragraph(name,headfont);//设置字体样式
pt.setAlignment(1);//设置文字居中 0靠左   1,居中     2,靠右
pt.setIndentationLeft(12);// 左缩进  
pt.setIndentationRight(12);// 右缩进  
pt.setFirstLineIndent(24);// 首行缩进 
paragraph.setLeading(20f); //行间距
paragraph.setSpacingBefore(5f); //设置段落上空白
paragraph.setSpacingAfter(10f); //设置段落下空白

表格table
Table table =new Table(3);//括号参数表示列
int width[] = {10,45,45};//设置每列宽度比例   
table.setWidths(width);   
table.setWidth(95);//占页面宽度比例
table.setAlignment(Element.ALIGN_CENTER);//居中    
table.setAutoFillEmptyCells(true);//自动填满       
table.setBorderWidth((float)0.1);//表格边框线条宽度    
table.setPadding(1);//边距:单元格的边线与单元格内容的边距  
table.setSpacing(0);//间距:单元格与单元格之间的距离
table.addCell(new Paragraph("name"),textfont));//添加单元格内容
table.endHeaders();//每页都会显示表头

单元格内容样式cell  
Cell cell=new Cell(new Paragraph("序号",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中   
table.addCell(cell);

表格嵌套 
  最外层表格
PdfPTable table =new PdfPTable(3);
table.setTotalWidth(300);
table.setLockedWidth(true);

PdfPCell cell;
cell =new PdfPCell(new Phrase("Table 5"));
cell.setColspan(3);
cell.setBorderWidth(0);//设置表格的边框宽度为0
table.addCell(cell);

  嵌套表格
PdfPTable celltable =new PdfPTable(2);
cell =new PdfPCell(celltable);
cell.setRowspan(2);
cell.setBorderWidth(1);//设置表格的边框宽度为1
cell.setPadding(10);//设置表格与上一个表格的填充为10
table.addCell(cell);

直线  
Paragraph p1 =new Paragraph();  
p1.add(new Chunk(new LineSeparator()));   
doc.add(p1);  

点线  
Paragraph p2 =new Paragraph();  
p2.add(new Chunk(new DottedLineSeparator()));

超链接
Anchor anchor =new Anchor("this is anchor");

定位
点击后,跳到topline的位置
Anchor gotop =new Anchor("go top");
gotop.setReference("#us");

添加图片
Image image =Image.getInstance(imgPath);
image.setAlignment(Image.ALIGN_CENTER);
image.scalePercent(40);//依照比例缩放

原文地址:https://www.cnblogs.com/zj68/p/14111191.html