java代码实现word转pdf(windows和linux都适用)

1.外挂一个jar包

aspose-words-15.8.0-jdk16.jar 这个需要到官网下载

2.准备一个配置文件

license.xml 放在maven项目的resources目录下

<License>
  <Data>
    <Products>
      <Product>Aspose.Total for JavaProduct>
      <Product>Aspose.Words for JavaProduct>
    Products>
    <EditionType>EnterpriseEditionType>
    <SubscriptionExpiry>20991231SubscriptionExpiry>
    <LicenseExpiry>20991231LicenseExpiry>
    <SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9SerialNumber>
  Data>
  <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=Signature>
License>

3.主要工具类

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import java.io.*;
public class WordToPdfUtil {

    public  boolean getLicense() {
        boolean result = false;
        try {
            InputStream is =this.getClass().getClassLoader().getResourceAsStream("license.xml"); 
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        WordToPdfUtil bean = new WordToPdfUtil();
        bean.word2Pdf2("C:\\TEST.doc","C:\\TEST.pdf");
    }

	/**
	* inpath: 输入word的路径,例如: C:\\TEST.doc
	* outpath: 输出pdf的路径,例如: C:\\TEST.pdf
	*/
    public  void word2Pdf2(String inpath,String outpath) throws Exception {
        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            System.out.println("非法------------");
            return;
        }

        long old = System.currentTimeMillis();
        File file = new File(outpath);  

        FileOutputStream os = new FileOutputStream(file);

        //解决乱码
        //如果是windows执行,不需要加这个
        //TODO 如果是linux执行,需要添加这个*****
        //FontSettings.setFontsFolder("/usr/share/fonts",true);

        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("共耗时:" + ((now - old) / 1000.0) + "秒");  
    }


    /**
     * @param path      pdf输出路径
     * @param wordInput word输入流
     * @param wordName  word文档的名称
     */
    public  void word2pdf(String path, InputStream wordInput, String wordName) throws FileNotFoundException {
        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            System.out.println("非法");
            return;
        }

        long old = System.currentTimeMillis();
        File file = new File(path + wordName + ".pdf");  //新建一个空白pdf文档
        FileOutputStream os = new FileOutputStream(file);

        Document doc = null;                    //Address是将要被转化的word文档
        try {
            doc = new Document(wordInput);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        } catch (Exception e) {
            e.printStackTrace();
        }
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
    }
}

4.linux配置字体

主要是: FontSettings.setFontsFolder("/usr/share/fonts",true);

  • yum install -y fontconfig mkfontscale
  • cd /usr/share/fonts目录
  • 把windows机器中 C:\Windows\Fonts里面的内容,全部拷贝到linux的上述目录(/usr/share/fonts中)
  • mkfontscale
  • mkfontdir
  • fc-cache
  • 最后再linux中执行上述代码,就可以解决乱码问题。

你可能感兴趣的:(java篇)