当前位置: 代码网 > it编程>编程语言>Java > Java代码实现轻松读取PDF中表格的完整指南

Java代码实现轻松读取PDF中表格的完整指南

2026年09月03日 Java 我要评论
本文以java示例展示读取pdf中的表格的方法。1、方法1:spire.pdf1.1 maven仓库下载导入在pom.xml中配置maven路径,指定依赖,如下:<repositories>

本文以java示例展示读取pdf中的表格的方法。

1、方法1:spire.pdf

1.1 maven仓库下载导入

在pom.xml中配置maven路径,指定依赖,如下:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupid>e-iceblue</groupid>
        <artifactid>spire.pdf</artifactid>
        <version>4.10.2</version>
    </dependency>
</dependencies>

1.2 读取pdf中的表格

代码

package com;

import com.spire.pdf.pdfdocument;
import com.spire.pdf.utilities.pdftable;
import com.spire.pdf.utilities.pdftableextractor;

import java.io.filewriter;
import java.io.ioexception;

public class extracttable {
    public static void main(string[] args)throws ioexception {
        //加载pdf文档
        pdfdocument pdf = new pdfdocument();
        pdf.loadfromfile("e:\\文档\\金融数据抽取\\公告\\600519_贵州茅台_贵州茅台2019年年度报告.pdf");

        //创建stringbuilder类的实例
        stringbuilder builder = new stringbuilder();

        //抽取表格
        pdftableextractor extractor = new pdftableextractor(pdf);
        pdftable[] tablelists ;
        for (int page = 0; page < pdf.getpages().getcount(); page++) {
            tablelists = extractor.extracttable(page);
            if (tablelists != null && tablelists.length > 0) {
                for (pdftable table : tablelists)
                {
                    int row = table.getrowcount();
                    int column = table.getcolumncount();
                    for (int i = 0; i < row; i++)
                    {
                        for (int j = 0; j < column; j++)
                        {
                            string text = table.gettext(i, j);
                            builder.append(text+"\t");
                        }
                        builder.append("\r\n");
                    }
                }
            }
        }

        //将提取的表格内容写入txt文档
        filewriter filewriter = new filewriter("e:\\文档\\金融数据抽取\\公告\\600519_贵州茅台_贵州茅台2019年年度报告.txt");
        filewriter.write(builder.tostring());
        filewriter.flush();
        filewriter.close();
    }
}

表格内容

读取结果

2、方法2:tabula

2.1 maven仓库下载导入

在pom.xml中配置maven路径,指定依赖,如下:

<dependency>
    <groupid>technology.tabula</groupid>
    <artifactid>tabula</artifactid>
    <version>1.0.3</version>
        <exclusions>
            <exclusion>
                <artifactid>slf4j-simple</artifactid>
                <groupid>org.slf4j</groupid>
            </exclusion>
        </exclusions>
</dependency>

2.2 读取pdf中的表格

代码

package com;

import com.alibaba.fastjson.jsonarray;
import org.apache.commons.cli.commandline;
import org.apache.commons.cli.commandlineparser;
import org.apache.commons.cli.defaultparser;
import technology.tabula.commandlineapp;

import java.text.parseexception;
import java.util.arraylist;
import java.util.list;

public class testpdftabula {
    public static void main(string[] args) throws exception {
        //-f导出格式,默认csv  (一定要大写)
        //-p 指导出哪页,all是所有
        //path d:\\1xx.pdf
        //-l 强制使用点阵模式提取pdf (关键在于这儿)
        string[] argsa = new string[]{"-f=json","-p=4", "e:\\文档\\金融数据抽取\\公告\\600519_贵州茅台_贵州茅台2019年年度报告.pdf","-l"};
        //commandlineapp.main(argsa);
        commandlineparser parser = new defaultparser();
        commandline cmd = parser.parse(commandlineapp.buildoptions(), argsa);
        stringbuilder stringbuilder = new stringbuilder();
        new commandlineapp(stringbuilder, cmd).extracttables(cmd);
        system.out.println("打印返回数据:  " + stringbuilder.tostring());
    }

}

表格内容

抽取结果

[
    {
        "extraction_method": "lattice",
        "top": 149.34004,
        "left": 58.485725,
        "width": 441.1282958984375,
        "height": 98.79637145996094,
        "right": 499.614,
        "bottom": 248.13641,
        "data": [
            [
                {
                    "top": 149.34004,
                    "left": 58.485725,
                    "width": 441.1282958984375,
                    "height": 14.144073486328125,
                    "text": "常用词语释义"
                },
                {
                    "top": 0.0,
                    "left": 0.0,
                    "width": 0.0,
                    "height": 0.0,
                    "text": ""
                },
                {
                    "top": 0.0,
                    "left": 0.0,
                    "width": 0.0,
                    "height": 0.0,
                    "text": ""
                }
            ],
            [
                {
                    "top": 163.48412,
                    "left": 58.485725,
                    "width": 106.0539779663086,
                    "height": 14.136001586914063,
                    "text": "证监会"
                },
                {
                    "top": 163.48412,
                    "left": 164.5397,
                    "width": 91.28379821777344,
                    "height": 14.136001586914063,
                    "text": "指"
                },
                {
                    "top": 163.48412,
                    "left": 255.8235,
                    "width": 243.79051208496095,
                    "height": 14.136001586914063,
                    "text": "中国证券监督管理委员会"
                }
            ],
            [
                {
                    "top": 177.62011999999999,
                    "left": 58.485725,
                    "width": 106.0539779663086,
                    "height": 14.0400390625,
                    "text": "上交所"
                },
                {
                    "top": 177.62011999999999,
                    "left": 164.5397,
                    "width": 91.28379821777344,
                    "height": 14.0400390625,
                    "text": "指"
                },
                {
                    "top": 177.62011999999999,
                    "left": 255.8235,
                    "width": 243.79051208496095,
                    "height": 14.0400390625,
                    "text": "上海证券交易所"
                }
            ],
            [
                {
                    "top": 191.66016,
                    "left": 58.485725,
                    "width": 106.0539779663086,
                    "height": 14.15972900390625,
                    "text": "本公司、公司"
                },
                {
                    "top": 191.66016,
                    "left": 164.5397,
                    "width": 91.28379821777344,
                    "height": 14.15972900390625,
                    "text": "指"
                },
                {
                    "top": 191.66016,
                    "left": 255.8235,
                    "width": 243.79051208496095,
                    "height": 14.15972900390625,
                    "text": "贵州茅台酒股份有限公司"
                }
            ],
            [
                {
                    "top": 205.81989,
                    "left": 58.485725,
                    "width": 106.0539779663086,
                    "height": 14.160186767578125,
                    "text": "控股股东"
                },
                {
                    "top": 205.81989,
                    "left": 164.5397,
                    "width": 91.28379821777344,
                    "height": 14.160186767578125,
                    "text": "指"
                },
                {
                    "top": 205.81989,
                    "left": 255.8235,
                    "width": 243.79051208496095,
                    "height": 14.160186767578125,
                    "text": "中国贵州茅台酒厂(集团)有限责任公司"
                }
            ],
            [
                {
                    "top": 219.98007,
                    "left": 58.485725,
                    "width": 106.0539779663086,
                    "height": 14.039901733398438,
                    "text": "报告期"
                },
                {
                    "top": 219.98007,
                    "left": 164.5397,
                    "width": 91.28379821777344,
                    "height": 14.039901733398438,
                    "text": "指"
                },
                {
                    "top": 219.98007,
                    "left": 255.8235,
                    "width": 243.79051208496095,
                    "height": 14.039901733398438,
                    "text": "2019 年度"
                }
            ],
            [
                {
                    "top": 234.01997,
                    "left": 58.485725,
                    "width": 106.0539779663086,
                    "height": 14.116439819335938,
                    "text": "本报告"
                },
                {
                    "top": 234.01997,
                    "left": 164.5397,
                    "width": 91.28379821777344,
                    "height": 14.116439819335938,
                    "text": "指"
                },
                {
                    "top": 234.01997,
                    "left": 255.8235,
                    "width": 243.79051208496095,
                    "height": 14.116439819335938,
                    "text": "2019 年年度报告"
                }
            ]
        ]
    },
    {
        "extraction_method": "lattice",
        "top": 325.7725,
        "left": 62.325226,
        "width": 444.7215576171875,
        "height": 60.634979248046878,
        "right": 507.04678,
        "bottom": 386.40747,
        "data": [
            [
                {
                    "top": 325.7725,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.197784423828125,
                    "text": "公司的中文名称"
                },
                {
                    "top": 325.7725,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.197784423828125,
                    "text": "贵州茅台酒股份有限公司"
                }
            ],
            [
                {
                    "top": 340.97028,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.119720458984375,
                    "text": "公司的中文简称"
                },
                {
                    "top": 340.97028,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.119720458984375,
                    "text": "贵州茅台"
                }
            ],
            [
                {
                    "top": 356.09,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.119781494140625,
                    "text": "公司的外文名称"
                },
                {
                    "top": 356.09,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.119781494140625,
                    "text": "kweichow moutai co.,ltd."
                }
            ],
            [
                {
                    "top": 371.20978,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.19769287109375,
                    "text": "公司的法定代表人"
                },
                {
                    "top": 371.20978,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.19769287109375,
                    "text": "高卫东"
                }
            ]
        ]
    },
    {
        "extraction_method": "lattice",
        "top": 433.77307,
        "left": 62.325226,
        "width": 444.7215576171875,
        "height": 84.65386962890625,
        "right": 507.04678,
        "bottom": 518.42694,
        "data": [
            [
                {
                    "top": 433.77307,
                    "left": 62.325226,
                    "width": 121.29447937011719,
                    "height": 14.13677978515625,
                    "text": ""
                },
                {
                    "top": 433.77307,
                    "left": 183.6197,
                    "width": 175.25013732910157,
                    "height": 14.13677978515625,
                    "text": "董事会秘书"
                },
                {
                    "top": 433.77307,
                    "left": 358.86984,
                    "width": 148.17694091796876,
                    "height": 14.13677978515625,
                    "text": "证券事务代表"
                }
            ],
            [
                {
                    "top": 447.90985,
                    "left": 62.325226,
                    "width": 121.29447937011719,
                    "height": 14.04034423828125,
                    "text": "姓名"
                },
                {
                    "top": 447.90985,
                    "left": 183.6197,
                    "width": 175.25013732910157,
                    "height": 14.04034423828125,
                    "text": "刘刚"
                },
                {
                    "top": 447.90985,
                    "left": 358.86984,
                    "width": 148.17694091796876,
                    "height": 14.04034423828125,
                    "text": "陈华"
                }
            ],
            [
                {
                    "top": 461.9502,
                    "left": 62.325226,
                    "width": 121.29447937011719,
                    "height": 14.15997314453125,
                    "text": "联系地址"
                },
                {
                    "top": 461.9502,
                    "left": 183.6197,
                    "width": 175.25013732910157,
                    "height": 14.15997314453125,
                    "text": "贵州省仁怀市茅台镇"
                },
                {
                    "top": 461.9502,
                    "left": 358.86984,
                    "width": 148.17694091796876,
                    "height": 14.15997314453125,
                    "text": "贵州省仁怀市茅台镇"
                }
            ],
            [
                {
                    "top": 476.11017,
                    "left": 62.325226,
                    "width": 121.29447937011719,
                    "height": 14.16015625,
                    "text": "电话"
                },
                {
                    "top": 476.11017,
                    "left": 183.6197,
                    "width": 175.25013732910157,
                    "height": 14.16015625,
                    "text": "0851-22386002"
                },
                {
                    "top": 476.11017,
                    "left": 358.86984,
                    "width": 148.17694091796876,
                    "height": 14.16015625,
                    "text": "0851-22386002"
                }
            ],
            [
                {
                    "top": 490.27032,
                    "left": 62.325226,
                    "width": 121.29447937011719,
                    "height": 14.040130615234375,
                    "text": "传真"
                },
                {
                    "top": 490.27032,
                    "left": 183.6197,
                    "width": 175.25013732910157,
                    "height": 14.040130615234375,
                    "text": "0851-22386193"
                },
                {
                    "top": 490.27032,
                    "left": 358.86984,
                    "width": 148.17694091796876,
                    "height": 14.040130615234375,
                    "text": "0851-22386193"
                }
            ],
            [
                {
                    "top": 504.31046,
                    "left": 62.325226,
                    "width": 121.29447937011719,
                    "height": 14.116485595703125,
                    "text": "电子信箱"
                },
                {
                    "top": 504.31046,
                    "left": 183.6197,
                    "width": 175.25013732910157,
                    "height": 14.116485595703125,
                    "text": "mtdm@moutaichina.com"
                },
                {
                    "top": 504.31046,
                    "left": 358.86984,
                    "width": 148.17694091796876,
                    "height": 14.116485595703125,
                    "text": "mtdm@moutaichina.com"
                }
            ]
        ]
    },
    {
        "extraction_method": "lattice",
        "top": 565.7921,
        "left": 62.325226,
        "width": 444.7215576171875,
        "height": 90.90570068359375,
        "right": 507.04678,
        "bottom": 656.6978,
        "data": [
            [
                {
                    "top": 565.7921,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.1981201171875,
                    "text": "公司注册地址"
                },
                {
                    "top": 565.7921,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.1981201171875,
                    "text": "贵州省仁怀市茅台镇"
                }
            ],
            [
                {
                    "top": 580.99023,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.11981201171875,
                    "text": "公司注册地址的邮政编码"
                },
                {
                    "top": 580.99023,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.11981201171875,
                    "text": "564501"
                }
            ],
            [
                {
                    "top": 596.11005,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.1214599609375,
                    "text": "公司办公地址"
                },
                {
                    "top": 596.11005,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.1214599609375,
                    "text": "贵州省仁怀市茅台镇"
                }
            ],
            [
                {
                    "top": 611.2315,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.14837646484375,
                    "text": "公司办公地址的邮政编码"
                },
                {
                    "top": 611.2315,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.14837646484375,
                    "text": "564501"
                }
            ],
            [
                {
                    "top": 626.3799,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.2398681640625,
                    "text": "公司网址"
                },
                {
                    "top": 626.3799,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.2398681640625,
                    "text": "http://www.moutaichina.com/"
                }
            ],
            [
                {
                    "top": 641.61975,
                    "left": 62.325226,
                    "width": 193.49827575683595,
                    "height": 15.07806396484375,
                    "text": "电子信箱"
                },
                {
                    "top": 641.61975,
                    "left": 255.8235,
                    "width": 251.22328186035157,
                    "height": 15.07806396484375,
                    "text": "mtdm@moutaichina.com"
                }
            ]
        ]
    },
    {
        "extraction_method": "lattice",
        "top": 704.1823,
        "left": 62.325226,
        "width": 444.7215576171875,
        "height": 45.267578125,
        "right": 507.04678,
        "bottom": 749.4499,
        "data": [
            [
                {
                    "top": 704.1823,
                    "left": 62.325226,
                    "width": 207.12469482421876,
                    "height": 15.07757568359375,
                    "text": "公司选定的信息披露媒体名称"
                },
                {
                    "top": 704.1823,
                    "left": 269.44992,
                    "width": 237.59686279296876,
                    "height": 15.07757568359375,
                    "text": "《中国证券报》《上海证券报》"
                }
            ],
            [
                {
                    "top": 719.2599,
                    "left": 62.325226,
                    "width": 207.12469482421876,
                    "height": 15.11956787109375,
                    "text": "登载年度报告的中国证监会指定网站的网址"
                },
                {
                    "top": 719.2599,
                    "left": 269.44992,
                    "width": 237.59686279296876,
                    "height": 15.11956787109375,
                    "text": "http://www.sse.com.cn/"
                }
            ],
            [
                {
                    "top": 734.37946,
                    "left": 62.325226,
                    "width": 207.12469482421876,
                    "height": 15.0704345703125,
                    "text": "公司年度报告备置地点"
                },
                {
                    "top": 734.37946,
                    "left": 269.44992,
                    "width": 237.59686279296876,
                    "height": 15.0704345703125,
                    "text": "公司董事会办公室"
                }
            ]
        ]
    }
]

 需要注意的是:如果表格两边没有框,就解析不了!

3、终极大杀器:pdfbox

3.1 maven仓库下载导入

在pom.xml中配置maven路径,指定依赖,如下:

   <dependency>
            <groupid>org.apache.pdfbox</groupid>
            <artifactid>pdfbox</artifactid>
            <version>2.0.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/fontbox -->
        <dependency>
            <groupid>org.apache.pdfbox</groupid>
            <artifactid>fontbox</artifactid>
            <version>2.0.15</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/jempbox -->
        <dependency>
            <groupid>org.apache.pdfbox</groupid>
            <artifactid>jempbox</artifactid>
            <version>1.8.16</version>
        </dependency>

3.2 读取pdf中的表格

代码

package com;

import org.apache.pdfbox.io.randomaccessfile;
import org.apache.pdfbox.pdfparser.pdfparser;
import org.apache.pdfbox.pdmodel.pddocument;
import org.apache.pdfbox.text.pdftextstripper;

import java.io.file;
import java.io.fileinputstream;
import java.io.filewriter;
import java.io.ioexception;

public class pdfutil {
    /**
     * 用来读取pdf文件
     * @param filepath
     * @return
     * @throws ioexception
     */
    public static string readpdf(string filepath) {
        string buffer = "";
        try{
            file input = new file(filepath);
            if (input != null && input.exists()) {
                pddocument pd = pddocument.load(input);
                pdftextstripper stripper = new pdftextstripper();
                stripper.setsortbyposition(false);
                buffer = stripper.gettext(pd);
                pd.close();
            }else
                buffer =  "read failed";
        }catch (exception e){
            e.printstacktrace();
            return "read failed";
        }
        return buffer;
    }

    public static string readpdf2(string filename) {
        string result = "";
        file file = new file(filename);
        fileinputstream in = null;
        try {
            in = new fileinputstream(filename);
            // 新建一个pdf解析器对象
            pdfparser parser = new pdfparser(new randomaccessfile(file,"rw"));
            // 对pdf文件进行解析
            parser.parse();
            // 获取解析后得到的pdf文档对象
            pddocument pdfdocument = parser.getpddocument();
            // 新建一个pdf文本剥离器
            pdftextstripper stripper = new pdftextstripper();
            stripper .setsortbyposition(false); //sort:设置为true 则按照行进行读取,默认是false
            // 从pdf文档对象中剥离文本
            result = stripper.gettext(pdfdocument);
        } catch (exception e) {
            system.out.println("读取pdf文件" + file.getabsolutepath() + "生失败!" + e);
            e.printstacktrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (ioexception e1) {
                }
            }
        }
        return result;
    }

    /**
     * 测试pdf文件的创建
     * @param args
     */
    public static void main(string[] args) {
        try {
            string filename = "600519_贵州茅台_贵州茅台2019年年度报告";
            string filepath = "e:\\文档\\金融数据抽取\\公告\\"+filename+".pdf";  //这里先手动把绝对路径的文件夹给补上。
            pdfutil pdfutil = new pdfutil();
//            string result = pdfutil.readpdf(filepath);
            string result = pdfutil.readpdf2(filepath);

            system.out.println(result);
            //将提取的表格内容写入txt文档
            filewriter filewriter = new filewriter("e:\\文档\\金融数据抽取\\公告\\"+filename+".txt");
            filewriter.write(result);
            filewriter.flush();
            filewriter.close();
        } catch (ioexception e) {
            e.printstacktrace();
        }

    }
}

表格内容

抽取结果

4、总结

几种方式各有利弊,自己权衡。

以上就是java代码实现轻松读取pdf中表格的完整指南的详细内容,更多关于java读取pdf表格的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com