最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

總結(jié)

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

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

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>

這個(gè)工具使用了poi,最新的2.0.3對(duì)應(yīng)poi的5.2.0,2.0.1對(duì)應(yīng)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操作 (直接寫入響應(yīng))
PdfConverter.getInstance().convert(document, response.getOutputStream(), pdfOptions);
response.setContentType("application/pdf");

或者寫入輸出流

    /**
     * 將word轉(zhuǎn)為pdf并返回一個(gè)輸出流
     *
     * @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的時(shí)候經(jīng)常出問題(直接報(bào)錯(cuò)或者合并無(wú)效)

研究了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一堆的依賴,這個(gè)一個(gè)就好,而且本身就支持轉(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 去除水印
     * 若不驗(yàn)證則轉(zhuǎn)化出的pdf文檔會(huì)有水印產(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,用這個(gè)轉(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("報(bào)告下載失敗", 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并返回一個(gè)輸入流
     *
     * @param outputStream 輸出文件名(pdf格式)
     */
    public static ByteArrayInputStream outputStreamToPdfInputStream(ByteArrayOutputStream outputStream) throws IOException {
        //輸出的pdf輸出流轉(zhuǎn)輸入流
        try {
            //臨時(shí)
            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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis-Plus或PageHelper多表分頁(yè)查詢總條數(shù)不對(duì)問題的解決方法

    Mybatis-Plus或PageHelper多表分頁(yè)查詢總條數(shù)不對(duì)問題的解決方法

    PageHelper 這個(gè)插件用了很多次了,今天使用的時(shí)候才遇到一個(gè)問題,這篇文章主要給大家介紹了關(guān)于Mybatis-Plus或PageHelper多表分頁(yè)查詢總條數(shù)不對(duì)問題的解決方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Java 基礎(chǔ)之修飾符關(guān)鍵詞整理

    Java 基礎(chǔ)之修飾符關(guān)鍵詞整理

    這篇文章主要介紹了Java 基礎(chǔ)之修飾符關(guān)鍵詞整理的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • SpringMVC的SseEmitter實(shí)時(shí)推送方式

    SpringMVC的SseEmitter實(shí)時(shí)推送方式

    SseEmitter是SpringMVC提供的一種技術(shù),可以實(shí)現(xiàn)服務(wù)端向客戶端實(shí)時(shí)推送數(shù)據(jù)的功能,在Controller中提供一個(gè)接口返回SseEmitter對(duì)象,發(fā)送數(shù)據(jù)可以在另一個(gè)接口調(diào)用其send方法,SpringBoot已經(jīng)集成此功能
    2026-03-03
  • String.join()方法示例詳解

    String.join()方法示例詳解

    String.join() 方法是連接指定數(shù)組的元素或集合的成員,在每個(gè)元素或成員之間使用指定的分隔符,這篇文章主要介紹了String.join()方法示例詳解,需要的朋友可以參考下
    2024-01-01
  • mybatis連接PGSQL中對(duì)于json和jsonb的處理方法

    mybatis連接PGSQL中對(duì)于json和jsonb的處理方法

    在使用PostgreSQL數(shù)據(jù)庫(kù)時(shí),將表字段設(shè)置為jsonb格式可以存儲(chǔ)JSON數(shù)據(jù),本文給大家介紹mybatis連接PGSQL中對(duì)于json和jsonb的處理方法,感興趣的朋友一起看看吧
    2024-11-11
  • @NotEmpty、@NotBlank、@NotNull的區(qū)別

    @NotEmpty、@NotBlank、@NotNull的區(qū)別

    這篇文章主要介紹了@NotEmpty、@NotBlank、@NotNull的區(qū)別,需要的朋友可以參考下
    2016-09-09
  • Spring擴(kuò)展接口知識(shí)總結(jié)

    Spring擴(kuò)展接口知識(shí)總結(jié)

    今天帶大家學(xué)習(xí)Java Spring的相關(guān)知識(shí),文中對(duì)Spring擴(kuò)展接口作了非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Java字符串相關(guān)類使用方法詳解

    Java字符串相關(guān)類使用方法詳解

    String、StringBuilder、StringBuffer還傻傻分不清,下面這篇文章主要給大家介紹了關(guān)于Java字符串相關(guān)類使用的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • 使用Swagger時(shí)Controller中api接口顯示不全的問題分析及解決

    使用Swagger時(shí)Controller中api接口顯示不全的問題分析及解決

    swagger是一個(gè)十分好用的api接口管理、測(cè)試框架,現(xiàn)在越來(lái)越多的人使用這個(gè)做接口的測(cè)試和管理,但經(jīng)常遇到Controller中的api接口顯示不全的問題,所以本文給大家詳細(xì)分析了問題以及解決方法,需要的朋友可以參考下
    2024-02-02
  • 關(guān)于后端如何解決跨域的問題說(shuō)明

    關(guān)于后端如何解決跨域的問題說(shuō)明

    這篇文章主要介紹了關(guān)于后端如何解決跨域的問題說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08

最新評(píng)論

布尔津县| 炎陵县| 十堰市| 遂溪县| 浑源县| 南溪县| 攀枝花市| 盐池县| 正安县| 贵阳市| 江华| 和平县| 武义县| 淳安县| 迁西县| 鄂托克旗| 德兴市| 库伦旗| 尤溪县| 东明县| 杨浦区| 磐石市| 手游| 思茅市| 武宁县| 辽宁省| 左贡县| 德州市| 宿迁市| 泸定县| 彝良县| 肃宁县| 灵山县| 长乐市| 大同县| 田林县| 尉氏县| 尉犁县| 台州市| 南部县| 云浮市|