Java實現(xiàn)word文檔轉(zhuǎn)成圖片的示例詳解
一、引用相關(guān)jar包
<!-- word轉(zhuǎn)圖工具 -->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-words</artifactId>
<version>23.1</version>
</dependency>
word操作包: aspose-words-21.1.jar, 如下鏈接自行下載:
aspose-words-21.1.jar, 提取碼為: zwcs
二、編輯一張word文檔
如下圖

三、將文檔關(guān)鍵字替換并轉(zhuǎn)換為圖片
代碼由下:
主類代碼:
package com.demo.ceshi;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import com.demo.ceshi.util.OfficeUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CeshiApplicationTests {
@Test
public void test4() {
try {
// 獲取word文檔
File file = new File("E:\\橫屏.docx");
// 讀取文件
InputStream ins = Files.newInputStream(file.toPath());
// 使用模板引擎將模板渲染,并傳入一個數(shù)據(jù)映射表 initWordMap()。
XWPFTemplate template = XWPFTemplate.compile(ins).render(OfficeUtils.initWordMap());
// 將模板渲染后保存為新的 Word 文件
template.writeToFile("E:\\test.docx");
// 填充數(shù)據(jù)完畢的test.docx,在轉(zhuǎn)換成圖片
File file1 = new File("E:\\test.docx");
// 打開生成的 Word 文件
Document doc = new Document(Files.newInputStream(file1.toPath()));
String filePath = "E:\\";
String pathPre = new Date().getTime() + ".png";
// 逐頁將 Word 文件保存為圖片(PNG格式)
for (int i = 0; i < doc.getPageCount(); i++) {
Document extractedPage = doc.extractPages(i,1);
// 拼接上文件名
String path = filePath + pathPre;
// 將 Word 文件保存為圖片PNG格式
extractedPage.save(path, SaveFormat.PNG);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
工具類代碼:
public class OfficeUtils {
/**
* word文檔需要填充的數(shù)據(jù)
* @return
*/
public static Map<String, Object> initWordMap() {
Map<String, Object> wordData = new HashMap<>();
wordData.put("XM", "陳XX");
wordData.put("ZSMC", "上班摸魚許可證");
wordData.put("KCMC", "野外生存摸魚達人");
wordData.put("JGMC", "Super摸魚有限責任公司");
wordData.put("BFRQ", "2024年3月23日");
return wordData;
}
}
四、執(zhí)行效果圖
如下:


到此這篇關(guān)于Java實現(xiàn)word文檔轉(zhuǎn)成圖片的示例詳解的文章就介紹到這了,更多相關(guān)Java word轉(zhuǎn)圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)Fibonacci(斐波那契)取余的示例代碼
這篇文章主要介紹了Java實現(xiàn)Fibonacci取余的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03
SpringBoot+Redis實現(xiàn)分布式緩存的方法步驟
在高并發(fā)的分布式的系統(tǒng)中,緩存是提升系統(tǒng)性能的重要手段,本文主要介紹了SpringBoot+Redis實現(xiàn)分布式緩存的方法步驟,具有一定的參考價值,感興趣的可以了解一下2024-07-07
在SpringBoot中配置Thymeleaf的模板路徑方式
這篇文章主要介紹了在SpringBoot中配置Thymeleaf的模板路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
SpringBoot配置多數(shù)據(jù)源的四種方式分享
在日常開發(fā)中我們都是以單個數(shù)據(jù)庫進行開發(fā),在小型項目中是完全能夠滿足需求的,但是,當我們牽扯到大型項目的時候,單個數(shù)據(jù)庫就難以承受用戶的CRUD操作,那么此時,我們就需要使用多個數(shù)據(jù)源進行讀寫分離的操作,本文就給大家介紹SpringBoot配置多數(shù)據(jù)源的方式2023-07-07
Java?8?的異步編程利器?CompletableFuture的實例詳解
這篇文章主要介紹了Java?8?的異步編程利器?CompletableFuture?詳解,本文通過一個例子給大家介紹下Java?8??CompletableFuture異步編程的相關(guān)知識,需要的朋友可以參考下2022-03-03

