java 使用pdfbox批量转换pdf写入txt

1. 包引用

<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>pdfbox</artifactId>
  <version>2.0.24</version>
</dependency>

2 Demo

package pdf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class Pdf2Txt {
	
	public static String base_path = "";
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入pdf文件夹所在的目录:");
		String path = sc.nextLine();
		base_path = path + "/";
		sc.close();
		getFiles();
	}
	
	public static void getFiles() {
		
		File file = new File(base_path+"pdfs");
		File[] fileArray = file.listFiles();
		if(fileArray == null || fileArray.length == 0) {
			System.out.print("未查找到pdf文件,请检查目录是否正确...");
			System.exit(0);
			return;
		}
		for(int i = 0; i < fileArray.length; i++) {
			File f = fileArray[i];
			String fileName = f.getName();
			getTxt(fileName);
		}
	}
	
	public static void getTxt(String name) {
		try {
			// pdfs为存放pdf文件的文件夹,可自行修改为其它文件夹
			PDDocument doc = PDDocument.load(new File(base_path + "pdfs/" +name));
			if(doc.isEncrypted()) {
				System.out.println(name + "文档被加密,无法解析....");
				return;
			}
			
			PDFTextStripper stripper = new PDFTextStripper();
			writedText(stripper.getText(doc), name);
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void writedText(String result, String name) {
		String fileNameWithoutExtension = name.substring(0, name.lastIndexOf('.'));
		try {
			// txts为存放结果的文件夹,可自行修改此名称,并事先新建好。
			String path = base_path + "txts/" + fileNameWithoutExtension + ".txt";
			File file = new File(path);
			if(!file.exists()) {
				file.createNewFile();
			}
			
			OutputStream os = new FileOutputStream(file);
			OutputStreamWriter writer = new OutputStreamWriter(os);
			writer.write(result);
			writer.close();
			os.close();
			System.out.println(name + " : 提取完成...");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您好!关于您的问题,我可以为您提供一些帮助。 您可以使用以下步骤实现向PDF内追加文本: 1. 首先,您需要在项目中加入PDFBox库文件和字体文件。 2. 创建一个PDDocument对象,该对象代表正在编辑的PDF文档。 3. 使用PDPage类在文档中添加一个新的页面。 4. 创建一个PDPageContentStream对象,该对象用于编辑页面内容。 5. 使用setFont()方法设置所需的字体。 6. 使用beginText()方法开始编辑文本,并使用showText()方法添加文本。 7. 使用endText()方法结束编辑。 8. 使用close()方法关闭PDPageContentStream对象。 9. 使用save()方法保存修改。 10. 使用close()方法关闭PDDocument对象。 下面是一个示例代码,可以让您更好地了解如何使用PDFBoxPDF中追加文本: ``` import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class AddTextToPDF { public static void main(String args[]) throws IOException { // Create a document and add a page to it PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); // Initialize a stream to write to the page content PDPageContentStream contentStream = new PDPageContentStream(document, page); // Set the font to Helvetica contentStream.setFont(PDType1Font.HELVETICA, 12); // Begin editing the page content contentStream.beginText(); // Write some text to the page contentStream.showText("Hello, World!"); // End editing the page content contentStream.endText(); // Save the changes and close the content stream contentStream.close(); // Save the document to a file and close it document.save(new File("output.pdf")); document.close(); } } ``` 希望以上内容对您有所帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值