我有 200 多个 pdf 报告文件,我需要从每个报告中获取 VIN# 和案例编号,然后使用 VIN + Case#.pdf 重命名报告。
对于 VIN#,很容易获得它,因为它始终位于页面的开头,并且 VIN 的固定长度为 17 个字符。
我遇到案例编号问题,我无法获得确切数字,因为“案例编号”的索引根据“案例编号”之前的每个单元格中的单词数从报告更改为另一个报告”的细胞。
我的问题是:如何告诉 java 给我两个空格之间的字符串,其中一个在“案例编号”之后,第二个在单元格“系统密钥”之前
我试图用空格分割所有单词,但我陷入了如何真正获得该特定数字的逻辑上,尽管它有索引号。
注意:箱号总是不同的,长度也不一样
这是我到目前为止所拥有的:
package Read_Pdf_AsA_Text;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
public class GetVinAndCaseNum {
public static void main(String args[]) throws IOException {
File folder = new File("C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\Tasks\\test\\");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
File f = new File("C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\Tasks\\test\\"+listOfFiles[i].getName());
PDDocument document = PDDocument.load(f);
PDFTextStripper pdfStripper = new PDFTextStripper(); // Instantiate PDFTextStripper class
String text = pdfStripper.getText(document); // Retrieving text from PDF document
System.out.println(text);
if (text.contains("VIN")) {
int vinIndexIs = text.indexOf("VIN");
int newVINIndex = vinIndexIs + 3;
String vinNum = text.substring(newVINIndex, newVINIndex + 19);
System.err.println("New VIN is ===> " + vinNum);
}
int caseNo = 0;
if (text != null) {
String[] spcase = text.split(" ");
System.out.println("spaces ==> " + spcase);
boolean foundCaseNumber = false;
for (String stringAfterSpace : spcase) {
System.out.println("stringAfterSpace ==> " + stringAfterSpace);
if(foundCaseNumber) {
caseNo = Integer.parseInt(stringAfterSpace.trim());
System.out.println("caseNo ==> " + caseNo);
break;
}
if("Case Number".equals(stringAfterSpace)) {
System.out.println("Case Number issss ===> " + stringAfterSpace);
foundCaseNumber = true;
}
}
if(caseNo == 0) {
System.out.println("Case No. not found.");
}
}
document.close();
System.out.println("conversion is done");
}
}
}
}
我找到了一个解决方案,如下所示:我用空格分割文本,然后用 CaseNumber 替换 Case Number,这样我就可以将“Case”和“Number”之间的空格变成红色。然后我做了一些逻辑如下:
String caseNum = "";
if (text != null) {
String[] spcase = text.replace("Case Number", "CaseNumber").split(" ");
boolean foundCaseNum = false;
for (String stringAfterSpace : spcase) {
if(foundCaseNum) {
caseNum = stringAfterSpace.trim();
System.err.println("Case Number is ==> " + caseNum);
break;
}
if(stringAfterSpace.contains("CaseNumber")) {
foundCaseNum = true;
}
}
if(caseNum.isEmpty()) {
System.out.println("Case No. not found.");
}
}
document.close();
System.out.println("conversion is done");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句