java读取pdf文本转换html

java读取pdf文本转换html

完整代码地址 也就两个文件

 java读取pdf中的纯文字,这里使用的是pdfbox工具包

maven引入如下配置

     <dependency>
            <groupId>net.sf.cssbox</groupId>
            <artifactId>pdf2dom</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox-tools</artifactId>
            <version>2.0.12</version>
        </dependency>

工具类直接读取

代码示例

  /*
    读取pdf文字
     */
    @Test
    public void readPdfTextTest() throws IOException {
        byte[] bytes = getBytes("D:\\code\\pdf\\HashMap.pdf");
        //加载PDF文档
        PDDocument document = PDDocument.load(bytes);
        readText(document);
    }

    public void readText(PDDocument document) throws IOException {
        PDFTextStripper stripper = new PDFTextStripper();
        String text = stripper.getText(document);
        System.out.println(text);
    }

将pdf转换为html

效果图

 代码示例

/*
    pdf转换html
     */
    @Test
    public void pdfToHtmlTest()  {
        String outputPath = "D:\\code\\pdf\\HashMap.html";
        byte[] bytes = getBytes("D:\\code\\pdf\\HashMap.pdf");
//        try() 写在()里面会自动关闭流
        try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputPath)),"UTF-8"));){
            //加载PDF文档
            PDDocument document = PDDocument.load(bytes);
            PDFDomTree pdfDomTree = new PDFDomTree();
            pdfDomTree.writeText(document,out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /*
    将文件转换为byte数组
     */
    private byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }

完整的一个上传pdf转换为HTML功能(今后转换pdf也不需要找什么第三方了,哈哈)

@RequestMapping("ud")
@Controller
public class UpAndDownController {
    @RequestMapping("upload.do")
    @ResponseBody
    public Map<String,Object> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("code","200");
        try {
            PdfConvertUtil pdfConvertUtil = new PdfConvertUtil();
            String pdfName = file.getOriginalFilename();
            int lastIndex = pdfName.lastIndexOf(".pdf");
            String fileName = pdfName.substring(0, lastIndex);
            String htmlName = fileName + ".html";
            String realPath = ResourceUtils.getURL("classpath:").getPath() + "/templates/file";
            File f = new File(realPath);
            if(!f.exists()){
                f.mkdirs();
            }
            String htmlPath = realPath + "\\" + htmlName;
            pdfConvertUtil.pdftohtml(file.getBytes(), htmlPath);
        } catch (Exception e) {
            map.put("code","500");
            e.printStackTrace();
        }
        return map;
    }

}

可以使用postman调试

需要设置请求头 Content-Type 指定为 application/x-www-form-urlencoded

之后选择body选择form-data,OK

如果涉及到HTML页面直接加载PDF,无需插件

可以参考下 

pdf.js实现在HTML下直接浏览pdf文档,无需插件即可实现 - 河畔一角 - 博客园

GitHub - mozilla/pdf.js: PDF Reader in JavaScript

posted @ 2019-05-12 17:54 陈灬大灬海 阅读(...) 评论(...) 编辑 收藏
对于将PDF和JPG文件转换文本的通用程序,你可以结合使用Apache PDFBox和Tesseract OCR库。Apache PDFBox用于处理PDF文件,而Tesseract OCR用于将图像中的文本转换为可识别的文本。 首先,确保已将以下依赖项添加到你的项目中: 1. Apache PDFBox:用于处理PDF文件 2. Tesseract OCR:用于识别图像中的文本 然后,你可以使用以下代码来读取PDF和JPG文件,并将其转换文本: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class FileConverter { public static void main(String[] args) { String pdfFilePath = "path/to/pdf/file.pdf"; String jpgFilePath = "path/to/jpg/file.jpg"; // 读取PDF文件并转换文本 try { PDDocument document = PDDocument.load(new File(pdfFilePath)); PDFTextStripper stripper = new PDFTextStripper(); String pdfText = stripper.getText(document); System.out.println("PDF文本内容:"); System.out.println(pdfText); document.close(); } catch (IOException e) { e.printStackTrace(); } // 读取JPG文件并转换文本 try { BufferedImage image = ImageIO.read(new File(jpgFilePath)); Tesseract tesseract = new Tesseract(); String jpgText = tesseract.doOCR(image); System.out.println("JPG文本内容:"); System.out.println(jpgText); } catch (IOException | TesseractException e) { e.printStackTrace(); } } } ``` 请确保已将PDF文件和Tesseract OCR库的训练数据文件(.traineddata)放置在适当的位置,并在代码中指定正确的文件路径。 这个程序将分别读取PDF和JPG文件,并将它们转换文本输出。你可以根据需要进行进一步的文本处理或保存到文件中。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈灬大灬海

万水千山总是情

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值