java使用poi轉換doc/docx為pdf


為了方便前端預覽word文件,上傳后進行pdf轉換(也可以預覽時生成臨時文件)*注word中插入的表格的話表格內字體都要為宋體不然轉出來為空

引用jar包

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
    <dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
  <version>2.0.1</version>
    </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency>

 

docx轉換比較簡單代碼如下

        FileInputStream fileInputStream = null;
        FileOutputStream  fileOutputStream=null;
        try {
            // 讀取docx文件
            fileInputStream = new FileInputStream(inPath);
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions pdfOptions = PdfOptions.create();
            // 輸出路徑
            fileOutputStream = new FileOutputStream(outPath);
            // 調用轉換
           PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
                fileInputStream.close();
                fileOutputStream.close();
        }                    

doc不能直接通過poi轉換pdf,看到有教程先轉html然后再轉pdf,要結合itext還要處理html標簽閉合問題覺得太麻煩(好吧 其實是我懶).

轉換思路找到一個jar包可以把doc轉成docx :spire.doc.free-3.9.0.jar

網址:https://www.e-iceblue.cn/Introduce/Free-Spire-Doc-JAVA.html        下載后引入或安裝到本地maven倉庫添加pom即可

官方有提示:免費版有篇幅限制。在加載或保存Word 文檔時,要求 Word 文檔不超過 500 個段落,25 個表格。同時將 Word 文檔轉換為 PDF 和 XPS 等格式時,僅支持轉換前三頁。

對於場景來說五百個段落足夠了,轉前三頁有點少  所以我們在這里僅使用doc轉docx的方法代碼如下:

 /**
     * doc轉docx
     * @param path
     * @param outPath
     * @return
     */
    public static void docToDOcx(String path,String outPath){
        Document dc = new Document();
        dc.loadFromFile(path);
        dc.saveToFile(outPath, FileFormat.Docx_2013);
        dc.close();
    }   

 

整體工具類對文檔進行兼容判斷處理,doc就生成臨時docx文檔進行轉換代碼如下:

public class PdfUtil {
    /**
     * 2003- 版本的word
     */
    private static final String word2003L = ".doc";
    /**
     * 2007+ 版本的word
     */
    private final static String word2007U = ".docx";

    /**
     * word轉pdf
     * @param inPath
     * @param outPath
     * @return
     */
    public static boolean doc2pdf(String inPath, String outPath)throws IOException{
        // 是否需清除中間轉換的docx文檔
        Boolean isDelete = false;
        String fileType = inPath.substring(inPath.lastIndexOf(".")).toLowerCase();
        if (word2003L.equals(fileType)){
            docToDOcx(inPath,inPath+"x");
            inPath = inPath+"x";
            isDelete = true;
        }else if(word2007U.equals(fileType)){

        }else {
            return false;
        }
        FileInputStream fileInputStream = null;
        FileOutputStream  fileOutputStream=null;
        try {
            fileInputStream = new FileInputStream(inPath);
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions pdfOptions = PdfOptions.create();
            fileOutputStream = new FileOutputStream(outPath);
            PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

                fileInputStream.close();
                fileOutputStream.close();
                if (isDelete){
                    deleteDocx(inPath);
                }
        }
        return true;
    }

    /**
     * doc轉docx
     * @param path
   * @param outPath
     * @return
     */
    public static void docToDOcx(String path,String outPath){
        Document dc = new Document();
        dc.loadFromFile(path);
        dc.saveToFile(outPath, FileFormat.Docx_2013);
        dc.close();
    }

    /**
     * 清除臨時轉換docx
     * @param path
     */
    public static void  deleteDocx(String path){
        File file = new File(path);
        file.delete();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM