目录
前言:
pom需要的依赖:
测试类:
效果:
生成表格PDF:
其他复杂的格式就去研究那个 如何生成吧
测试类代码:
前言:
摸鱼来的
pom需要的依赖:
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13.2</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency>
测试类:
生成一个helloword
package com.example.demo;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class PDFutilOut {
public static void main(String[] args) {
createPDF("C:/Users/本机用户姓名路径/Desktop/test1.pdf");
System.out.println("打印完成");
}
public static void createPDF(String filename) {
Document document = new Document(PageSize.A4);
try {
PdfWriter.getInstance(document, new FileOutputStream(filename));
document.addTitle("example of PDF");
document.open();
document.add(new Paragraph("Hello World!"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
效果:
生成表格PDF:
其他复杂的格式就去研究那个 如何生成吧
测试类代码:
package com.example.demo;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class PDFUtil2 {
//pdf创建表格
public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
PdfPTable table = new PdfPTable(2);//生成一个两列的表格
PdfPCell cell;
//有中文文字的话需要设置字体
// Font font = new Font(BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED));
Font font = new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
int size = 30;
cell = new PdfPCell(new Phrase("您好--1", font));
cell.setFixedHeight(size);//设置高度
table.addCell(cell);
cell = new PdfPCell(new Phrase("2"));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Phrase("3"));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Phrase("4"));
cell.setFixedHeight(size);
table.addCell(cell);
//合并单元格
cell = new PdfPCell(new Phrase("5"));
cell.setColspan(1);//设置所占列数
cell.setRowspan(2);//设置所占行数
cell.setFixedHeight(size * 2);//设置高度为标准高度的两倍
cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
table.addCell(cell);
cell = new PdfPCell(new Phrase("6"));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Phrase("77777"));
cell.setFixedHeight(size);
table.addCell(cell);
return table;
}
//创建pdf文件
public static void createPDF(String filename) throws IOException {
Document document = new Document(PageSize.A4);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
document.addTitle("example of PDF");
document.open();
//document.add(new Paragraph("Hello World!"));
PdfPTable table = createTable(writer);
document.add(table);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
}
public static void main(String[] args) throws IOException {
createPDF("C:/Users/本机用户姓名/Desktop/test1.pdf");
System.out.println("打印完成");
}
}