声明:只记录,不推荐,因为e-iceblue的spire.pdf是收费的,要licence。
pom引入
XML
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>10.7.3</version>
</dependency>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>14.7.7</version>
</dependency>
demo代码:
java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.utilities.PdfTable;
import com.spire.pdf.utilities.PdfTableExtractor;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
/**
* @className: PDF2ExcelUtil
* @description:
* @author: spirit
* @date: 2024-09-05 10:20
* @version: 1.0
**/
public class PDF2ExcelUtil {
public static void convertPDF(){
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.loadFromFile("E:/xxx-已加密.pdf","123");
//保存为Excel
pdf.saveToFile("E:/PDFToXLS.xlsx", FileFormat.XLSX);
}
public static void main(String[] args) {
//创建PdfDocument实例
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.loadFromFile("E:/xxx-已加密.pdf","123");
//实例化Workbook类的对象
Workbook workbook = new Workbook();
//清除默认工作表
workbook.getWorksheets().clear();
//实例化PdfTableExtractor类的对象
PdfTableExtractor extractor = new PdfTableExtractor(pdf);
int sheetNumber = 1;
//遍历PDF页面
for (int pageIndex = 0; pageIndex < pdf.getPages().getCount(); pageIndex++) {
//提取页面中的表格存入PdfTable[]数组
PdfTable[] tableList = extractor.extractTable(pageIndex);
if (tableList != null && tableList.length > 0) {
//遍历表格
for (PdfTable table : tableList) {
//添加工作表
Worksheet sheet = workbook.getWorksheets().add("Sheet" + sheetNumber);
//获取表格的行数和列数
int row = table.getRowCount();
int column = table.getColumnCount();
//遍历行和列
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
//获取表格单元格中的文本内容
String text = table.getText(i, j);
//将获取的文本写入Excel工作表
sheet.get(i + 1, j + 1).setValue(text);
}
}
sheetNumber++;
}
}
}
//将工作簿保存为Excel文件
workbook.saveToFile("E:/xxx-已加密.xlsx");
}
}