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();
}
相关推荐
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
reembarkation2 天前
使用pdfjs-dist 预览pdf,并添加文本层的实现
前端·javascript·pdf
reembarkation2 天前
vue-pdf 实现blob数据的预览
javascript·vue.js·pdf
Light602 天前
领码方案|Linux 下 PLT → PDF 转换服务超级完整版:异步、权限、进度(一气呵成)
linux·spring boot·pdf·gpcl6/ghostpcl·s3/oss·权限与审计·异步与进度
伟贤AI之路2 天前
【分享】中小学教材课本 PDF 资源获取指南
人工智能·pdf
东风西巷2 天前
PDFgear:免费全能的PDF处理工具
前端·pdf·软件需求
Sunny_yiyi3 天前
Java根据模版导出PDF文件
java·开发语言·pdf
小*-^-*九4 天前
php 使用html 生成pdf word wkhtmltopdf 系列2
pdf·html·php
千册4 天前
pyside6 的pdf显示测试 -- 01
开发语言·python·pdf
qq_172805594 天前
Go 语言 PDF 生成库综合比较与实践指南
开发语言·golang·pdf