java pdf to text
时间: 2024-05-12 22:19:13 浏览: 54
To convert a PDF file to text using Java, you can use the Apache PDFBox library. Here's a sample code:
```
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
public class PDFToTextConverter {
public static void main(String[] args) {
try {
File file = new File("input.pdf");
PDDocument document = PDDocument.load(file);
PDFTextStripper pdfStripper = new PDFTextStripper();
String text = pdfStripper.getText(document);
System.out.println(text);
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
This code loads a PDF file named "input.pdf", extracts its text using PDFBox's PDFTextStripper class, and prints the text to the console. You can modify the code to save the text to a file or process it further as needed.