3

我正在使用 Pdf Parser 将 pdf 转换为文本。下面是我使用 java 将 pdf 转换为文本文件的代码。我的 PDF 文件包含以下数据:

    Data Sheet(Header)
    PHP Courses for PHP Professionals(Header)

   Networking Academy
    We live in an increasingly connected world, creating a global economy and a growing need for technical skills.  Networking Academy delivers information technology skills to over 500,000 students a year in more than 165 countries worldwide. Networking Academy students have the opportunity to participate in a powerful and consistent learning experience that is supported by high quality, online curricula and assessments, instructor training, hands-on labs, and classroom interaction. This experience ensures the same level of qualifications and skills regardless of where in the world a student is located.

    All copyrights reserved.(Footer).

示例代码:

public class PDF_TEST {
    PDFParser parser;
    String parsedText;
    PDFTextStripper pdfStripper;
    PDDocument pdDoc;
    COSDocument cosDoc;
    PDDocumentInformation pdDocInfo;

    // PDFTextParser Constructor 
    public PDF_TEST() {
    }

    // Extract text from PDF Document
    String pdftoText(String fileName) {


        File f = new File(fileName);

        if (!f.isFile()) {

            return null;
        }

        try {
            parser = new PDFParser(new FileInputStream(f));
        } catch (Exception e) {

            return null;
        }

        try {
            parser.parse();
            cosDoc = parser.getDocument();
            pdfStripper = new PDFTextStripper();
            pdDoc = new PDDocument(cosDoc);
            parsedText = pdfStripper.getText(pdDoc); 
        } catch (Exception e) {

            e.printStackTrace();
            try {
                   if (cosDoc != null) cosDoc.close();
                   if (pdDoc != null) pdDoc.close();
               } catch (Exception e1) {
               e.printStackTrace();
            }
            return null;
        }      

        return parsedText;
    }

    // Write the parsed text from PDF to a file
    void writeTexttoFile(String pdfText, String fileName) {


        try {
            PrintWriter pw = new PrintWriter(fileName);
            pw.print(pdfText);
            pw.close();     
        } catch (Exception e) {

            e.printStackTrace();
        }

    }

    //Extracts text from a PDF Document and writes it to a text file
    public static void test() {
        String args[]={"C://Sample.pdf","C://Sample.txt"};
        if (args.length != 2) {

            System.exit(1);
        }

        PDFTextParser pdfTextParserObj = new PDFTextParser();


        String pdfToText = pdfTextParserObj.pdftoText(args[0]);

        if (pdfToText == null) {

        }
        else {

            pdfTextParserObj.writeTexttoFile(pdfToText, args[1]);
        }
    }  

    public static void main(String args[]) throws IOException
    {
        test();
    }
}

上面的代码适用于将 pdf 提取为文本。但我的要求是忽略页眉和页脚,只从 pdf 文件中提取内容。所需输出:

Networking Academy
        We live in an increasingly connected world, creating a global economy and a growing need for technical skills.  Networking Academy delivers information technology skills to over 500,000 students a year in more than 165 countries worldwide. Networking Academy students have the opportunity to participate in a powerful and consistent learning experience that is supported by high quality, online curricula and assessments, instructor training, hands-on labs, and classroom interaction. This experience ensures the same level of qualifications and skills regardless of where in the world a student is located.

请建议我如何做到这一点。谢谢。

4

2 回答 2

6

一般来说,PDF 中的页眉或页脚文本没有什么特别之处。可以以不同的方式标记该材料,但标记是可选的,并且 OP 没有提供示例 PDF 来检查。

因此,通常需要一些手动工作(或有些失败的图像分析)来查找页面上的页眉、内容和页脚材料区域。

但是,一旦您有了这些区域的坐标,您就可以使用PDFTextStripperByAreawhich extends thePDFTextStripper来按区域收集文本。只需使用包含内容但不包括页眉和页脚的最大矩形为页面内容定义一个区域,然后pdfStripper.getText(pdDoc)调用getTextForRegion定义的区域。

于 2014-03-06T07:44:30.090 回答
1
You can use PDFTextStripperByArea to remove "Header" and "Footer" by pdf file.
Code in java using PDFBox.

 public String fetchTextByRegion(String path, String filename, int pageNumber) throws IOException {
        File file = new File(path + filename);
        PDDocument document = PDDocument.load(file);
        //Rectangle2D region = new Rectangle2D.Double(x,y,width,height);
        Rectangle2D region = new Rectangle2D.Double(0, 100, 550, 700);
        String regionName = "region";
        PDFTextStripperByArea stripper;
        PDPage page = document.getPage(pageNumber + 1);
        stripper = new PDFTextStripperByArea();
        stripper.addRegion(regionName, region);
        stripper.extractRegions(page);
        String text = stripper.getTextForRegion(regionName);
        return text;
    }
于 2018-08-10T11:32:41.660 回答