PDFBOX和ASPOSE.PDF

一、aspose.pdf

1、按段落分段

java 复制代码
/**
 * docx文本按段分段
 */
public static void main(String[] args) {
    int i = 1;
    try {
        // 打开文件流
        FileInputStream file = new FileInputStream("I:\\范文.docx");
        // 创建 Word 文档对象
        XWPFDocument doc = new XWPFDocument(file);
        // 获取所有段落
        for (XWPFParagraph para : doc.getParagraphs()) {
            // 输出段落文本
            if (!para.getText().equals("")) {
                System.out.println(i++ + ":" + para.getText());
            }
        }
        // 关闭文件流
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2、按句子分段

其他

word转pdf工具之aspose

java 复制代码
<dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-words-crack</artifactId>
        <version>21.1</version>
    </dependency>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-slides</artifactId>
        <version>18.7</version>
    </dependency>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cell</artifactId>
        <version>18.9</version>
    </dependency>

操作pdf其它api依赖

java 复制代码
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext7-core</artifactId>
    <version>7.1.15</version>
</dependency>
java 复制代码
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>1.8.10</version>
</dependency>

二、PDFBOX

1、提取pdf指定页码的数据

java 复制代码
public static void main(String[] args) throws IOException {
    String pdfPath = "C:\\Users\\Fushihao\\Desktop\\pdf\\test.pdf";
    int pageIndex = 4; // 指定要提取文本的页面号码

    // 加载 PDF 文件
    PDDocument document = PDDocument.load(new File(pdfPath));

    // 提取指定页面的文本
    PDFTextStripper stripper = new PDFTextStripper();
    stripper.setStartPage(pageIndex + 1);
    stripper.setEndPage(pageIndex + 1);
    String pageText = stripper.getText(document);

    // 输出文本到控制台
    System.out.println(pageText);

    // 关闭 PDF 文件
    document.close();
}
pom 复制代码
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>1.8.10</version>
</dependency>

2、获取 PDF 字数

java 复制代码
public static int getPdfWords(File file) throws IOException {
    // 加载文件
    PDDocument document = PDDocument.load(file);
    PDFTextStripper stripper = new PDFTextStripper();
    // 获取字数
    String text = stripper.getText(document);
    // 关闭
    document.close();
    // 去除特殊符号返回字数
    return text.replaceAll("[\\r\\n\\t ]+", "").length();
}
相关推荐
敲代码的小吉米8 小时前
前端上传el-upload、原生input本地文件pdf格式(纯前端预览本地文件不走后端接口)
前端·javascript·pdf·状态模式
东方巴黎~Sunsiny11 小时前
EasyExcel导出excel再转PDF转图片详解
pdf·excel
aklry15 小时前
uniapp实现在线pdf预览以及下载
前端·pdf·uni-app
繁依Fanyi15 小时前
我的 PDF 工具箱:CodeBuddy 打造 PDFMagician 的全过程记录
java·pdf·uni-app·生活·harmonyos·codebuddy首席试玩官
DevOpenClub21 小时前
PPT 转高精度 PDF API 接口
pdf·powerpoint
想太多会累i1 天前
Spring Boot 使用Itext绘制并导出PDF
spring boot·pdf
zixingcai1 天前
Adobe Acrobat pro在一份PDF中插入空白页
pdf·adobe acrobat reader
課代表1 天前
AcroForm JavaScript Promise 对象应用示例: 异步加载PDF文件
开发语言·javascript·pdf·promise·对象
charlie1145141911 天前
基于Qt6 + MuPDF在 Arm IMX6ULL运行的PDF浏览器——MuPDF Adapter文档
arm开发·qt·学习·pdf·教程·设计·qt6
东风西巷2 天前
MobiPDF:安卓设备上的专业PDF阅读与编辑工具
android·智能手机·pdf·软件需求