Apache PDFBox

Apache PDFBox

简介

Apache PDFBox 库是一个用于处理 PDF 文档的开源 Java 工具。

使用方式

引入依赖:

1
2
3
dependencies {
implementation 'org.apache.pdfbox:pdfbox:2.0.30'
}

编写生成代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import java.io.IOException;

public class PDFGenerator {

public static void main(String[] args) {
String pdfFilePath = "output.pdf";
String imgPath = "<image_path>";
try {
// Create a new document
PDDocument document = new PDDocument();
PDPage page1 = new PDPage(PDRectangle.A4);
document.addPage(page1);

// Create content stream
PDPageContentStream contentStream = new PDPageContentStream(document, page1);

// Set image position and size
float x = 100; // X-coordinate
float y = 500; // Y-coordinate
float width = 300; // Image width
float height = 200; // Image height

// Add image to the page
contentStream.drawImage(PDImageXObject.createFromFile(imgPath, document), x, y, width, height);

contentStream.close();

PDPage page2 = new PDPage(PDRectangle.A4);
document.addPage(page2);
PDPageContentStream contentStream2 = new PDPageContentStream(document, page2);

// Draw table
drawTableHeaders(contentStream2);
drawTableRow(contentStream2, 1, "Data 1", "Value 1");
drawTableRow(contentStream2, 2, "Data 2", "Value 2");
contentStream2.close();


document.save(pdfFilePath);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void drawTableHeaders(PDPageContentStream contentStream) throws IOException {
float margin = 50;
float yStart = 700;
float tableWidth = 500;
float yPosition = yStart;
float rowHeight = 20;
float cellMargin = 5f;

float[] columnWidths = {200, 300}; // Adjust column widths as needed

// Draw table headers
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.setLineWidth(1f);
contentStream.moveTo(margin, yPosition);
contentStream.lineTo(margin + tableWidth, yPosition);
contentStream.stroke();

yPosition -= rowHeight;
contentStream.beginText();
contentStream.newLineAtOffset(margin + cellMargin, yPosition - 15);
contentStream.showText("Column 1");
contentStream.newLineAtOffset(columnWidths[0], 0);
contentStream.showText("Column 2");
contentStream.endText();
}

private static void drawTableRow(PDPageContentStream contentStream, int rowNum, String data1, String data2) throws IOException {
float margin = 50;
float yStart = 700;
float tableWidth = 500;
float yPosition = yStart - rowNum * 20;
float rowHeight = 20;
float cellMargin = 5f;

float[] columnWidths = {200, 300}; // Adjust column widths as needed

// Draw table row
contentStream.setLineWidth(1f);
contentStream.moveTo(margin, yPosition);
contentStream.lineTo(margin + tableWidth, yPosition);
contentStream.stroke();

yPosition -= rowHeight;
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 12);
contentStream.newLineAtOffset(margin + cellMargin, yPosition - 15);
contentStream.showText(data1);
contentStream.newLineAtOffset(columnWidths[0], 0);
contentStream.showText(data2);
contentStream.endText();
}
}

参考资料

官方网站

官方项目


Apache PDFBox
https://wangqian0306.github.io/2023/pdfBox/
作者
WangQian
发布于
2023年7月24日
许可协议