1导入jar包
<!--设备生成pdf使用-->
<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>
这里需要注意版本,不然使用字体时会报错
打印PDF报错:STSong-Light' with 'UniGB-UCS2-H' is not recognized
2 代码
public static void main(String[] args) {
String tmpPath = "C:\\Users\\Desktop\\PDFDemo.pdf";
PdfPCell cell;
PdfPCell iCell;
PdfPTable iTable;
float lineHeight1 = (float) 40.0;
float lineHeight2 = (float) 40.0;
float lineHeight3 = (float) 40.0;
float lineHeight4 = (float) 40.0;
float lineHeight5 = (float) 40.0;
try {
// 不同字体(这里定义为同一种字体:包含不同字号、不同style)
//注意 此处不定义字体,生成出来的pdf内容汉字部分会不显示
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new Font(bfChinese, 16, Font.BOLD);
headfont = new Font(bfChinese, 14, Font.BOLD);
keyfont = new Font(bfChinese, 16, Font.BOLD);
textfont = new Font(bfChinese, 10, Font.NORMAL);
Document pdfDoc = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(pdfDoc, new FileOutputStream(tmpPath));
pdfDoc.open();
//自上往下1列
PdfPTable headerTable = new PdfPTable(1);
//设置表格具体宽度
headerTable.setTotalWidth(160);
//设置每一列宽度40
headerTable.setWidthPercentage(40);
//在第一列下分两列
iTable = new PdfPTable(2);
//设置每一列所占的长度
iTable.setWidths(new float[]{40f, 120f});
iCell = new PdfPCell(new Paragraph("检测公司", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
//merge column
iCell.setColspan(2);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("设备名称", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight2);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight2);
iTable.addCell(iCell);
cell = new PdfPCell(iTable);
cell.setPadding(0);
headerTable.addCell(cell);
//在上面一行下再新建一行分4列
iTable = new PdfPTable(4);
//设置每一列所占的长度
iTable.setWidths(new float[]{40f, 40f, 40f, 40f});
iCell = new PdfPCell(new Paragraph("设备编号", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight3);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(""));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight3);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("规格型号", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight3);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(""));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight3);
iCell.setColspan(4);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("出厂编号", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight4);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(""));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight4);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("生产厂家", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight4);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(""));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight4);
iCell.setColspan(4);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("购置日期", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight5);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(""));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight5);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph("管理人员", keyfont));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight5);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(""));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight5);
iTable.addCell(iCell);
cell = new PdfPCell(iTable);
cell.setPadding(0);
headerTable.addCell(cell);
pdfDoc.add(headerTable);
pdfDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}