doc转pdf

详细请参考只说 aspose  这一种,因为这种windows ,linux都支持,linux 需要设置导入字体

不喜欢墨迹   废话不多说直接上代码了

主打方便大家学习

 工具类

public static void docToPdf2(File docFile, File pdfFile){
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return R.fail("验证规则失败");
        }
        FileOutputStream os = null;
        try {
            long old = System.currentTimeMillis();
            // 新建一个空白pdf文档
            os = new FileOutputStream(pdfFile);
            Document document= new Document(docFile.toString()); // Address是将要被转化的word文档
            TableCollection tables = document.getFirstSection().getBody().getTables();
            for (Table table : tables) {
                RowCollection rows = table.getRows();
                table.setAllowAutoFit(false);
                for (Row row : rows) {
                    CellCollection cells = row.getCells();
                    for (Cell cell : cells) {
                        CellFormat cellFormat = cell.getCellFormat();
                        cellFormat.setFitText(false);
                        cellFormat.setWrapText(true);
                    }
                }
            }
            document.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
// EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (os != null) {
                try {
                    os.flush();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    
   public static boolean getLicense() {
        InputStream is=null;
        try {
             is = FileUtils.class.getClassLoader().getResourceAsStream("license.xml");
            License license = new License();
            license.setLicense(is);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(String.format("验证规则错误信息:%s", e.getMessage()));
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

 取消水印处理

<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>

需要jar

阿里云仓库无aspose文件,需要手动下载jar包,然后本地mvn引入依赖

下载地址 提取码:7777
该依赖仅限于学习使用。。。。。
其他版本,可至官网下载。

引入jar

mvn install:install-file -DgroupId=com.aspose.words -DartifactId=aspose-words -Dversion=19.5 -Dpackaging=jar -Dfile=写入文件所在地址

linux 导入字体

 yum install -y fontconfig mkfontscale 安装字体管理器
[root@admin fonts]# mkfontscale (创建字体索引)
[root@admin fonts]# mkfontdir (创建字体目录)
[root@admin fonts]# fc-cache (刷新字体缓存)
[root@admin fonts]# fc-list (查看所有字体)

windows 字体路径 C:\Windows\Fonts 

linux 字体路径 /usr/share/fonts   (文件夹不存在手动创建)

windows 复制到 linux 下面即可

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值