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();
}
相关推荐
有过~5 小时前
Total PDF Converter v6.5.0.356.0 电脑PDF多功能转换器
pdf
E_ICEBLUE13 小时前
Python 教程:如何快速在 PDF 中添加水印(文字、图片)
开发语言·python·pdf
缺点内向15 小时前
Java: 为PDF批量添加图片水印实用指南
java·开发语言·pdf
造价女工19 小时前
PDF转图片:轻松实现工程图纸的高效共享与高清展示
pdf·格式工厂
涔溪1 天前
vue中预览pdf文件
前端·vue.js·pdf
lyx_20161 天前
PDF文档导出分页功能实现
react.js·typescript·pdf·react
puyaCheer1 天前
Android 打开 在线 pdf 文件
android·pdf
开开心心_Every1 天前
专业视频修复软件,简单操作效果好
学习·elasticsearch·pdf·excel·音视频·memcache·1024程序员节
2501_907136822 天前
发票识别工具,支持xml、pdf、ofd文件
xml·pdf·软件需求
DreamNotOver2 天前
批量转换论文正文引用为上标后转PDF保持上标
pdf·word·引用·上标