使用aspose将docx(支持多种文档类型)转化为pdf,以及linux上转化后中文乱码问题

点击下载对应jar包
使用aspose将docx(支持多种文档类型)转化为pdf,以下为docx流转化为pdf文件或pdf流(不产生中间文件)

/**
     * 验证License 若不验证则转化出的pdf文档会有水印产生
     * @return
     */
    private static boolean getLicense() {
        boolean result = false;
        InputStream is = null;
        try {
            is = WordUtils.class.getClassLoader().getResourceAsStream("aspose/license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * @param inputStream 需要被转换的word的流
     * @param pdfPath 转换之后pdf的全路径带文件名
     */
    public static boolean docxToPdf(InputStream inputStream, String pdfPath) {
        FileOutputStream os = null;
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        if (!getLicense()) {
            return false;
        }
        try {
            File outputFile = new File(pdfPath);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
             }
            os = new FileOutputStream(outputFile);
            Document docx = new Document(inputStream);
            // 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB,XPS, SWF 相互转换
            docx.save(os, SaveFormat.PDF);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * @param inputStream 需要被转换的word的流
     * @return pdf流
     */
    public static InputStream docxToPdf(InputStream inputStream) {
        ByteArrayOutputStream os = null;
        ByteArrayInputStream is = null;
        // 验证License 若不验证则转化出的pdf文档会有水印产生
//        if (!getLicense()) {
//            throw new ResultException(500, "docx转pdf验证失败");
//        }
        //设置中文字体所在目录
        OsInfo osInfo = SystemUtil.getOsInfo();
        if(osInfo.isLinux()){
            FontSettings fontSettings = new FontSettings();
            fontSettings.setFontsFolder("/usr/share/fonts/chinese", true);
        }
        try {
            os = new ByteArrayOutputStream();
            Document docx = new Document(inputStream);
            // 支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB,XPS, SWF 相互转换
            docx.save(os, SaveFormat.PDF);
            is = new ByteArrayInputStream(os.toByteArray());
            return is;
        } catch (Exception e) {
            throw new ResultException(500, "合同生成失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

license.xml用来解决水印问题;linux上可能会中文乱码,因为linux没有对应的中文文件,上面代码设置了linux上中文文件所在的路径,只需将window上的中文文件拷贝到linux上即可。window上路径为C:\Windows\Fonts
在这里插入图片描述

参考1:https://blog.csdn.net/River_Frozen/article/details/105774825?spm=1001.2014.3001.5502
参考2:https://blog.csdn.net/Number_oneEngineer/article/details/122124096

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在使用 Aspose 将 Word 转换为 PDF 时设置字体为 Linux 字体,您可以按照以下步骤进行操作: 1. 确保您已经安装了所需的 Linux 字体。可以从适合您需求的字体提供商或在线字体库获取所需字体文件。 2. 在代码中指定要使用的字体名称。例如,如果您希望使用 Linux 下的 Arial 字体,可以将字体名称设置为 "Arial"。 3. 使用 Aspose 提供的 API 来设置所需字体。以下是一个示例代码片段,演示如何在 Aspose.Words 中将字体设置为 Linux 字体并将 Word 文档转换为 PDF: ```java // 加载 Word 文档 Document doc = new Document("input.docx"); // 创建 FontSettings 对象 FontSettings fontSettings = new FontSettings(); fontSettings.setFontsFolder("/path/to/linux/fonts/folder", true); // 设置 Linux 字体文件夹路径 fontSettings.DefaultFontName = "Arial"; // 设置要使用的字体名称 // 将 FontSettings 对象分配给文档 doc.FontSettings = fontSettings; // 保存为 PDF doc.Save("output.pdf", SaveFormat.Pdf); ``` 在上述示例中,通过创建一个 `FontSettings` 对象并使用 `setFontsFolder` 方法指定 Linux 字体文件夹的路径,然后将该对象分配给文档的 `FontSettings` 属性。同时,将 `DefaultFontName` 属性设置为所需的字体名称。 请注意,具体的 API 调用方式可能因您使用Aspose 产品和版本而有所不同。因此,请参考相关的 Aspose 文档或示例代码以获取更多详细信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值