图片型pdf转文本文档

基本思路

直接用工具将扫描型pdf转文本是不行的,因为扫描型的pdf是图片。先读取整个pdf文件按页生成图片,在调用OCR识别读取文字即可。

pdf第三方库pdfbox

依赖:

        <!--pdfbox pdf解析-->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.1</version>
        </dependency>

使用该库可对pdf文件进行基本读写操作

File file = new File(PdfFilePath);
//加载pdf,可对其进行基本读写
PDDocument pdDocument = PDDocument.load(file);
int pages =pdDocument.getNumberOfPages();// 获取PDF页数
//pdf renderer 渲染器,可转成图片读取
PDFRenderer renderer = new PDFRenderer(pdDocument);
BufferedImage image = renderer.renderImageWithDPI(i, dpi);//i为页数,dpi为精度,默认96
从pdf中读取文本
    /**
     * 指定pdf与目标文件路径,从pdf文件读取文本到指定文件
     * @param pdfPath pdf文件路径,绝对路径
     * @param targetPath 目标文件,绝对路径
     */
    public static void readTextFromPdf(String pdfPath,String targetPath)  {
   
        //pdf文件校验
        File pdfFile=new File(pdfPath);
        if(!pdfFile.exists()){
   
            System.out.println("pdf文件未发现:"+pdfPath);
            return;
        }

        File targetFile=new File(targetPath);

        PDDocument pdDocument=null;

        try{
   
            //读取文档
            pdDocument=PDDocument.load(pdfFile);
            //获取文档页码
            int pages=pdDocument.getNumberOfPages();
            //读取文档内容并设置读取参数
            PDFTextStripper stripper=new PDFTextStripper();
            stripper.setSortByPosition(true);
            stripper.setStartPage(1);
            stripper.setEndPage(pages);
            String content=stripper.getText(pdDocument);

            //校验目标文件
            if(targetFile.exists()){
   
               System.out.println("文件已存在,将覆盖文件");
            }else {
   
                System.out.println("目标文件不存在,新建文件");
                targetFile.createNewFile();
            }

            //写入目标文件
            FileWriter fileWriter=new FileWriter(targetFile);
            fileWriter.write(content);

        }catch (Exception e){
   
            e.printStackTrace();
        }

        System.out.println("写入文件成功");
    }
将pdf转换为图片
    /**
     * PDF文件转PNG/JPEG图片
     * @param PdfFilePath 完整路径
     * @param dstImgFolder 图片存放的文件夹
     * @param dpi dpi越大转换后越清晰,相对转换速度越慢,一般电脑默认96dpi
     */
    public static void pdf2Image(String PdfFilePath,
                                     String dstImgFolder,
                                     int dpi) {
   
        File file = new File(PdfFilePath);
        PDDocument pdDocument;
        try {
   
            //获取pdf文件名称与上层路径
            String imgPDFPath = file.getParent();
            int dot = file.getName().lastIndexOf('.');
            // 获取图片文件名
            String imagePDFName = file.getName().substring(0
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值