第一步,pom.xml中引入jar包
XML
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.24</version>
</dependency>
第二步
java
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();
}
}
}