JAVA PDFBOX 读取PDF表格

阅读更多

最近在帮公司做工具,需要读取PDF中表格的数据。网上查了,大部分PDFBox读取的代码都大致相同,一行一行从头读到尾。尝试读取PDF表格的人可能会遇到表格有空数据时,列与列就会对不齐,这样就不能很好地进行数据的处理了。网上看到一个例子,用iText坐标精确读取的例子,参考以后出现了亚洲语种字体不支持,添加了语言包iTextAsian.jar导入字体后,结果发现打印的都是空格无法处理。后找到了PDFBox坐标读取的方法,相当给力。在此过程中了解到有很多人遇到了我这样的问题。所以写下来望对现在还未解决问题还有以后遇到此问题的人提供帮助。

 

上代码:

 

package com.pdfbox.util.test;

 

import org.apache.pdfbox.exceptions.InvalidPasswordException;

 

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.pdmodel.PDPage;

import org.apache.pdfbox.util.PDFTextStripperByArea;

 

import java.awt.Rectangle;

 

import java.util.List;

 

 

public class ExtractTextByArea

{

    private ExtractTextByArea()

    {

 

    }

 

    public static void main( String[] args ) throws Exception

    {

    String file = "H:\123.pdf";

            PDDocument document = null;

            try

            {

                document = PDDocument.load( file);

                if( document.isEncrypted() )

                {

                    try

                    {

                        document.decrypt( "" );

                    }

                    catch( InvalidPasswordException e )

                    {

                        System.err.println( "Error: Document is encrypted with a password." );

                        System.exit( 1 );

                    }

                }

                PDFTextStripperByArea stripper = new PDFTextStripperByArea();

                stripper.setSortByPosition( true );

                Rectangle rect = new Rectangle( 10, 280, 275, 60 );

                stripper.addRegion( "class1", rect );

                List allPages = document.getDocumentCatalog().getAllPages();

                PDPage firstPage = (PDPage)allPages.get( 0 );

                stripper.extractRegions( firstPage );

                System.out.println( "Text in the area:" + rect );

                System.out.println( stripper.getTextForRegion( "class1" ) );

 

            }

            finally

            {

                if( document != null )

                {

                    document.close();

                }

            }

    }

 

}

 

 

 

PDFBox.jar最好用1.7及以上版本的,它包含了fontbox和jempbox等辅助包。希望能帮助一些人解决问题。

--------------------- 

作者:bacoder 

来源:CSDN 

原文:https://blog.csdn.net/bacoder/article/details/43536157 

版权声明:本文为博主原创文章,转载请附上博文链接!

 

 

 

首先:我们要了解以下的代码含义:

 

Rectangle rectFirstPage = new Rectangle( 220, 130, 130, 900 );

 

他其实对应的就是以下图示内容:

 

 

其次:上代码

 

      PDDocument document = PDDocument.load(new File("E:\\2018workFolderShun\\chinaCC测试\\财务\\2017-4分类.pdf"));

      PDFTextStripper textStripper = new PDFTextStripper();

//      System.out.println(textStripper.getText(document));

//      document.close();

      PDFTextStripperByArea stripper = new PDFTextStripperByArea();

      

      stripper.setSortByPosition( true );

      Rectangle rectFirstPage = new Rectangle( 220, 130, 130, 900 );

      stripper.addRegion( "class1", rectFirstPage );

      PDPageTree allPages = document.getDocumentCatalog().getPages();

      PDPage firstPage = (PDPage)allPages.get(0);

      stripper.extractRegions( firstPage );

      System.out.println( stripper.getTextForRegion( "class1" ) );

      System.err.println("-------------------------------");

      Rectangle rectFrom2 = new Rectangle( 220, 70, 130, 900 );

      stripper.addRegion( "class1", rectFrom2 );

      for (int i = 1; i < allPages.getCount(); i++) {

          firstPage = (PDPage)allPages.get(i);

          stripper.extractRegions( firstPage );

          System.out.println( stripper.getTextForRegion( "class1" ) );

}

最后:依赖包

 

 

--------------------- 

作者:顺子在帝都2017 

来源:CSDN 

原文:https://blog.csdn.net/shunzi2016/article/details/80243262 

版权声明:本文为博主原创文章,转载请附上博文链接!

 

任务是提取pdf文件中的数据,并将其转化到csv格式的文件中。

 

首先用到的工具为开源jar包:pdfbox,由于pdf中没有表结构,且要提取的pdf文件格式固定,pdfbox提供extractbyarea方法,所以通过坐标可以提取表格中每一列的数据,好像方法挺笨的,但是没找到其他跟好的方法。

 

关于java操作pdf,在网上看到好多人说itext比pdfbox方便,功能强大,但是itext好像只是在生成pdf文件时比较方便,图表插入很方便,但是不太适合读取pdf中的信息。

 

然后是将提取的信息保存到csv文件中,居然还有个开源的javacsv.jar,用起来还是比较方便的

--------------------- 

作者:dragonfly 

来源:CSDN 

原文:https://blog.csdn.net/fan_long_fei/article/details/38335225 

版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(JAVA PDFBOX 读取PDF表格)