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();
}
相关推荐
开开心心就好7 小时前
批量PDF转换工具,一键转换Word Excel
开发语言·前端·学习·pdf·电脑·word·excel
老家的回忆20 小时前
jsPDF和html2canvas生成pdf,组件用的elementplus,亲测30多页,20s实现
前端·vue.js·pdf·html2canvas·jspdf
Vertira20 小时前
pdf 合并 python实现(已解决)
前端·python·pdf
十一0829931 天前
【PDF-XSS攻击】springboot项目-上传文件-解决PDF文件XSS攻击
spring boot·pdf·xss
qq_393828221 天前
PDF的图片文字识别工具
windows·pdf·电脑·软件需求·图片处理
wsxqaz1 天前
浏览器原生控件上传PDF导致hash值不同
算法·pdf·哈希算法
工业3D_大熊11 天前
3D模式格式转换工具HOOPS Exchange如何将3D PDF转换为STEP格式?
3d·pdf·3d格式转换·3d模型格式转换·cad格式转换·cad数据格式转换·3d模型可视化
IDRSolutions_CN11 天前
在 Java 中生成 PDF 缩略图(教程)
java·经验分享·pdf·软件工程·团队开发
IDRSolutions_CN11 天前
用Java将PDF转换成GIF
java·经验分享·pdf·软件工程·团队开发
贤和兄11 天前
使用docx4j 实现word转pdf(linux乱码处理)
linux·pdf·word