word转pdf

来源:互联网 发布:手机音乐可视化软件 编辑:程序博客网 时间:2024/06/05 02:29
/**
 * Project Name:sxsj-manager-web
 * File Name:TestController.java
 * Package Name:com.soft.controllers.common
 * Date:2017年7月30日上午10:34:36
 * Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.soft.controllers.common;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;

import com.soft.common.util.CommonsUtil;
import com.soft.common.util.ResponseObject;

/**
 * ClassName:TestController <br/>
 * Function:  ADD FUNCTION. <br/>
 * Reason:      ADD REASON. <br/>
 * Date:     2017年7月30日 上午10:34:36 <br/>
 * @author   wpengfei
 * @version  
 * @since    JDK 1.6
 * @see     
 */
@Path("/")
public class TestController {
    
    
    
    /**
     * getCommonHtml:(这里用一句话描述这个方法的作用). <br/>
     * localhost:8080/common/getCommonHtml
     *
     * @author wpengfei
     * @param inv
     * @param baseTableId
     * @return
     * @since JDK 1.6
     */
    @Get("/getCommonHtml")
    public String getCommonHtml(Invocation inv,
             @Param("baseTableId") String baseTableId) {
        
        WordConvert w = new WordConvert();
        String sourceFile = "D:\\news.docx";
        String distFile = "D:\\new111.pdf";
        w.wordConvertPdf(sourceFile, distFile);
        return "@" + CommonsUtil.returnObjectToJson(ResponseObject.newSuccessResponseObject(true));
    }

    
    
}



/**
 * Project Name:zzw-timer
 * File Name:WordConvert.java
 * Package Name:com.wt.zzw.aspose.words
 * Date:2015年12月23日下午4:19:53
 * Copyright (c) 2015, chenzhou1025@126.com All Rights Reserved.
 *
 */

package com.soft.controllers.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

import org.apache.log4j.Logger;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

/**
 * ClassName:WordConvert <br/>
 * Function: ADD FUNCTION. <br/>
 * Reason: ADD REASON. <br/>
 * Date: 2015年12月23日 下午4:19:53 <br/>
 *
 * @author wpengfei
 * @version
 * @since JDK 1.6
 * @see
 */
public class WordConvert {

    private Logger logger = Logger.getLogger(this.getClass());

    /**
     * 获取license
     *
     * @return
     */
    public static boolean getLicense() {

        boolean result = false;

        try {

            URL url = Thread.currentThread().getContextClassLoader().getResource("license.xml");
            if(null == url){
                
                ClassLoader loader = WordConvert.class.getClassLoader();
                url = loader.getResource("license.xml");
            }
            InputStream is = new FileInputStream(url.getPath());
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
    

    
    /**
     * wordConvertPdf:(word(doc.docx)转换为pdf格式). <br/>
     *
     * @author wpengfei
     * @param sourceFile
     * @param distFile
     * @return
     * @since JDK 1.6
     */
    public boolean wordConvertPdf(String sourceFile, String distFile) {

         //验证License
        if (!getLicense()) {
            return false;
        }

        try {

            long old = System.currentTimeMillis();
            Document doc = new Document(sourceFile); //原始word路径
            File pdfFile = new File(distFile); //输出路径
            FileOutputStream fileOS = new FileOutputStream(pdfFile);

            doc.save(fileOS, SaveFormat.PDF);

            long now = System.currentTimeMillis();
            logger.info(sourceFile+"共耗时:" + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {

            e.printStackTrace();
            return false;
        }

        return true;
    }

}

<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>