1.引入的maven依赖
<!-- pdf依赖包-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- pdf文件合并依赖包-->
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.27</version>
</dependency>
2.画pdf简单表格示例
package com.test;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class pdfTest {
public static void main(String[] args) {
String fileName = "单元格合并.pdf";
String path = "E:/dasy/"; // 文件路径
PdfPCell cell;
PdfPCell iCell;
PdfPTable iTable;
PdfPTable fourTable;
float lineHeight1 = (float) 25.0; // 单元格高度
float lineHeight2 = (float) 25.0;
try {
//实例化Document类的对象
Document pdfDoc = new Document(PageSize.A4.rotate(), 36, 36, 24, 36);
// 生成pdf文件
PdfWriter.getInstance(pdfDoc, new FileOutputStream(path+fileName));
pdfDoc.open();
PdfPTable headerTable = new PdfPTable(4); // 最外层表格设置4列数
headerTable.setWidthPercentage(40);
// 第一行设置每个单元格信息
iCell = new PdfPCell(new Paragraph(" 11 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" 12 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" 13 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" 14 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
// 第二、三行
iTable = new PdfPTable(1); //设置一个一列的表格
iCell = new PdfPCell(new Paragraph(" A "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" B "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
iTable.addCell(iCell);
cell = new PdfPCell(iTable);
headerTable.addCell(cell); // 把表格内容放到最外层表格里
iTable = new PdfPTable(1);
iCell = new PdfPCell(new Paragraph(" A2 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
iTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" B2 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
iTable.addCell(iCell);
cell = new PdfPCell(iTable);
headerTable.addCell(cell); // 把表格内容放到最外层表格里
// fill cell 2 列合并
cell = new PdfPCell(new Paragraph(" E "));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(0);
cell.setFixedHeight(lineHeight1 + lineHeight2); // 高度为A、B合并的高度
headerTable.addCell(cell);
cell = new PdfPCell(new Paragraph(" F "));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setPadding(0);
cell.setFixedHeight(lineHeight1 + lineHeight2);
headerTable.addCell(cell);
// 第4行
iCell = new PdfPCell(new Paragraph(" 41 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" 42 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" 43 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
iCell = new PdfPCell(new Paragraph(" 44 "));
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
iCell.setFixedHeight(lineHeight1);
headerTable.addCell(iCell);
pdfDoc.add(headerTable);
pdfDoc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
pdf文件导出结果:
2.pdf文件添加单元格斜线
参考网址:https://www.superweb999.com/article/352686.html
package com.test;
import java.awt.Color;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class pdfTest {
private static final float PDF_PERCENT = 0.75f;
public static void main(String[] args) throws Exception {
float[] widths = { 144 * PDF_PERCENT, 113 * PDF_PERCENT, 91 * PDF_PERCENT,
110 * PDF_PERCENT };
int TOP = 36, LEFT = 36;
FileOutputStream fos = new FileOutputStream("E:/dasy/line.pdf");
// 创建一个iText Document 对象,pdf文档大小设置为A4,通过PdfWriter对象获取PdfContentByte 对象为 canvas,
// 下面是通过canvas去画图形
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
document.setPageSize(PageSize.A4);
document.open();
PdfContentByte canvas = writer.getDirectContent();
float top = document.getPageSize().getHeight() - TOP;
// 创建一个PdfPTable 对象,Pdf Table需要指定每列的宽度,pdf的宽度=像素宽*0.75, 指定Table的总宽度,总宽度是每列宽度之和,设置Table相对于Document居左对齐。
PdfPTable table = new PdfPTable(widths);
table.setLockedWidth(true);
table.setTotalWidth(458 * PDF_PERCENT);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
PdfPCell pdfCell = new PdfPCell();
final String text = " AB\n\n\n CD";
setBorderColor(pdfCell);
pdfCell.setMinimumHeight(77 * PDF_PERCENT);
pdfCell.setPhrase(new Paragraph(text, getPdfChineseFont()));
table.addCell(pdfCell);
// 画第一个单元格3个斜线,x1,y1,x2,y2,x3,y3为线的坐标终止位置,开始坐标为单元格开始位置,
// LEFT=36,TOP=36,是PdfTable距离文档的左边距和上边距,而Pdf的y坐标位置是从底部开始计算的,所以第一个单元格的坐标位置是
// document.getPageSize().getHeight() - TOP 与 LEFT, canvas.moveTo()单元格坐标位置,3条线分别lineTo()每个条线的终止坐标。
// draw cell line
int x1 = 61, y1 = 77;
int x2 = 132, y2 = 76;
int x3 = 144, y3 = 31;
drawLine(canvas, LEFT, top, x1, y1);
drawLine(canvas, LEFT, top, x2, y2);
drawLine(canvas, LEFT, top, x3, y3);
pdfCell = new PdfPCell();
setBorderColor(pdfCell);
pdfCell.setMinimumHeight(77 * PDF_PERCENT);
table.addCell(pdfCell);
pdfCell = new PdfPCell();
setBorderColor(pdfCell);
pdfCell.setMinimumHeight(77 * PDF_PERCENT);
table.addCell(pdfCell);
pdfCell = new PdfPCell();
setBorderColor(pdfCell);
pdfCell.setMinimumHeight(77 * PDF_PERCENT);
table.addCell(pdfCell);
pdfCell = new PdfPCell();
setBorderColor(pdfCell);
pdfCell.setMinimumHeight(83 * PDF_PERCENT);
table.addCell(pdfCell);
pdfCell = new PdfPCell();
setBorderColor(pdfCell);
pdfCell.setMinimumHeight(83 * PDF_PERCENT);
table.addCell(pdfCell);
pdfCell = new PdfPCell();
setBorderColor(pdfCell);
pdfCell.setColspan(2);
pdfCell.setMinimumHeight(83 * PDF_PERCENT);
table.addCell(pdfCell);
setBorderColor(pdfCell);
int cellX = (int) (widths[0] + widths[1]);
int cellY = (int) (77 * PDF_PERCENT);
int x = 201, y = 84;
drawLine(canvas, LEFT + cellX, top - cellY, x, y);
document.add(table);
document.close();
}
// draw cell line
private static void drawLine(PdfContentByte canvas, float left, float top, int x, int y) {
canvas.saveState();
canvas.moveTo(left, top);
canvas.lineTo(left + x * PDF_PERCENT, top - y * PDF_PERCENT);
canvas.stroke();
canvas.restoreState();
}
// 设置边框样式
public static void setBorderColor(PdfPCell pdfCell) {
Color rgbColor = Color.BLACK;
BaseColor baseColor = new BaseColor(rgbColor.getRed(), rgbColor.getGreen(), rgbColor.getBlue());
pdfCell.setBorderWidthTop(0.1f);
pdfCell.setBorderWidthBottom(0.1f);
pdfCell.setBorderWidthLeft(0.1f);
pdfCell.setBorderWidthRight(0.1f);
pdfCell.setBorderColorBottom(baseColor);
pdfCell.setBorderColorLeft(baseColor);
pdfCell.setBorderColorRight(baseColor);
pdfCell.setBorderColorTop(baseColor);
}
public static Font getPdfChineseFont() throws Exception {
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
return fontChinese;
}
}
pdf文件导出结果:
3.