简体   繁体   中英

while parsing a pdf document using org.apache.pdfbox and java , '-' is converted as '?'

PDPDDocument pdfDoc = PDDocument.load(input);
PDFTextStripper stripper=new PDFTextStripper();
stripper.setSortByPosition(true);
stripper.fixedCharWidth =5; 
String text= stripper.getText(pdfDoc);

When I observed the output for input string in the pdf file: 07‑Jul‑2014 / 7/ 2014

Output of the above line is like this: 07?JUL?2014 / 7/ 2014

Here it is a work around (at least for me) : 
String DD_MM_YYYY_DATE_FORMAT_REGEX="[0-9]{1,2}(\\?)[a-zA-Z]{3}(\\?)[0-9]{4}";//string length=10
String DD_MMM_YYYY_DATE_FORMAT_REGEX="[0-9]{1,2}(\\?)[a-zA-Z]{3}(\\?)[0-9]{4}";//length=11

if(wordArray[index].substring(0,11).matches(DD_MMM_YYYY_DATE_FORMAT_REGEX))
{
        wordArray[index]=wordArray[index].replaceAll("\\?", "/");
}
                                if(wordArray[index].substring(0,10).matches(DD_MM_YYYY_DATE_FORMAT_REGEX))
{
    wordArray[index]=wordArray[index].replaceAll("\\?", "/");
}

It looks like an encoding issue. Seeing as you won't share the PDF, I can only suggest trying the following:

PDFTextStripper stripper=new PDFTextStripper("UTF-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM