首页 新闻 赞助 找找看

pdfbox 获取pdf中的表格对象

0
悬赏园豆:100 [待解决问题]

pdf文件可能是动态的,表格出现的位置、表格数量不固定,如何获取?

万水千山z的主页 万水千山z | 初学一级 | 园豆:102
提问于:2016-11-21 17:04
< >
分享
所有回答(3)
0

没用过,帮顶

balahoho | 园豆:2050 (老鸟四级) | 2016-11-21 17:55
0

我们需要PDFBox的Jar包,所以我们先在百度搜索一下“PDFBox”。点击“Apache PDFBox | A Java PDF Library”这个链接。

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等辅助包。希望能帮助一些人解决问题。

Like金 | 园豆:42 (初学一级) | 2016-11-30 16:10

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

我要获取动态的表格。即表格出现的位置不是固定的。

支持(0) 反对(0) 万水千山z | 园豆:102 (初学一级) | 2016-11-30 16:13
0

一样遇到了这个需求,不知道楼主解决了没有

JillWen | 园豆:200 (初学一级) | 2018-06-25 17:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册