Java實(shí)現(xiàn)word/pdf轉(zhuǎn)html并在線預(yù)覽
實(shí)現(xiàn)文檔在線預(yù)覽的方式除了上篇文章《文檔在線預(yù)覽(一)通過(guò)將txt、word、pdf轉(zhuǎn)成圖片實(shí)現(xiàn)在線預(yù)覽功能》說(shuō)的將文檔轉(zhuǎn)成圖片的實(shí)現(xiàn)方式外,還有轉(zhuǎn)成pdf,前端通過(guò)pdf.js、pdfobject.js等插件來(lái)實(shí)現(xiàn)在線預(yù)覽,以及本文將要說(shuō)到的將文檔轉(zhuǎn)成html的方式來(lái)實(shí)現(xiàn)在線預(yù)覽。代碼基于 aspose-words(用于word轉(zhuǎn)html),pdfbox(用于pdf轉(zhuǎn)html),所以事先需要在項(xiàng)目里下面兩個(gè)依賴:
<dependency> <groupId>com.luhuiguo</groupId> <artifactId>aspose-words</artifactId> <version>23.1</version></dependency> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.4</version> </dependency>
一、將文件轉(zhuǎn)換成html字符串
1、將word文件轉(zhuǎn)成html字符串
public static String wordToHtmlStr(String wordPath) { try { Document doc = new Document(wordPath); // Address是將要被轉(zhuǎn)化的word文檔 String htmlStr = doc.toString(); return htmlStr; } catch (Exception e) { e.printStackTrace(); } return null; }
驗(yàn)證結(jié)果:
2、將pdf文件轉(zhuǎn)成html字符串
public static String pdfToHtmlStr(String pdfPath) throws IOException, ParserConfigurationException { PDDocument document = PDDocument.load(new File(pdfPath)); Writer writer = new StringWriter(); new PDFDomTree().writeText(document, writer); writer.close(); document.close(); return writer.toString(); }
驗(yàn)證結(jié)果:
二、將文件轉(zhuǎn)換成html,并生成html文件
有時(shí)我們是需要的不僅僅返回html字符串,而是需要生成一個(gè)html文件這時(shí)應(yīng)該怎么做呢?一個(gè)改動(dòng)量小的做法就是使用org.apache.commons.io包下的FileUtils工具類寫入目標(biāo)地址:
1、FileUtils類將html字符串生成html文件示例
首先需要引入pom:
<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.8.0</version> </dependency>
相關(guān)代碼:
String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\書籍\\電子書\\小說(shuō)\\歷史小說(shuō)\\最后的可汗.doc"); FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,還可以對(duì)上面的代碼進(jìn)行一些調(diào)整,已實(shí)現(xiàn)生成html文件,代碼調(diào)整如下:
2、將word文件轉(zhuǎn)換成html文件
public static void wordToHtml(String wordPath, String htmlPath) { try { File sourceFile = new File(wordPath); String path = htmlPath + File.separator + sourceFile.getName().substring(0, sourceFile.getName().lastIndexOf(".")) + ".html"; File file = new File(path); // 新建一個(gè)空白pdf文檔 FileOutputStream os = new FileOutputStream(file); Document doc = new Document(wordPath); // Address是將要被轉(zhuǎn)化的word文檔 HtmlSaveOptions options = new HtmlSaveOptions(); options.setExportImagesAsBase64(true); options.setExportRelativeFontSize(true); doc.save(os, options); } catch (Exception e) { e.printStackTrace(); } }
驗(yàn)證結(jié)果:
3、將pdf文件轉(zhuǎn)換成html文件
public static void pdfToHtml(String pdfPath, String htmlPath) throws IOException, ParserConfigurationException { File file = new File(pdfPath); String path = htmlPath + File.separator + file.getName().substring(0, file.getName().lastIndexOf(".")) + ".html"; PDDocument document = PDDocument.load(new File(pdfPath)); Writer writer = new PrintWriter(path, "UTF-8"); new PDFDomTree().writeText(document, writer); writer.close(); document.close(); }
圖片版PDF文件驗(yàn)證結(jié)果:
文字版PDF文件驗(yàn)證結(jié)果:
到此這篇關(guān)于Java實(shí)現(xiàn)word/pdf轉(zhuǎn)html并在線預(yù)覽的文章就介紹到這了,更多相關(guān)Java在線預(yù)覽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Java和Apache POI實(shí)現(xiàn)HTML轉(zhuǎn)Word的完整指南
- 關(guān)于Java實(shí)現(xiàn)word(docx、doc)轉(zhuǎn)html的完美解決方案
- Java實(shí)現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼
- Java實(shí)現(xiàn)HTML轉(zhuǎn)為Word的示例代碼
- Java 將Word轉(zhuǎn)為HTML的方法
- java使用POI實(shí)現(xiàn)html和word相互轉(zhuǎn)換
- java實(shí)現(xiàn)word文件轉(zhuǎn)html文件
- Java實(shí)現(xiàn)將HTML文件與字符串轉(zhuǎn)為Word
相關(guān)文章
SpringBoot整合SpringSecurity和JWT的示例
這篇文章主要介紹了SpringBoot整合SpringSecurity和JWT的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java使用JSONObject需要的6個(gè)jar包下載地址
這篇文章主要介紹了Java使用JSONObject需要的6個(gè)jar包下載地址,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之校園一卡通系統(tǒng)的實(shí)現(xiàn)
這是一個(gè)使用了java+Springboot+Maven+mybatis+Vue+mysql+wd開(kāi)發(fā)的校園一卡通系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有校園一卡通系統(tǒng)該有的所有功能,感興趣的朋友快來(lái)看看吧2022-01-01一文詳解Java?etcd的應(yīng)用場(chǎng)景及編碼實(shí)戰(zhàn)
etcd?是一個(gè)高度一致的分布式鍵值存儲(chǔ)系統(tǒng)。本文旨在幫助大家理解etcd,從宏觀角度俯瞰etcd全局,掌握etcd的基本操作技能,需要的可以參考一下2022-08-08iOS socket網(wǎng)絡(luò)編程實(shí)例詳解
socket是一個(gè)針對(duì)TCP和UDP編程的接口,你可以借助它建立TCP連接等。這篇文章主要介紹了iOS socket網(wǎng)絡(luò)編程 ,需要的朋友可以參考下2017-03-03FeignClient如何通過(guò)配置變量調(diào)用配置文件url
這篇文章主要介紹了FeignClient如何通過(guò)配置變量調(diào)用配置文件url,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06微信公眾號(hào)測(cè)試賬號(hào)自定義菜單的實(shí)例代碼
這篇文章主要介紹了微信公眾號(hào)測(cè)試賬號(hào)自定義菜單的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02SpringBoot解決同名類導(dǎo)致的bean名沖突bean name conflicts問(wèn)題
這篇文章主要介紹了SpringBoot解決同名類導(dǎo)致的bean名沖突bean name conflicts問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06