如何摆脱String索引超出范围:iText中的-1

时间:2012-05-29 15:26:49

标签: java itext

我需要对异常进行处理,我需要将这些PDF移动到我在代码'fail folder'中指定的文件夹。

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));

我从{1000}和1000s的PDF获得PDF的String index out of range(-1或某些时间0)几百秒。虽然我谷歌它,但没有解决方案。当我从iText获得时,下面是例外。而不是我的代码。对于某些pdf,我的代码ArrayIndexOutOfBoundsException中的同一行也会获得(PdfTextExtractor.getTextFromPage)(某些时间为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)

for 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)

1 个答案:

答案 0 :(得分:0)

经过一切尝试,我发现了获得异常的问题。这是因为其版本5.1 中的iText API错误,当我使用最新的版本5.2 重建我的应用程序时,我没有例外,并且每件事都运行良好:)