目录
写在前面
字体问题
itext默认的字体是不支持中文的,这样会导致只有西欧字符可以显示。如果需要支持简体中文,需要自己创建字体。
在window环境下查找字体
- 可以去fonts目录下查找。例如 :
C:\WINDOWS|FONTS\SIMHEI.TTF
,这个是黑体。 - 控制面板-外观个性化-字体。 可以查找对应字体,该方式比较直观,可以看到字体的名称。
选好字体,右键选择属性,切换到安全选项卡。就可以看到字体的路径。 例如选中楷体常规,右键就可以看到字体路径。
自定义字体
String fontPath="";
BaseFont baseFont = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font =new Font(baseFont,20f,Font.BLOD,BaseColor.BLACK);
//创建了一个自定义字体格式,样式:粗体 字体大小:20 颜色:黑色
这样就可以在其他地方使用这个字体了。
如果是诸如:微软雅黑这类的字体,还需要在传入字体后添加",数字"。数字表示字体的样式
BaseFont baseFont = BaseFont.createFont("D:\\fonts\\msyh.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
0: 常规
1:粗体
2: 简体
流程
创建pdf文件五部曲
Document document = new Document();//1.创建document实例
FileOutputStream out=new FileOutputStream(new File("D:/text.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, out);//2.创建PdfWriter 实例,并指定输出路径。
document.open();//3. 打开 document实例,开始想document中添加内容
//添加内容
document.add(new Paragraph("内容",font));//4.添加内容
document.close();//5. 关闭
表格相关
PdfPTable :
PdfPCell
Note that you always have to set thetotal width if you intend to add the PdfPTable at an absolute position
表格的宽度问题
默认情况下新增的table的宽度是80而不是100。
所以添加的表格总是左右两个有明显的留白。
table.setWidthPercentage(100);//设置表格宽度为父容器宽度的100%
一般表格嵌套时会经常使用该方法
PdfPTable outTable=new PdfPTable(2);// 外表格
PdfPCell cell=new PdfPCell();
PdfPTable innerTable=new PdfPTable(2);//内表格
innerTable.setWidthPercentage(100);// 宽度占比100%
PdfPCell cell1=new PdfPCell(new Phrase("cell0"));
cell1.setBorderWidthTop(0);//边框宽度设置为0
cell1.setBorderWidthLeft(0);
cell1.setBorderWidthBottom(0);
innerTable.addCell(cell1);//添加 单元格
cell1=new PdfPCell(new Phrase("cell1"));
cell1.setBorderWidthTop(0);
cell1.setBorderWidthRight(0);
cell1.setBorderWidthBottom(0);
cell1.setBorderWidth(0);
innerTable.addCell(cell1);//添加单元格
cell.setPadding(0);//设置内边距为0
cell.addElement(innerTable);//添加内嵌表格
outTable.addCell(cell);
outTable.addCell(new Phrase("xxxx"));
设置单元格样式
-
设置行距。
对于Phrase而言提供了setLeading()和setMultipliedLeading()方法设置行距。但是该方法设置的行距对PdfPCell这种容器中是不起作用的。需要使用PdfPcell的setLeading方法。
效果如下:
-
设置内边距
setPadding(),可以用来设置内边距.setBorderPaddingTop();//上边框 setBorderPaddingBottom();//下边框 setBorderPaddingLeft();//左边框 setBorderPaddingRight();//右边框
-
设置边框宽度
setBorderWidth,设置单元格边框宽度。如果需要设置某一个边的边框。setBorderWidthTop();//上边框 setBorderWidthBottom();//下边框 setBorderWidthLeft();//左边框 setBorderWidthRight();//右边框
-
设置边框颜色
setBorderColor(),设置边框颜色setBorderColorTop();//上边框 setBorderColorBottom();//下边框 setBorderColorLeft();//左边框 setBorderColorRight();//右边框
目录
实现目录主要用到两个类 Chapter 和Section。其中Chapter是Section的子类,
Paragraph prag = new Paragraph("第一章", font);
Chapter chapter2 = new Chapter(title2, 1);
Section section =chapter.addSection(new Paragraph("1. 第一节"),font);
Section section =chapter.addSection(new Paragraph("1. 1 第1小节"),font);
Chapter
重要的概念:
- depth: 章节的深度,默认情况深度为1.
- title : 章节的标题
- number。章节的序号,只能为数字
- outline:书签
Chapter中有很多默认的行为:
-
每个Chapter 默认是新起一页,可以通过setTriggerNewPage(false)修改这一默认行为
-
默认情况下创建的chapter标题后面会追加逗号( “.”)。 可以通过setNumberStyle(NUMBERSTYLE_DOTTED_WITHOUT_FINAL_DOT)更改这一行为
-
默认添加的所有子章节都会在左侧的书签中显示,可以通过setBookmarkOpen(false)修改这一行为。可以控制子标签是否显示。
-
添加Chapter时,默认会创建书签。
Chapter firstChapter=new Chapter(2);
firstChapter.setTitle(new Paragraph("章节一",font));
firstChapter.add(new Phrase("first chapter depth:"+firstChapter.getDepth()));
Section section = firstChapter.addSection("second1");//子章节
section.setIndentationLeft(20f);
section.add(new Phrase("second1 depth:"+section.getDepth()));
section.addSection("second1.1").setIndentationLeft(20f);;
Section section2 = firstChapter.addSection("second2");//子章节
section2.setIndentationLeft(20f);
section2.add(new Phrase("second2 depth:"+section.getDepth()));
document.add(firstChapter);
读取文档
PdfReader
重要的方法:
getNumberOfPages();// 获取文档有多少页
getPageSizeWithRotation(;// getPageSize() 和 getPageRotation()的结合体
-
损坏的pdf文件是不能打开的。
-
如果文档进行了加密,也是不能打开的(特指用户密码)
读取文档时减少内存使用。
-
读取全部文档
PdfReader reader= new PdfReader(filename);
-
读取部分文件
PdfReader reader = new PdfReader(new RandomAccessFileOrArray(filename), null);
When reading a file partially, more memory will be used as soon as you start working with the reader object, but PdfReader won’t cache unnecessary objects
3. 在开始使用PdfReader之前,减少页数
reader.selectPages("4-8"); //读取4到8页
//[!][o][odd][e][even]start[-end
selectPages的语法。
[!] [o] [odd] [e ] [even] start [-end]
- 可以同时指定多个返回使用逗号隔开,“4-8,9-11”。范围必须是递增的。
- 可以省略 start 或者 end。
- 如果同时省略start 或者end 。需要指定 o(奇数页)或者 e(偶数页)
- 使用! 可以删除之前已被选中的范围
- 选中范围以后,页面将从1 开始计数。4-8,则第四页为新的第一页
读取pdf
从已存在的文档中拷贝页面
In this section, you’ll use an object named PdfImportedPage to copy the content from an existing PDF opened with PdfReader into a new Document written by PdfWriter.
如果想要重新使用某个文档的页面,并将这些页面看作图片。
Document document = new Document();
PdfWriter writer=PdfWriter.getInstance(document,path);
PdfReader reader= new PdfReader(path);
int total = reader.getNumberOfPages();
PdfImportedPage importedPage = null;
for(int i=0;i<total;i++){
page = writer.getImportedPage(reader, i);
table.addCell(Image.getInstance(page));
}