目录
前言:
Apache POI和pdfbox库实现pdf转excel的详细代码实现:
综上所述:
Java实现iText pdf转excel详细代码实现
综上所述:
依赖:
前言:
笔记而已
Apache POI和pdfbox库实现pdf转excel的详细代码实现:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class PdfToExcelConverter {public static void main(String[] args) {String pdfFilePath = "sample.pdf";String excelFilePath = "output.xlsx";try {String pdfText = getPdfText(pdfFilePath);writeTextToExcel(pdfText, excelFilePath);System.out.println("PDF successfully converted to Excel!");} catch (IOException e) {e.printStackTrace();}}private static String getPdfText(String pdfFilePath) throws IOException {PDDocument pdfDoc = PDDocument.load(new FileInputStream(new File(pdfFilePath)));PDFTextStripper stripper = new PDFTextStripper();String pdfText = stripper.getText(pdfDoc);pdfDoc.close();return pdfText;}private static void writeTextToExcel(String pdfText, String excelFilePath) throws IOException {boolean isXlsx = excelFilePath.endsWith(".xlsx");Workbook workbook = isXlsx ? new XSSFWorkbook() : new HSSFWorkbook();Sheet sheet = workbook.createSheet("Sheet1");String[] lines = pdfText.split("\n");int rowIndex = 0;for (String line : lines) {Row row = sheet.createRow(rowIndex++);String[] cells = line.split(" ");int cellIndex = 0;for (String cellValue : cells) {Cell cell = row.createCell(cellIndex++);cell.setCellValue(cellValue);}}workbook.write(new File(excelFilePath));workbook.close();}}
综上所述:
- 通过pdfbox库获取pdf文件的文本内容。
- 使用Apache POI创建一个Excel工作簿对象和一个工作表对象。
- 将pdf文本内容逐行添加到工作表中的单元格中。
- 将工作簿写入Excel文件中。
Java实现iText pdf转excel详细代码实现
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.Vector;
import com.itextpdf.text.pdf.parser.VectorParserListener;
import com.itextpdf.text.pdf.parser.VectorRenderFilter;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import com.itextpdf.text.pdf.parser.RenderFilter;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.parser.PdfContentStreamProcessor;
import com.itextpdf.text.pdf.parser.PdfRenderListener;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import com.itextpdf.text.pdf.parser.Vector;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class PdfToExcelConverter {public static void main(String[] args) {try {String pdfFilePath = "sample.pdf";String excelFilePath = "output.xlsx";// 读取PDF文件PdfReader reader = new PdfReader(new FileInputStream(pdfFilePath));PdfContentStreamProcessor processor = new PdfContentStreamProcessor(new RenderListener() {private List<String> lines = new ArrayList<>();private String currentLine = "";private boolean newLine = false;@Overridepublic void renderText(TextRenderInfo renderInfo) {String text = renderInfo.getText();Vector baseline = renderInfo.getBaseline().getStartPoint();float x = baseline.get(Vector.I1);float y = baseline.get(Vector.I2);if (newLine || currentLine.isEmpty()) {currentLine = text;newLine = false;} else {currentLine += " " + text;}if (text.endsWith("\n")) {currentLine = currentLine.trim();if (!currentLine.isEmpty()) {lines.add(currentLine);}currentLine = "";newLine = true;}}@Overridepublic void renderImage(ImageRenderInfo renderInfo) {}@Overridepublic void endTextBlock() {}@Overridepublic void beginTextBlock() {}});for (int i = 1; i <= reader.getNumberOfPages(); i++) {processor.processContent(reader.getPageContent(i), reader.getPageResources(i));}reader.close();// 将读取的内容写入Excel文件Workbook workbook = new XSSFWorkbook();Sheet sheet = workbook.createSheet();int rowIndex = 0;for (String line : lines) {Row row = sheet.createRow(rowIndex++);String[] cells = line.split(" ");int cellIndex = 0;for (String cellValue : cells) {Cell cell = row.createCell(cellIndex++);cell.setCellValue(cellValue);}}FileOutputStream outputStream = new FileOutputStream(new File(excelFilePath));workbook.write(outputStream);workbook.close();System.out.println("PDF successfully converted to Excel!");} catch (IOException e) {e.printStackTrace();}}
}
综上所述:
- 使用iText库的
PdfReader
类读取PDF文件,使用PdfContentStreamProcessor
类解析PDF内容,并将其转换为字符串。 - 将解析得到的字符串逐行添加到Excel工作表中的单元格中。
- 将工作簿写入Excel文件中。
需要注意的是,iText库的解析功能支持多种PDF格式和语言,也可以处理文本、表格、图像、链接等内容。因此,使用iText库可以更加灵活地处理PDF文件的内容。
依赖:
在你的项目中使用iText库,需要在你的项目中引入以下依赖: 根据版本情况来
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13</version>
</dependency>在你的项目中使用Apache POI库,需要在你的项目中引入以下依赖:<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.0.0</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.0.0</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>5.0.0</version>
</dependency>