如何使用java中的pdfbox从pdf文件中删除页眉和页脚

时间:2013-08-08 12:28:04

标签: java pdfbox

我正在使用Pdf Parser将pdf转换为text.Below是我使用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提取为text.But我的要求是忽略页眉和页脚并仅从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.

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

2 个答案:

答案 0 :(得分:5)

通常,PDF中的页眉或页脚文本没有什么特别之处。可以不同地标记该材料,但标记是可选的,OP不提供要检查的样本PDF。

因此,通常需要一些手动工作(或有些故障密集的图像分析)才能在页面上找到标题,内容和页脚材料的区域。

但是,只要您拥有这些区域的坐标,就可以使用扩展PDFTextStripperByArea的{​​{1}}按区域收集文本。只需使用包含内容但不包括页眉和页脚的最大矩形定义页面内容的区域,并在PDFTextStripper调用pdfStripper.getText(pdDoc)后定义区域。

答案 1 :(得分:0)

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