itexpdf
依赖
<!--依赖-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<!--中文-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--用来设置pdf密码 这里先不讨论-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bctsp-jdk15on</artifactId>
<version>1.46</version>
</dependency>
简单使用
public static void main(String[] args) throws FileNotFoundException, DocumentException {
//1.新建Document对象
Document document = new Document();
//2.建立一个书写器
String path = "E:/pdf/test.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(path));
//3.打开文档
document.open();
//4.添加一个内容段落
document.add(new Paragraph("Hello itextpdf"));
//5.关闭文档
document.close();
}
以上程序会在目标path中生成一个新的pdf文件
表格
先创建文件,在创建表格(指定列),再创建行,最好把行添加到行集合中;把表格添加到文件中
public static void main(String[] args) throws FileNotFoundException, DocumentException {
//1.创建文件
Document document = new Document();
//2.创建一个书写器
String path = "E:/pdf/test2.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(path));
//3.打开文件 添加内容
document.open();
document.add(new Paragraph("HD context here"));
//新建一个三列的表 并设置属性
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
ArrayList<PdfPRow> listRows = table.getRows();
//设置列宽
float[] columnWidths = {1f,2f,3f};
table.setWidths(columnWidths);
//行1
PdfPCell[] cells1 = new PdfPCell[3];
PdfPRow row1 = new PdfPRow(cells1);
//设置单元格
cells1[0] = new PdfPCell(new Paragraph("大威天龙"));
cells1[0].setBorderColor(BaseColor.BLUE); //边框验证
cells1[0].setPaddingLeft(20); //左填充20
cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);
cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
cells1[1] = new PdfPCell(new Paragraph("222"));
cells1[2] = new PdfPCell(new Paragraph("333"));
//行2
PdfPCell[] cells2 = new PdfPCell[3];
PdfPRow row2 = new PdfPRow(cells2);
cells2[0] = new PdfPCell(new Paragraph("444"));
//把行添加到集合
listRows.add(row1);
listRows.add(row2);
//把表格添加到文件
document.add(table);
//关闭文档
document.close();
//关闭书写器
pdfWriter.close();
}
在上述的代码中cells1[0] = new PdfPCell(new Paragraph("大威天龙"))
并没有正确输出中文到pdf文件中,原因是我们没有使用itext-asian,在之后我们会尝试解决这个问题。
编辑Pdf信息
pdf信息即文件的属性,在document上既可以设置
public static void main(String[] args) throws FileNotFoundException, DocumentException {
//1.创建文件
Document document = new Document();
//2.创建写入器
String path = "E:/pdf/test3.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(path));
//3.打开文件添加内容
document.open();
document.add(new Paragraph("Hahahah"));
//设置属性
//标题
document.addTitle("Big Title");
//作者
document.addAuthor("rope");
//主题
document.addSubject("subject1");
//关键字
document.addKeywords("KeyWords");
//创建时间
document.addCreationDate();
//
document.addCreator("111");
//关闭文档
document.close();
//关闭书写器
pdfWriter.close();
}
添加图片
public static void main(String[] args) throws IOException, DocumentException {
//创建文件
Document document = new Document();
//建立一个书写器
String path = "E:/pdf/test4.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(path));
//打开文件 添加内容
document.open();
document.add(new Paragraph("HaHaHa"));
//图片
Image image1 = Image.getInstance("C:/Users/oroch/Pictures/Screenshots/1.png");
//设置图片属性
image1.setAbsolutePosition(100f,550f);
image1.scaleAbsolute(200,200);
document.add(image1);
//关闭文档和书写器
document.close();
pdfWriter.close();
}
创建列表
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document document = new Document();
String path = "E:/pdf/test5.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(path));
document.open();
document.add(new Paragraph("HaHa"));
//添加有序列表
List orderList = new List(List.ORDERED);
orderList.add(new ListItem("Iten one"));
orderList.add(new ListItem("Iten two"));
orderList.add(new ListItem("Iten three"));
document.add(orderList);
document.close();
pdfWriter.close();
}
格式化输出
主要是针对之前的中文输出问题
public static void main(String[] args) throws IOException, DocumentException {
//1.创建文件
Document document = new Document();
//2.建立一个书写器
String path = "E:/pdf/test6.pdf";
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(path));
//打开文件
document.open();
//设置中文字体
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
//蓝色字体
Font blueFont = new Font(baseFontChinese);
blueFont.setColor(BaseColor.BLUE);
//段落文本
Paragraph paragraphBlue = new Paragraph("paragraphOne blue font",blueFont);
document.add(paragraphBlue);
//绿色字体
Font greenFont = new Font(baseFontChinese);
greenFont.setColor(BaseColor.GREEN);
//创建章节
Paragraph chapterTitle = new Paragraph("段落标题----大威天龙",greenFont);
Chapter chapter1 = new Chapter(chapterTitle, 1);
chapter1.setNumberDepth(0);
Paragraph sectionTitle = new Paragraph("部分标题",greenFont);
Section section1 = chapter1.addSection(sectionTitle);
Paragraph sectionContext = new Paragraph("部分内容", blueFont);
section1.add(sectionContext);
document.add(chapter1);
document.close();
pdfWriter.close();
}