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();
}
相关推荐
放飞自我的Coder16 小时前
【PDF拆分 Python拆分左右并排PDF】
python·pdf
开开心心_Every16 小时前
发票批量打印工具支持双面预览页面方向设置
游戏·微信·pdf·华为云·excel·语音识别·googlecloud
AI刀刀16 小时前
千问 文心 元宝 Kimi公式乱码
ai·pdf·豆包·deepseek·ds随心转
J_liaty21 小时前
Java实现PDF添加水印的完整方案(支持灵活配置、平铺、多页策略)
java·开发语言·pdf
德育处主任Pro21 小时前
『NAS』告别付费和广告,在群晖部署PDF工具箱-bentopdf
pdf·nas
索荣荣1 天前
Spring Boot 实现DOCX转PDF(基于docx4j的轻量级开源方案)
spring boot·后端·pdf
裴嘉靖1 天前
uni-app 打包后 PDF 无法生成问题完整解决方案
pdf·uni-app
wujian83111 天前
AI导出pdf方法
人工智能·pdf
小郎君。1 天前
PDF-知识图谱全流程前后端实现【工具已实现,搭建前后端pipline】
pdf·状态模式·知识图谱
wujian83111 天前
ChatGPT和Gemini导出pdf方法
人工智能·ai·chatgpt·pdf·deepseek