微网站和手机网站,厦门网站建设公司闽icp,app开发公司怎么收费,高端定制网站设计公司使用该方法请注意#xff1a;因为此方法使用了base64解密#xff0c;需要保证txt中的数据首先用了base64加密#xff0c;如果只是普通的二进制数据#xff0c;该方法并不适用
第一步 dependencygroupIdorg.apache.pdfbox/groupIdartifactId因为此方法使用了base64解密需要保证txt中的数据首先用了base64加密如果只是普通的二进制数据该方法并不适用
第一步 dependencygroupIdorg.apache.pdfbox/groupIdartifactIdpdfbox/artifactIdversion2.0.24/version
/dependency 第二步
package org.example.test.example.changefile;import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;public class ParsePDFFromString {public static void main(String[] args) {// 输入txt文件的路径String txtFilePath C:\\Users\\EDY\\Desktop\\input.txt;// 输出PDF文件的路径String outputPdfPath C:\\Users\\EDY\\Desktop\\output.pdf;try {// 读取txt文件内容String base64Data new String(Files.readAllBytes(Paths.get(txtFilePath))).trim();// 检查Base64数据是否为空if (base64Data.isEmpty()) {System.out.println(Base64 data is empty.);return;}// Base64解码byte[] pdfBytes java.util.Base64.getDecoder().decode(base64Data);// 检查解码后的数据是否为空if (pdfBytes.length 0) {System.out.println(Decoded PDF data is empty.);return;}// 将解码后的数据转换为PDF文件try (PDDocument document PDDocument.load(new ByteArrayInputStream(pdfBytes))) {// 创建PDFTextStripper对象PDFTextStripper stripper new PDFTextStripper();// 从PDF文档中提取文本String text stripper.getText(document);// 输出提取的文本System.out.println(text);// 保存PDF文档document.save(outputPdfPath);}} catch (IOException e) {e.printStackTrace();}}
}