欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java將word轉(zhuǎn)pdf的方法示例詳解

 更新時間:2025年01月26日 09:47:06   作者:ziyue7575  
這篇文章主要介紹了java將word轉(zhuǎn)pdf的相關(guān)資料,文中講解了使用Aspose-Words工具將Word文檔轉(zhuǎn)換為PDF的優(yōu)劣,并提供了一種在Java項目中使用Aspose-Words進行Word轉(zhuǎn)PDF的示例方法,需要的朋友可以參考下

總結(jié)

建議使用aspose-words轉(zhuǎn)pdf,poi的容易出問題還丑…

poi的(多行的下邊框就不對了)

aspose-words的(基本和word一樣)

poi工具轉(zhuǎn)換

        <!-- 處理PDF -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.3</version>
        </dependency>

這個工具使用了poi,最新的2.0.3對應poi的5.2.0,2.0.1對應poi的3.15

使用

//拿到word流
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/muban3.docx");
        if (inputStream == null) {
            throw new MsgException("讀取模板失敗");
        }
XWPFDocument document = new XWPFDocument(inputStream);
//.....word處理
PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding( BaseFont.CP1250 );
//轉(zhuǎn)pdf操作 (直接寫入響應)
PdfConverter.getInstance().convert(document, response.getOutputStream(), pdfOptions);
response.setContentType("application/pdf");

或者寫入輸出流

    /**
     * 將word轉(zhuǎn)為pdf并返回一個輸出流
     *
     * @param document 輸出文件名(pdf格式)
     */
    public static ByteArrayOutputStream wordToPdfOutputStream(XWPFDocument document) throws IOException {
        //word轉(zhuǎn)pdf
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfOptions pdfOptions = PdfOptions.create();//.fontEncoding( BaseFont.CP1250 );
        //轉(zhuǎn)pdf操作
        PdfConverter.getInstance().convert(document, outputStream, pdfOptions);

        return outputStream;
    }

問題

poi改了word之后,生成沒問題,word中創(chuàng)建的表格,轉(zhuǎn)pdf的時候經(jīng)常出問題(直接報錯或者合并無效)

研究了2天,pdf轉(zhuǎn)一直各種問題,一起之下?lián)Q技術(shù)

aspose-words

https://blog.csdn.net/Wang_Pink/article/details/141898210

        &lt;dependency&gt;
            &lt;groupId&gt;com.luhuiguo&lt;/groupId&gt;
            &lt;artifactId&gt;aspose-words&lt;/artifactId&gt;
            &lt;version&gt;23.1&lt;/version&gt;
        &lt;/dependency&gt;

poi處理word一堆的依賴,這個一個就好,而且本身就支持轉(zhuǎn)pdf!!!

使用

  • 在resources創(chuàng)建word-license.xml

<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>
  • 工具類
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

@Slf4j
public class Doc2PdfUtil {

    /**
     * 獲取 license 去除水印
     * 若不驗證則轉(zhuǎn)化出的pdf文檔會有水印產(chǎn)生
     */
    private static void getLicense() {
        String licenseFilePath = "word-license.xml";
        try {
            InputStream is = Doc2PdfUtil.class.getClassLoader().getResourceAsStream(licenseFilePath);
            License license = new License();
            license.setLicense(Objects.requireNonNull(is));
        } catch (Exception e) {
            log.error("license verify failed");
            e.printStackTrace();
        }
    }

    /**
     * word 轉(zhuǎn) pdf
     *
     * @param wordFile word 文件路徑
     * @param pdfFile  生成的 pdf 文件路徑
     */
    public static void word2Pdf(String wordFile, String pdfFile) {
        File file = new File(pdfFile);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }
        getLicense();
        try (FileOutputStream os = new FileOutputStream(new File(pdfFile))) {
            Document doc = new Document(wordFile);
            doc.save(os, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("word轉(zhuǎn)pdf失敗", e);
        }
    }

    /**
     * word 轉(zhuǎn) pdf
     *
     * @param wordFile word 文件流
     * @param pdfFile  生成的 pdf 文件流
     */
    public static void word2Pdf(InputStream wordFile, OutputStream pdfFile) {
        getLicense();
        try {
            Document doc = new Document(wordFile);
            doc.save(pdfFile, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("word轉(zhuǎn)pdf失敗", e);
        }
    }
}

使用

Doc2PdfUtil.word2Pdf("aa.docx","bb.pdf");

我是依舊使用poi處理word,用這個轉(zhuǎn)pdf

//拿到word流
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("word/muban3.docx");
        if (inputStream == null) {
            throw new MsgException("讀取模板失敗");
        }
XWPFDocument document = new XWPFDocument(inputStream);
//.....word處理
        ByteArrayInputStream in = null;
        try {
            //由于使用的poi的document,需要現(xiàn)將poi的document轉(zhuǎn)為普通的輸入流
            in = WordUtil.getInputStream(document);
            Doc2PdfUtil.word2Pdf(in,response.getOutputStream());
            response.setContentType("application/pdf");

        } catch (Exception e) {
            log.error("報告下載失敗", e);
        } finally {
            try {
                document.close();
            } catch (Exception e1) {
                log.error("document 流關(guān)閉失敗", e1);
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e1) {
                    log.error("in 流關(guān)閉失敗", e1);
                }
            }
        }
    public static ByteArrayInputStream getInputStream(XWPFDocument document) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            document.write(outputStream);
            return outputStreamToPdfInputStream(outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 將word轉(zhuǎn)為pdf并返回一個輸入流
     *
     * @param outputStream 輸出文件名(pdf格式)
     */
    public static ByteArrayInputStream outputStreamToPdfInputStream(ByteArrayOutputStream outputStream) throws IOException {
        //輸出的pdf輸出流轉(zhuǎn)輸入流
        try {
            //臨時
            byte[] bookByteAry = outputStream.toByteArray();
            return new ByteArrayInputStream(bookByteAry);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

完美轉(zhuǎn)換

到此這篇關(guān)于java將word轉(zhuǎn)pdf的文章就介紹到這了,更多相關(guān)java將word轉(zhuǎn)pdf內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Installij IDEA install或clean項目的使用

    Installij IDEA install或clean項目的使用

    這篇文章主要介紹了Installij IDEA install或clean項目的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • spring boot項目生成docker鏡像并完成容器部署的方法步驟

    spring boot項目生成docker鏡像并完成容器部署的方法步驟

    這篇文章主要介紹了spring boot項目生成docker鏡像并完成容器部署的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Java遞歸調(diào)用如何實現(xiàn)數(shù)字的逆序輸出方式

    Java遞歸調(diào)用如何實現(xiàn)數(shù)字的逆序輸出方式

    這篇文章主要介紹了Java遞歸調(diào)用如何實現(xiàn)數(shù)字的逆序輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 使用IDEA創(chuàng)建一個vert.x項目的方法

    使用IDEA創(chuàng)建一個vert.x項目的方法

    這篇文章主要介紹了使用IDEA創(chuàng)建一個vert.x項目的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法

    springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法

    這篇文章主要介紹了springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-06-06
  • 排序算法的Java實現(xiàn)全攻略

    排序算法的Java實現(xiàn)全攻略

    這篇文章主要介紹了排序算法的Java實現(xiàn),包括Collections.sort()的使用以及各種經(jīng)典算法的Java代碼實現(xiàn)方法總結(jié),超級推薦!需要的朋友可以參考下
    2015-08-08
  • Java集合ArrayDeque類實例分析

    Java集合ArrayDeque類實例分析

    這篇文章主要介紹了Java集合ArrayDeque類實例分析的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot Redis實現(xiàn)接口冪等性校驗方法詳細講解

    SpringBoot Redis實現(xiàn)接口冪等性校驗方法詳細講解

    這篇文章主要介紹了SpringBoot Redis實現(xiàn)接口冪等性校驗方法,近期一個老項目出現(xiàn)了接口冪等性校驗問題,前端加了按鈕置灰,依然被人拉著接口參數(shù)一頓輸出,還是重復調(diào)用了接口,通過復制粘貼,完成了后端接口冪等性調(diào)用校驗
    2022-11-11
  • SpringBoot中集成Swagger2及簡單實用

    SpringBoot中集成Swagger2及簡單實用

    使用Swagger你只需要按照它的規(guī)范去定義接口及接口相關(guān)的信息,再通過Swagger衍生出來的一系列項目和工具,就可以做到生成各種格式的接口文檔,以及在線接口調(diào)試頁面等等,這篇文章主要介紹了SpringBoot中集成Swagger2,需要的朋友可以參考下
    2023-06-06
  • Java判斷字符串是否含有亂碼實例代碼

    Java判斷字符串是否含有亂碼實例代碼

    本文通過實例代碼給大家介紹了Java判斷字符串是否含有亂碼的方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧
    2018-11-11

最新評論