PDF文件解析

1.表格类的PDF文件,可以通过tabula解析

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>

片段代码

public String getContent(byte[] bytes) {
    String str = Base64.encodeBase64String(bytes);
    String[] args = new String[]{"-f=JSON", "-p=all", str, "-l"};
    CommandLineParser parser = new DefaultParser();
    StringBuilder sb = new StringBuilder();
    try {
        CommandLine cmd = parser.parse(CommandLineApp.buildOptions(), args);
        CommandLineApp commandLineApp = new CommandLineAppExt(sb, cmd);
        commandLineApp.extractTables(cmd);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
public class CommandLineAppExt extends CommandLineApp {

    @Override
    public void extractTables(CommandLine line) throws ParseException {
        if (line.getArgs().length != 1) {
            throw new ParseException("Need exactly one filename\nTry --help for help");
        } else {
            byte[] decode = Base64.decodeBase64(line.getArgs()[0]);
            this.extractFileTables(decode);
        }
    }

    public void extractFileTables(byte[] bytes) throws ParseException {
        this.extractFile(bytes, this.defaultOutput);
    }

    private void extractFile(byte[] bytes, Appendable outFile) throws ParseException {
        PDDocument pdfDocument = null;

        try {
            pdfDocument = this.password == null ? PDDocument.load(bytes) : PDDocument.load(bytes, this.password);
            PageIterator pageIterator = this.getPageIterator(pdfDocument);
            ArrayList tables = new ArrayList();

            while (true) {
                while (pageIterator.hasNext()) {
                    Page page = pageIterator.next();
                    Rectangle area;
                    if (this.pageAreas != null) {
                        for (Iterator var7 = this.pageAreas.iterator(); var7.hasNext(); tables.addAll(this.tableExtractor.extractTables(page.getArea(area)))) {
                            Pair<Integer, Rectangle> areaPair = (Pair) var7.next();
                            area = (Rectangle) areaPair.getRight();
                            if ((Integer) areaPair.getLeft() == 0) {
                                area = new Rectangle((float) ((double) (area.getTop() / 100.0F) * page.getHeight()), (float) ((double) (area.getLeft() / 100.0F) * page.getWidth()), (float) (area.getWidth() / 100.0D * page.getWidth()), (float) (area.getHeight() / 100.0D * page.getHeight()));
                            }
                        }
                    } else {
                        tables.addAll(this.tableExtractor.extractTables(page));
                    }
                }

                this.writeTables(tables, outFile);
                return;
            }
        } catch (IOException var17) {
            throw new ParseException(var17.getMessage());
        } finally {
            try {
                if (pdfDocument != null) {
                    pdfDocument.close();
                }
            } catch (IOException var16) {
                System.out.println("Error in closing pdf document" + var16);
            }

        }
    }

}

说明:CommandLineAppExt类继承CommandLineApp类,为满足传入byte[]类型参数,做了改造,CommandLineApp类本身不支持传入byte[]类型数据,支持传入File文件类型。

2.pdf文件不是表格,而是平铺的文档,用spire解析

maven 依赖

<dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.pdf.free</artifactId>
            <version>3.9.0</version>
</dependency>

<dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.4</version>
</dependency>

片段代码

public String getContent(byte[] bytes) {
        PdfDocument doc = new PdfDocument();
        doc.loadFromBytes(bytes);
        StringBuffer sb = new StringBuffer();
        PdfPageBase page;
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            //获取每一页的page对象
            page = doc.getPages().get(i);
            sb.append(page.extractText(true));
        }
        doc.close();
        return sb.toString();
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在C语言中实现PDF文件解析的过程可以包括以下几个步骤: 1. 首先,需要定义一些辅助函数,比如用于判断对象类型的函数。其中,引用给出的示例代码是一个判断是否为图像对象的函数,而引用给出的示例代码是一个判断是否为字体描述符对象的函数。 2. 接下来,需要定义保存字体的函数。其中,引用给出的示例代码是一个保存字体的函数,其中包括了一些用于处理字体数据的变量和操作,比如定义一个缓冲区用于保存字体数据。 3. 在解析PDF文件之前,需要打开文件并读取其内容。可以使用C语言中的文件操作函数来实现这一步骤。 4. 接下来,需要解析PDF文件的结构。这包括解析PDF的头部信息、交叉引用表和对象流等。可以使用C语言中的字符串处理函数和正则表达式来实现这一步骤。 5. 解析文件结构后,需要提取所需的数据,比如文本、图像和字体等。可以使用前面定义的辅助函数来判断对象类型,并使用相应的函数来提取数据。 6. 最后,需要进行清理工作,比如释放内存和关闭文件等。 需要注意的是,以上只是一个大致的框架,具体的实现可能会因为不同的PDF文件格式解析需求而有所不同。在实际的开发中,还需要考虑异常处理、内存管理和性能优化等方面的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [c++ mupdf 提取pdf文件里面图片](https://blog.csdn.net/u011269801/article/details/123951280)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值