Jon*_*ero 10
将这个简单的程序编译打包成jar
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripperByArea;
public class ExtractText {
// Usage: xxx.jar filepath page x y width height
public static void main(String[] args) throws IOException {
if (args.length != 6) {
System.out.println("Help info");
return;
}
// Parameters
String filepath = args[0];
int page = Integer.parseInt(args[1]);
int x = Integer.parseInt(args[2]);
int y = Integer.parseInt(args[3]);
int width = Integer.parseInt(args[4]);
int height = Integer.parseInt(args[5]);
PDDocument document = PDDocument.load(new File(filepath));
PDFTextStripperByArea textStripper = new PDFTextStripperByArea();
Rectangle2D rect = new java.awt.geom.Rectangle2D.Float(x, y, width, height);
textStripper.addRegion("region", rect);
PDPage docPage = document.getPage(page);
textStripper.extractRegions(docPage);
String textForRegion = textStripper.getTextForRegion("region");
System.out.println(textForRegion);
}
}
Run Code Online (Sandbox Code Playgroud)
从命令行运行它,例如:
xxx.jar filepathToPdf pageToExtract x y width height
Run Code Online (Sandbox Code Playgroud)
添加参数的验证代码和一些使用信息。
编辑
还要添加 PDFbox 库
java -cp "..." -jar xxx.jar filepathToPdf pageToExtract x y width height
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4561 次 |
| 最近记录: |