【发布时间】:2019-05-24 09:30:05
【问题描述】:
我正在从 pdf 中提取数据并存储在字符串中,从这个大数据中我想在字段中存储一些特定的数据。
我已经尝试了一些代码,它正在工作,但是如何捕获所有需要的数据?
import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
public class LdcReaderDemoNew {
private static final Pattern END_OF_SENTENCE = Pattern.compile("\\.\\s+");
public static void main(String[] args) throws IOException {
String filePath = "C:\\Users\\Admin\\Downloads\\Ldc.pdf";
String text = convertPDFToTxt(filePath);
//String[] word = {"CERTIFICATE UNDER SECTION","Certificate No","rate of"};
String word = "Certificate No";
String str = getSentence(text, word);
String str1[] = str.split(":");
String[] str2 = str1[1].split(" ");
System.out.println(str2[1]);
}
public static String convertPDFToTxt(String filePath) throws IOException {
byte[] thePDFFileBytes = readFileAsBytes(filePath);
PDDocument pddDoc = PDDocument.load(thePDFFileBytes);
PDFTextStripper reader = new PDFTextStripper();
String pageText = reader.getText(pddDoc);
pddDoc.close();
return pageText;
}
private static byte[] readFileAsBytes(String filePath) throws IOException {
FileInputStream inputStream = new FileInputStream(filePath);
return IOUtils.toByteArray(inputStream);
}
public static String getSentence(String text, String word) {
final String lcword = word.toLowerCase();
return END_OF_SENTENCE.splitAsStream(text).filter(s -> s.toLowerCase().contains(lcword)).findAny().orElse(null);
}
}
例如“我有证书编号:0218AU464E”我想将 0218AU464E 存储在一个字符串中。同样,我想在单独的字段中捕获以下数据。
1)195(2)
2)0218AU464E
3)MUMT17510D
4)SHELF DRILLING OFFSHORE SERVICES ' INDIA ) PRIVATE LIMITED 地址 4TH FLOOR CHEMTEX
5)1218694350
6)4%
7)货架钻孔 C.E.THRONTON,LTD
8)AASCS2718N
9)01-APR -18
10)31-MAR-19
CERTIFICATE UNDER SECTION 195(2) OF THEINCOME TAX ACT, 1961 RELATING TO DEDUCTION OF
TAX AT SOURCE
Office of Assistant/Deputy Commissioner ofIncome Tax
INT TAX CIRCLE4(2)(1),M
Certificate No : 0218AU464E Pnnt Date : 10-MAY-18 To,
TAN MUMT17510D
Name SHELF DRILLING OFFSHORE SERVICES ' INDIA ) PRIVATE LIMITED Address 4TH FLOOR CHEMTEX
HOUSE MAIN STREET POWAi MUMBAI
MAHARASHTRA - 400076
Ihereby authorl%e you to pay or credit Other sums upto Rs. 1218694350 after deducting income tax
at the rate of 4 % (Excluding Education cess/surcharge as applicable) to or, as the case may be to the
acoount of SHELF DRILLING C.E.THRONTON,LTD.whose details are as below :
PAN : AASCS2718N
UnlUBranch : N A.
Address : SHELF DRILLING C ETHRONTON LTD 4TH FLOOR CHEMTEX HOUSE MAIN STREET
HIRANANDANIGARDENS POWAi MUMBAI
MAHARASHTRA - 400076
over whom I have jurisdiction forissue of this certificate.
Th s certificate is non-transferable and valid for above PAN holder for payments or credit by
whatever name called whichever is earlier from 01-APR -18 to 31-MAR-19, unless It Is cancelled by
me under intimation to you before that date.
The above certificate number should be quoted in the quarter y TDS statement for the relevant
【问题讨论】:
-
pdf 是否始终具有相同的格式?例如,pdf 是否总是以 'CERTIFICATE UN SECTION "XXX" OF '..
-
是的,它将从“CERTIFICATE UNDER SECTION”开始