iText を使用して PDF ドキュメントを読み込んでいます。ArrayIndexOutOfBoundsException が発生しています。奇妙なことは、特定のファイルとそれらのファイルの特定の場所でのみ発生することです。PDFがそれらの場所でエンコードされる方法に関係していると思われますが、何が問題なのかわかりません。
この質問Read pdf using iText を見てきましたが、彼はこのファイルの場所を変更することで問題を解決したようです。一部のファイル内の特定の場所で例外が発生するため、これは機能しません。したがって、例外を引き起こしているのはファイル自体ではなく、問題のページです。
スタックトレースは
スレッド「メイン」の例外 java.lang.ArrayIndexOutOfBoundsException: 無効なインデックス: 02 com.lowagie.text.pdf.CMapAwareDocumentFont.decodeSingleCID (不明なソース) com.lowagie.text.pdf.CMapAwareDocumentFont.decode (不明なソース) で com .lowagie.text.pdf.parser.PdfContentStreamProcessor.decode (不明なソース) com.lowagie.text.pdf.parser.PdfContentStreamProcessor.displayPdfString (不明なソース) com.lowagie.text.pdf.parser.PdfContentStreamProcessor$ShowText.invoke (不明なソース) com.lowagie.text.pdf.parser.PdfContentStreamProcessor.invokeOperator (不明なソース) com.lowagie.text.pdf.parser.PdfContentStreamProcessor.processContent (不明なソース) com.lowagie.text.pdf.parser com.pdfextractor.main.Extractor の .PdfTextExtractor.getTextFromPage(不明なソース)。メイン (Extractor.java:61)
61 行目は次の行に対応します。
content = extractor.getTextFromPage(page);
したがって、getTextFromPage() メソッドが機能していないことは明らかです。
public static void main(String[] args) throws IOException{
ArrayList<String> keywords = new ArrayList<String>();
keywords.add("location");
keywords.add("Mass Spectrometry");
keywords.add("vacuole");
keywords.add("cytosol");
String directory = "C:/Ankur/Projects/PEB/Extractor/papers/";
File directoryToRead = new File(directory);
String[] sa_filesToRead = directoryToRead.list();
List<String> filesToRead = Arrays.asList(sa_filesToRead);
Iterator<String> fileItr = filesToRead.iterator();
while(fileItr.hasNext()){
String nextFile = fileItr.next();
PdfReader reader = new PdfReader(directory+nextFile);
int noPages = reader.getNumberOfPages();
PdfTextExtractor extractor = new PdfTextExtractor(reader);
String content="";
for(int page=1;page<=noPages;page++){
int index = 1;
System.out.println(page);
content = extractor.getTextFromPage(page);
}
}
}