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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章