完整实例代码
/**
* 获取步骤
* 1.使用 PDDocument 解析文件
* 2.创建 new PDFTextStripperByArea();
* PDFTextStripperByArea:这是一个工具类,专门用于从 PDF 页面中提取指定区域的文本。
stripper:是你创建的 PDFTextStripperByArea 对象实例,用于调用相关方法。
3.获取对应的坐标
说明:// 这个四边形所在区域在 y轴向下为正,x轴向右为正。
/*int x = 70;//x轴 越大往右移动
int y = 30; //y轴 越大向下移动
int width = 200;//区域宽度
int height = 40;//区域高度
**/
public static Map<String, String> readTydzfp(File file) throws Exception {
Map<String, String> resMap = new HashMap<>();
//
PDDocument doc = PDDocument.load(file);
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
// 票据代码
Rectangle2D rect = new Rectangle(490, 50, 200, 10);
stripper.addRegion("fpdm", rect);
PDPage page = doc.getPage(0);
stripper.extractRegions(page);
String fpdm = stripper