引入依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
代码片段
File file = new File("test.pdf");
Document document = new Document(PageSize.A4, 20, 20, 50, 50);
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
pdfWriter.setStrictImageSequence(true);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese, 15, Font.BOLD);
Paragraph paragraph1 = new Paragraph("我是段落添加对象", font);
paragraph1.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph1);
PdfPTable pdfPTable = new PdfPTable(6);
pdfPTable.setWidthPercentage(500 / 5.23f);
PdfPCell cell;
cell = new PdfPCell();
cell.setPaddingTop(15);
cell.setPaddingBottom(7);
cell.disableBorderSide(Rectangle.BOX);
cell.setColspan(6);
Paragraph cellContent = new Paragraph("表格列内容", font4);
cellContent.setIndentationRight(30);
cellContent.setAlignment(Element.ALIGN_RIGHT);
cellContent.setLeading(28.0f);
cell.addElement(cellContent);
pdfPTable.addCell(cell);
Font fontChunk = new Font(bfChinese, 15, Font.UNDERLINE);
Chunk chunk;
cell = new PdfPCell();
cell.setFixedHeight(360);
cell.disableBorderSide(Rectangle.BOTTOM);
cell.setPaddingBottom(66);
cell.setColspan(6);
cellContent = new Paragraph();
cellContent.setLeading(28.0f);
chunk = new Chunk("font1字体样式", font);
cellContent.add(chunk);
chunk = new Chunk(“fontChun字体样式”, fontChunk);
cellContent.add(chunk);
cellContent.setAlignment(Element.ALIGN_LEFT);
cell.addElement(cellContent);
pdfPTable.addCell(cell);
cell = new PdfPCell();
cell.setBorderColor(BaseColor.GRAY);
cell.setPadding(10);
Image image = Image.getInstance(new URL("url"));
image.scaleToFit(400, 250);
cell.addElement(image);
pdfPTable.addCell(cell);
document.add(pdfPTable);