第一步,pom.xml中引入jar包
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.24</version></dependency>
第二步
package org.example.test.example.changefile;import org.apache.pdfbox.pdmodel.PDDocument;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Base64;public class ConvertPDFToBase64String {public static void main(String[] args) {String inputPdfPath = "C:\\Users\\EDY\\Desktop\\input.pdf"; // 输入PDF文件的路径String outputTxtPath = "C:\\Users\\EDY\\Desktop\\output.txt"; // 输出TXT文件的路径try {// 加载PDF文档try (PDDocument document = PDDocument.load(new File(inputPdfPath))) {// 创建字节输出流ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();// 将PDF文档写入字节输出流document.save(byteArrayOutputStream);// 获取PDF文档的字节数据byte[] pdfBytes = byteArrayOutputStream.toByteArray();// 使用Base64编码PDF字节数据String base64EncodedString = Base64.getEncoder().encodeToString(pdfBytes);// 将Base64编码的字符串写入TXT文件try (FileWriter writer = new FileWriter(outputTxtPath)) {writer.write(base64EncodedString);}System.out.println("PDF file has been converted to Base64 string and saved to TXT file.");}} catch (IOException e) {e.printStackTrace();}}
}