为什么PDF文件需要LOG4J& SLF4J? .Doc文件为何不需要?

时间:2017-06-26 14:14:49

标签: java file-io log4j slf4j pdfbox

我正在开发一个简单的应用程序,它使用PDFBox api为PDF文档提取文本并将其放入Excel中。用于excel文件的POIFSFilesystem(HSSFWorkbook)。最近我开发了一个应用程序,它从.doc文件中提取文本并放入excel,那时我从未遇到过LOGGER问题。这次系统抛出了几个错误[How to find specific org/slf4j/Logger jar file out of multiple bindings from the apache zip? 我红了Apache logging它说配置日志记录。我没有在我的应用程序中开发任何与Web相关的功能。添加jar文件是不够的?我红色https://www.slf4j.org/codes.html这个错误处理文章我从来没有找到与应用程序相关的特定错误。

log4j:WARN No appenders could be found for logger (org.apache.pdfbox.io.ScratchFileBuffer).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

您可以在下面找到我的代码,其中包含POI API& PDFBOX API。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FIRST {

    //private static WordExtractor we;
    //static InputStream inc;
    static PDDocument pdDoc = null;
        public static void main(String[] args) throws IOException  {
             POIFSFileSystem fs = null;

        //   String target_dir = "E:\\TESTTRS";
                //File dir = new File(target_dir);
              //  File[] files = dir.listFiles();

                String target_dir = "C:\\Users";
                File dir = new File(target_dir);
                File[] files = dir.listFiles();

                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet = workbook.createSheet("firstsheet");
                Row row0 = sheet.createRow(0);
                row0.createCell(0).setCellValue("S.NO");
                row0.createCell(1).setCellValue("DOCUMENT");
                row0.createCell(2).setCellValue("VALUE1");
                row0.createCell(3).setCellValue("VALUE2");
                row0.createCell(4).setCellValue("TEND");

        int j = 1;
        for ( int s=0;s<files.length;s++){
        if(files[s].isFile()){
            pdDoc = PDDocument.load(files[s]);
            //fs = new POIFSFileSystem(new FileInputStream(files[s]));
            PDFTextStripper Stripper =  new PDFTextStripper();
            String st = Stripper.getText(pdDoc);
            String linesp = System.lineSeparator();
             String[] paragraph = st.split(linesp);
            //HWPFDocument doc = new HWPFDocument(fs);
            //we = new WordExtractor(doc);
            //String[] paragraph= we.getParagraphText();

                                    Row row1 = sheet.createRow(j);
/***************************1_PRINTS S.NO *************************************/
                                        Cell cell_10 =row1.createCell(0);
                                            cell_10.setCellValue(j);
                                            j++;
/***************************2_PRINTS FILE NAMES *********************************/                                          
        Cell cell_11 = row1.createCell(1);
        cell_11.setCellValue(files[s].getName());
/******************************3_PRINTS VALUE1*****************************************/
                 Cell cell_12 = row1.createCell(2);

                 String len = files[s].getName().substring(13, 19);
                 cell_12.setCellValue(len);
 /**********************4_PRINTS VALUE2 *******************************/        
                 Cell cell_13 = row1.createCell(3);
                            for(String p: paragraph){
                                        if(p.startsWith("VALUE2"))
              cell_13.setCellValue(p.substring(22));
             }      

    /*******************5_PRINTS TEND*****************************************/      
             Cell cell_14 = row1.createCell(4);
         for(String pp: paragraph){
              if(pp.contains("TEND"))
          cell_14.setCellValue(pp);
                }

         /**************6_TEST PATTERNS*********************************************/
                Cell cell_15 = row1.createCell(5);
                    for(String c : paragraph){

                        final String regex = ("^.*([0-9]{6}\\/[A-Z][0-9]{5}).*$");
                        Pattern pattern = Pattern.compile(regex);
                        Matcher matcher = pattern.matcher(c);
                        for (int i = 1; i < matcher.groupCount(); i++) {
                        if(c.startsWith("COMMENT:"))
                            cell_15.setCellValue(""+matcher.group(i));
                    }
                }

            workbook.write(new FileOutputStream("C:\\Users\\abc.xls"));

    workbook.close();
                }
        pdDoc.close();
}}}

1 个答案:

答案 0 :(得分:0)

我通过将maven依赖项与我项目的相关mvnrepository构建到pom.xml中来解决它。并使用适当的根记录器和“src”在“src”中配置log4j.properties。 log4j appenders。如果我们想使用PDFBox API解析PDF文档。