1, generates a simple pdf
- Creating springboot project, not much to say here
- Import dependence
<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>
- Generating a pdf of helloword
package com.example.demo;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@Slf4j
public class TestPdf {
private static final String FILE_NAME = "F:\\pdf\\helloPdf.pdf";
public static void main(String[] args) throws IOException {
TestPdf pdf = new TestPdf();
pdf.createPdf(FILE_NAME);
log.info("打印完成");
}
public void createPdf(String filename)throws IOException {
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();
}
}
}
2, export tabular pdf
public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
Font fontChinese5 = new Font(bf,8);
PdfPTable table = new PdfPTable(2);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cell;
int size = 15;
cell = new PdfPCell(new Paragraph("哈哈哈",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("two",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("three",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("four",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
return table;
}
public 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();
PdfPTable table = createTable(writer);
document.add(table);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
}
3, looking at an example
public static PdfPTable createTable(PdfWriter writer) throws DocumentException, IOException {
BaseFont bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false, false, null, null);
Font fontChinese5 = new Font(bf,8);
PdfPTable table = new PdfPTable(2);
table.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell cell;
int size = 15;
cell = new PdfPCell(new Paragraph("one",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("two",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("three",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("four",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("five",fontChinese5));
cell.setColspan(1);
cell.setRowspan(2);
cell.setFixedHeight(size*2);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("six",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("seven",fontChinese5));
cell.setFixedHeight(size);
table.addCell(cell);
return table;
}