word、pdf、txt等各种模式相互转换

本文介绍如何使用Maven安装Aspose.Words库,并通过示例代码展示如何利用Aspose.Words处理Word文档,包括多线程操作、正则表达式匹配文档路径和转换文档为图片。

1、maven jar文件的产生

mvn install:install-file -Dfile=C:\aspose-words-18.5.jar -DgroupId=com.steven -DartifactId=aspose-words -Dversion=1.0.0 -Dpackaging=jar

pom.xml

        <dependency>
            <groupId>com.steven</groupId>
            <artifactId>aspose-words</artifactId>
            <version>1.0.0</version>
        </dependency>

2、license-18.xml放在resources下

3、参考代码

package com.example.demo;

import com.aspose.words.*;
import lombok.SneakyThrows;

import java.io.File;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

public class HoughWords {
    public static void main(String[] args) {

    }

    /**
     * 多线程处理 executor 可不传入,则默认最多20个线程
     *
     * @Title: handle
     * @param: [operate]
     * @return: java.util.concurrent.CompletableFuture<java.lang.Void>
     * @throws:
     */
    private CompletableFuture<Void> handle(File operate, Executor executor) {

        //捕捉异常,不会导致整个流程中断
        return CompletableFuture.runAsync(() -> {
            long old = System.currentTimeMillis();
            String wsmc = operate.toString();
            Pattern pattern = compile("\\.*\\d{18}\\.*");
            Matcher matcher = pattern.matcher(wsmc);
            String ahdm = "";
            if (matcher.find()) {
                ahdm = matcher.group();
            } else {
                //15位案号代码
                Pattern pattern2 = compile("\\.*\\d{15}\\.*");
                Matcher matcher2 = pattern2.matcher(wsmc);
                if (matcher2.find()) {
                    ahdm = matcher2.group();
                }
            }
            createDirectory("E:\\ysjys\\jpg\\" + ahdm + "\\");
            doc2pic(wsmc, "E:\\ysjys\\jpg\\" + ahdm + "\\");
            long now = System.currentTimeMillis();
            System.out.println("线程[" + Thread.currentThread().getName() + "] 共耗时:" + ((now - old) / 1000.0) + "秒");
        }, executor).exceptionally(throwable -> {
            System.out.println("线程[{" + Thread.currentThread().getName() + "}]发生了异常, 继续执行其他线程,错误详情[{" + throwable.getMessage() + "}]");
            return null;
        });
    }

    public static void createDirectory(String... args) {
        if (args != null && args.length > 0) {
            for (String pathName : args) {
                File file = new File(pathName);
                if (!file.exists()) {
                    file.mkdirs();
                }
            }
        }
    }

    /**
     * 通行证
     *
     * @Title: getLicense
     * @param: []
     * @return: boolean
     * @throws:
     */
    public static boolean getLicense() {
        boolean result = false;
        try {
            InputStream is = ConvertUtil.class.getResourceAsStream("/license-18.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @SneakyThrows
    public static void doc2pic(String inPath, String outPath) {
        if (!getLicense()) {
            return;
        }
        Document doc = new Document(inPath);
        ImageSaveOptions iso = new ImageSaveOptions(SaveFormat.JPEG);
        iso.setResolution(128f);
        iso.setPrettyFormat(true);
        iso.setUseAntiAliasing(true);
        for (int i = 0; i < doc.getPageCount(); i++) {
            iso.setPageIndex(i);
            doc.save(outPath + i + ".jpg", iso);
        }
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值