java word(doc docx excel) to pdf

java 代码

 

 

package com.ibeetl.admin.core.util.file;

import java.io.*;

import org.aspectj.weaver.ast.Test;
 
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;


/**
 * @author Administrator
 * @version $Id$
 * @since
 * @see
 */
public class WordToPdfUtil {
 //此插件不需要安装office服务但是此插件加密,如不注册会有水印
//    public static void main(String[] args) {
//        //doc2pdf("C:/Users/lss/Desktop/test.doc");
//     doc2pdf("F:\\doc\\a.doc","F:\\doc\\a.pdf");
//    }
    public static boolean getLicense() {
       boolean result = false;
        try {
            InputStream is = Test.class.getClassLoader().getResourceAsStream("/license/license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
            License aposeLic = new License();
            //aposeLic.setLicense("GsNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=");
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
           e.printStackTrace();
        }
        return result;
    }
 
    public static void doc2pdf(String inPath, String outPath) {
//        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
//            return;
//        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(outPath); // 新建一个空白pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(inPath); // Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                         // EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void doc2pdf(File inFile, File outFile) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
//        if (!getLicense()) {
//            return;
//        }
        FileInputStream os_inFile = null;
        try {
            long old = System.currentTimeMillis();
            //File file = new File(outPath); // 新建一个空白pdf文档
            os_inFile = new FileInputStream(inFile);
            FileOutputStream os_outfile = new FileOutputStream(outFile);
            // Address是将要被转化的word文档
            Document doc = new Document(os_inFile);
            // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,EPUB, XPS, SWF 相互转换
            doc.save(os_outfile, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            System.out.println(os_inFile+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                os_inFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void savedocx(String inPath, String outPath) {
      if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
          return;
      }
      try {
          long old = System.currentTimeMillis();
          File file = new File(outPath); // 新建一个空白pdf文档
          FileOutputStream os = new FileOutputStream(file);
          Document doc = new Document(inPath); // Address是将要被转化的word文档
          doc.save(os, SaveFormat.DOCX);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
                                       // EPUB, XPS, SWF 相互转换
          long now = System.currentTimeMillis();
          System.out.println(outPath+"生成共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
      } catch (Exception e) {
          e.printStackTrace();
      }
  }

        public static void main(String[] args) {
      String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
      String wordFile = PDFTIMEDIR + "新建DOC文档.doc";
      String pdfFile = PDFTIMEDIR + "新建DOC文档.doc.pdf";
      //doc2pdf(wordFile, pdfFile);
      doc2pdf(new File(wordFile), new File(pdfFile));


//    String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
//    String wordFile = PDFTIMEDIR + "新建DOCX文档.docx.docx";
//    String pdfFile = PDFTIMEDIR + "新建DOCX文档.docx.pdf";
//    doc2pdf(wordFile, pdfFile);


//    String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
//    String wordFile = PDFTIMEDIR + "新建PPT演示文稿.ppt";
//    String pdfFile = PDFTIMEDIR + "新建PPT演示文稿.ppt.pdf";
//    doc2pdf(wordFile, pdfFile);

//
//        String PDFTIMEDIR = "C:\\Users\\chenye\\Desktop\\";
//        String wordFile = PDFTIMEDIR + "新建 RTF 文档.rtf";
//        String pdfFile = PDFTIMEDIR + "新建 RTF 文档.rtf.pdf";
//        doc2pdf(wordFile, pdfFile);


    }
    
}

 

maven代码

主要是aspose 这个jar公共仓库下载不到

需要去aspose的私有仓库下载

地址:https://repository.aspose.com/repo

 

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>word</groupId>
   <artifactId>wordToPdf</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>wordToPdf</name>
   <url>http://maven.apache.org</url>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>

   <dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>3.8.1</version>
         <scope>test</scope>
      </dependency>
      <!-- word to pdf -->
      <dependency>
         <groupId>com.aspose</groupId>
         <artifactId>aspose-words</artifactId>
         <version>19.4</version>
         <classifier>jdk17</classifier>
      </dependency>
   </dependencies>
   <repositories>
      <!-- aspose word to pdf -->
      <repository>
         <id>AsposeJavaAPI</id>
         <name>Aspose Java API</name>
         <url>https://repository.aspose.com/repo/</url>
      </repository>
   </repositories>
</project>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java知路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值