2012-05-29 175 views
-1

這裏我需要做的異常,但它捕捉到的是我需要將這些PDF移動到我在我的代碼'失敗文件夾中指定的文件夾」。如何擺脫字符串索引超出範圍:-1在iText

package extractInfoFromPDF; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

import com.itextpdf.text.exceptions.InvalidPdfException; 

import com.itextpdf.text.pdf.PdfReader; 
import com.itextpdf.text.pdf.parser.PdfTextExtractor; 

public class Test { 
    static FileWriter output = null; 
    public static void main(String[] args) throws IOException { 



     File file = new File("c:/write.txt"); 
     output = new FileWriter(file); 

     PdfReader pdfArticle = null; 

     Pattern pattern = Pattern.compile("\\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?![\"&\\'<>])\\S)+)\\b", Pattern.CASE_INSENSITIVE); 

     File ArticleFolder = new File("D:\\AI\\failed1"); 
     File[] listOfArticles = ArticleFolder.listFiles(); 
     int count = 0; 

     StringBuffer s = null; 

     for (File article : listOfArticles) { 

      if(!article.getName().contains("article.fulltext.000001")){ 
       continue; 
      } 

       pdfArticle = new PdfReader(article.getAbsolutePath()); 
try{ 
       s = new StringBuffer(PdfTextExtractor.getTextFromPage(pdfArticle, 1)); 
} catch (InvalidPdfException|StringIndexOutOfBoundsException|ArrayIndexOutOfBoundsException e) { 

    copyFile(article, new File ("D:\\AI\\fail")); 
    delete(article); 

} 

      // System.out.println(s); 
      Matcher m = pattern.matcher(s); 
      String DOI = null; 
      if (m.find()) { 
       DOI = m.group(); 

      } 
      if (DOI == null) { 
       Pattern pattern2 = Pattern.compile("(DOI:).*", Pattern.CASE_INSENSITIVE); 
       Matcher m2 = pattern2.matcher(s); 

       if (m2.find()) { 
        DOI = m2.group(); 
        DOI=DOI.replaceAll("\\s+", ""); 
        m = pattern.matcher(DOI); 
        if (m.find()) { 
         DOI = m.group(); 

        }else{ 
         DOI = "DOI-NOT-AVALIABLE"; 
        } 

       }else{ 
        DOI = "DOI-NOT-AVALIABLE"; 
       } 

      } 
      count = count + 1; 
      String d[]=DOI.split(" "); 

      for(String d2 : d){ 
       if(d2.contains("10.")){ 
        DOI=d2; 
       } 
      } 

      DOI = DOI.replaceAll("(DOI:)(doi:)(\\s+)([\\.,;)]])", "").trim(); 
      System.out.println(count + " TAN: " + article.getName() + "  " 
      + DOI); 
//if(DOI.matches(""[A-Z-a-z-0-7]")) 

      output.write(count + " TAN: " + article.getName() + "  " + DOI+"\n"); 

      // FileUtils.writeStringToFile(new File("write.txt"), count++ 
      // +" TAN: "+article.getName()+"  "+DOI, "UTF-8"); 

     } 

     output.close(); 


    } 


    public static void copyFile(File source, File dest) throws IOException{ 

     if(!dest.exists()){ 

     dest.createNewFile(); 

     } 

     InputStream in = null; 

     OutputStream out = null; 

     try{ 

     in = new FileInputStream(source); 

     out = new FileOutputStream(dest); 

     byte[] buf = new byte[1024]; 

     int len; 

     while((len = in.read(buf)) > 0){ 

     out.write(buf, 0, len); 

     } 

     } 

     finally{ 

     in.close(); 

     out.close(); 

     } 
    } 

    public static boolean delete(File resource) throws IOException{ 

     if(resource.isDirectory()){ 

     File[] childFiles = resource.listFiles(); 

     for(File child : childFiles){ 

     delete(child); 

     } 

     } 

     return resource.delete(); 

     } 


} 

這是我的完整代碼,下面是我得到異常的具體行。

s = new StringBuffer(PdfTextExtractor.getTextFromPage(pdfArticle, 1)); 

從哪裏獲得String index out of range從1000對PDF幾100S(-1或一些thime 0)和PDF的1000。雖然我谷歌它,這是沒有解決辦法。下面是我從iText獲得的例外。而不是從我的代碼。在我的代碼(PdfTextExtractor.getTextFromPage)的同一行,我也得到ArrayIndexOutOfBoundsException(某時間397或某些時間286或其他一些相似的3位數字)。

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 
    at java.lang.String.charAt(String.java:695) 
    at com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy.getResultantText(LocationTextExtractionStrategy.java:121) 
    at com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage(PdfTextExtractor.java:73) 
    at com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage(PdfTextExtractor.java:88) 
    at extractInfoFromPDF.Test.main(Test.java:41) 

的ArrayIndexOutOfBoundsException異常另我PDF我得到這個例外

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 397 
    at com.itextpdf.text.pdf.CMapAwareDocumentFont.getWidth(CMapAwareDocumentFont.java:182) 
    at com.itextpdf.text.pdf.parser.TextRenderInfo.getStringWidth(TextRenderInfo.java:210) 
    at com.itextpdf.text.pdf.parser.TextRenderInfo.getUnscaledWidth(TextRenderInfo.java:113) 
    at com.itextpdf.text.pdf.parser.TextRenderInfo.getUnscaledBaselineWithOffset(TextRenderInfo.java:147) 
    at com.itextpdf.text.pdf.parser.TextRenderInfo.getBaseline(TextRenderInfo.java:122) 
    at com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy.renderText(LocationTextExtractionStrategy.java:154) 
    at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.displayPdfString(PdfContentStreamProcessor.java:303) 
    at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.access$2500(PdfContentStreamProcessor.java:74) 
    at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor$ShowText.invoke(PdfContentStreamProcessor.java:496) 
    at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.invokeOperator(PdfContentStreamProcessor.java:246) 
    at com.itextpdf.text.pdf.parser.PdfContentStreamProcessor.processContent(PdfContentStreamProcessor.java:366) 
    at com.itextpdf.text.pdf.parser.PdfReaderContentParser.processContent(PdfReaderContentParser.java:79) 
    at com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage(PdfTextExtractor.java:73) 
    at com.itextpdf.text.pdf.parser.PdfTextExtractor.getTextFromPage(PdfTextExtractor.java:88) 
    at extractInfoFromPDF.Test.main(Test.java:41) 
+0

作爲一般規則,你應該張貼的代碼在問題中得到異常。 –

+0

好的,我給你完整的代碼。 –

+0

我不明白爲什麼我的問題是** **投票**' –

回答

0

所有的嘗試,我發現了異常的問題。這是因爲在其版本iText API的應用的bug 5.1,當我重建應用了最新的5.2版本我得到也不例外,每一件事工作正常:)

0

很難的,因爲更少的代碼說,我建議你設置斷點調試。

+0

斷點是沒有用的。如果你需要我的代碼的任何部分,我現在就提供它。 –

+0

好的,發電子郵件到lzjun567#gmail,讓我看看,也許我可以幫你 – liuzhijun

+0

我已經發布我的完整代碼,我希望這已經足夠。 –

相關問題