PDF转文本以及转图片:itextpdf

文章目录

🐒个人主页:信计2102罗铠威

🏅JavaEE系列专栏

📖前言:

PDF转文本的插件常用的有:pdfbox ,itextpdf 和 spire.pdf 这几个,

🎀 1. itextpdf

1.1导入itextpdf的maven依赖
xml 复制代码
        <!--2.itexpdf依赖-->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.3</version>
        </dependency>
1.2 提取文本代码
java 复制代码
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.io.*;

public class Main_itextPdf {

    public static void main(String[] args) throws Exception {
        System.out.println("------------------------pdf提取文本开始------------------------------");
            // 2. 加载PDF文件
        File file = new File("C:/Users/Administrator/Desktop/罗铠威个人简历.pdf");
            PdfReader reader = new PdfReader(file.getAbsolutePath());

            // 3. 解析PDF文件,获取页面数据
            int page = 1; // 获取第一页
            String text = PdfTextExtractor.getTextFromPage(reader, page);
            System.out.println(text);
            // 4. 关闭PdfReader
            reader.close();
        System.out.println("------------------------pdf提取文本结束------------------------------");
        }
}
1.3 pdf转换成图片代码(本地图片地址还是线上PDF的URL地址均支持)
java 复制代码
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;

public class Main_itextPdf {

    public static void main(String[] args) throws Exception {
        System.out.println("------------------------pdf转图片开始------------------------------");
//        InputStream inputStream = readPdfFromUrl("http://minio.xxxx/xxxx.pdf");//线上的pdf文件
        InputStream inputStream = new FileInputStream("C:/Users/Administrator/Desktop/罗铠威个人简历.pdf") ;
        byte[] bytes = streamToByte(inputStream);
        InputStream newStream = new ByteArrayInputStream(bytes);
        //将pdf流转换成png图片流
        InputStream imgStream = pdfToImg(newStream);
        //存储图片imgStream到桌面
        ImageIO.write(ImageIO.read(imgStream), "png", new File("C:/Users/Administrator/Desktop/1.png"));
        System.out.println("------------------------pdf转图片结束------------------------------");
    }

    //支持线上pdf文件地址url
    public static InputStream readPdfFromUrl(String pdfUrl) throws IOException {
        URL url = new URL(pdfUrl);
        URLConnection connection = url.openConnection();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
        return bufferedInputStream;
    }

    public static ByteArrayInputStream pdfToImg(InputStream pdfStream) throws Exception {
        // 将 InputStream 转换为 PDDocument
        PDDocument document = PDDocument.load(pdfStream);

        // 创建 PDFRenderer 对象
        PDFRenderer pdfRenderer = new PDFRenderer(document);

        // 选择第一页面来生成图片
        // 可以根据需要改成遍历所有页面并保存
        BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300); // 0 表示第一页,300 DPI 提供高质量图像

        // 将 BufferedImage 转换为 InputStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "PNG", byteArrayOutputStream);
        document.close();
        // 返回一个新的 InputStream
        return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
    }
    public static byte[] streamToByte(InputStream inputStream) throws Exception {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[1024];
        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        return buffer.toByteArray();
    }

}
相关推荐
AI导出鸭9 小时前
Claude的LaTeX生成PDF文件复制后数学公式乱码,怎样修改?专业用户首选“AI导出鸭”
人工智能·pdf·ai导出鸭
qyyyyy5701 天前
外文 PDF 怎么整理成 Markdown?从翻译、内容提取到 RAG 知识库导入
自然语言处理·pdf·erlang·机器翻译·零知识证明
AI导出鸭2 天前
Perplexity的LaTeX生成PDF文件复制后数学公式乱码,怎样修改?AI导出鸭苹果版的技术拆解与批量之道
人工智能·pdf·ai导出鸭
SpaceAIGlobal2 天前
AI PPT生成工具哪些支持PDF文档导入?
人工智能·ai·pdf·powerpoint·办公
Xxtaoaooo2 天前
极空间部署Stirling PDF:搭建自托管PDF工具箱并实现远程访问
pdf·cpolar·stirling pdf
SamChan902 天前
对比 4 种主流 PDF 文档解析方案:PyMuPDF vs pdfplumber vs Apache PDFBox vs 大模型 OCR
python·ai·pdf·ocr·apache·机器翻译
AI刀刀3 天前
Kimi 文档导出格式错乱、排版丢失、导出报错?AI 导出鸭一键智能适配,稳定输出规范 Word、PDF,高效解决各类导出难题
人工智能·pdf·word·ai导出鸭
Ai-_Man3 天前
豆包智能体把对话批量导出为Word或PDF的正确顺序
人工智能·ai·小程序·pdf·word
SamChan903 天前
FastAPI+Celery+Redis搭建企业级PDF翻译异步任务系统
redis·后端·python·pdf·wpf·fastapi
SamChan904 天前
用Playwright端到端测试PDF翻译功能:Web自动化测试实战
前端·python·ai·pdf·wpf