通过OpenOffice实现Office文档转换为PDF格式的文档

声明

郑重声明本文来自CMDN jim先生 
原创网址:[https://blog.csdn.net/u011967234/article/details/72760008](https://blog.csdn.net/u011967234/article/details/72760008)
本人感觉 jim先生写的清楚明了,对自己有用以方便以后使用抄录过来。

环境

	下载Windows版本OpenOffice;
	官网地址:http://www.openoffice.org/download/index.html。

代码实现

//main方法测试
public static void main(String[] args) {
        convertWord2Pdf("C:\\Users\\Desktop\\2020-xxxxx.docx");
    }
    /**
     *
     * @description:将Office格式的文档转换为PDF格式的文档
     * @param inputFilePath
     *
     */
    public static void convertWord2Pdf(String inputFilePath) {

        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        // OpenOffice安装在本地环境的目录
        String officeHome = "D:\\OpenOffice 4";
        config.setOfficeHome(officeHome);

        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();

        OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
        String outputFilePath = getReplaceFileAbsolutePath(inputFilePath, ".pdf");
        File inputFile = new File(inputFilePath);
        if (inputFile.exists()) {
            File outputFile = new File(outputFilePath);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            // 进行PDF格式的转换
            converter.convert(inputFile, outputFile);
        }

        officeManager.stop();
    }



    /**
     *
     * @description:更改文档后缀为指定的后缀名
     * @param inputFilePath 输入文档的绝对路径
     * @param replaceEndWith 指定的后缀名
     * @return 返回替换指定后缀名的文档的绝对路径
     *
     */
    private static String getReplaceFileAbsolutePath(String inputFilePath, String replaceEndWith) {

        String replaceFilePath = null;
        Pattern pattern = Pattern.compile("(\\.[a-zA-Z]+)");
        Matcher matcher = pattern.matcher(inputFilePath);
        String endWith = null;
        if(matcher.find()) {
            endWith = matcher.group(1);
        }

        if(StringUtil.isEmpty(endWith)) {
            return null;
        }

        replaceFilePath = inputFilePath.replaceAll(endWith, replaceEndWith);
        return replaceFilePath;
    }


    /**
     *
     * @description:预览PDF文件
     * @param attathFile(文件流)
     * @param response
     *
     */
    public static void previewPdf(File attathFile, HttpServletResponse response) {

        response.setContentType("application/pdf");
        try {
            if(attathFile.exists()) {
                DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());
                DataInputStream dataInputStream = new DataInputStream(new FileInputStream(attathFile));

                byte[] buffer = new byte[2048];
                int len = buffer.length;
                while((len = dataInputStream.read(buffer, 0, len)) != -1) {
                    dataOutputStream.write(buffer);
                    dataOutputStream.flush();
                }

                dataInputStream.close();
                dataOutputStream.close();
            }
        } catch (Exception e) {
            /*logger.error(e.getMessage(), e);*/
            e.printStackTrace();
        }
    }

需要准备的maven依赖

<!-- 在线预览office文件 	start-->
		<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
		<dependency>
			<groupId>commons-cli</groupId>
			<artifactId>commons-cli</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.artofsolving/jodconverter -->
		<dependency>
			<groupId>com.artofsolving</groupId>
			<artifactId>jodconverter</artifactId>
			<version>2.2.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/juh -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>juh</artifactId>
			<version>4.1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/jurt -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>jurt</artifactId>
			<version>4.1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/ridl -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>ridl</artifactId>
			<version>4.1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-jdk14</artifactId>
			<version>1.7.25</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.openoffice/unoil -->
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>unoil</artifactId>
			<version>4.1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>1.3.1</version>
		</dependency>

		<dependency>
			<groupId>com.github.livesense</groupId>
			<artifactId>jodconverter-core</artifactId>
			<version>1.0.5</version>
		</dependency>
		<!-- 在线预览office文件 	end-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值