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

java將word轉pdf的方法示例詳解

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

總結

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

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

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

poi工具轉換

        <!-- 處理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 );
//轉pdf操作 (直接寫入響應)
PdfConverter.getInstance().convert(document, response.getOutputStream(), pdfOptions);
response.setContentType("application/pdf");

或者寫入輸出流

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

        return outputStream;
    }

問題

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

研究了2天,pdf轉一直各種問題,一起之下換技術

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一堆的依賴,這個一個就好,而且本身就支持轉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 去除水印
     * 若不驗證則轉化出的pdf文檔會有水印產生
     */
    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 轉 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轉pdf失敗", e);
        }
    }

    /**
     * word 轉 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轉pdf失敗", e);
        }
    }
}

使用

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

我是依舊使用poi處理word,用這個轉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,需要現將poi的document轉為普通的輸入流
            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 流關閉失敗", e1);
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e1) {
                    log.error("in 流關閉失敗", 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轉為pdf并返回一個輸入流
     *
     * @param outputStream 輸出文件名(pdf格式)
     */
    public static ByteArrayInputStream outputStreamToPdfInputStream(ByteArrayOutputStream outputStream) throws IOException {
        //輸出的pdf輸出流轉輸入流
        try {
            //臨時
            byte[] bookByteAry = outputStream.toByteArray();
            return new ByteArrayInputStream(bookByteAry);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

完美轉換

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

相關文章

  • Mybatis-Plus或PageHelper多表分頁查詢總條數不對問題的解決方法

    Mybatis-Plus或PageHelper多表分頁查詢總條數不對問題的解決方法

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

    Java 基礎之修飾符關鍵詞整理

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

    SpringMVC的SseEmitter實時推送方式

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

    String.join()方法示例詳解

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

    mybatis連接PGSQL中對于json和jsonb的處理方法

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

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

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

    Spring擴展接口知識總結

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

    Java字符串相關類使用方法詳解

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

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

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

    關于后端如何解決跨域的問題說明

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

最新評論

张北县| 元氏县| 望都县| 河源市| 梁山县| 海城市| 乐业县| 辽宁省| 内江市| 石泉县| 阿坝县| 黔东| 金华市| 西城区| 连城县| 兰州市| 宣武区| 芦溪县| 奎屯市| 炎陵县| 长治市| 辉南县| 依安县| 共和县| 永济市| 北碚区| 邵阳市| 全椒县| 安达市| 郸城县| 石柱| 鹰潭市| 镇巴县| 丁青县| 邛崃市| 介休市| 镇宁| 马山县| 威远县| 青龙| 莆田市|