Java實現(xiàn)word/pdf轉(zhuǎn)html并在線預覽
實現(xiàn)文檔在線預覽的方式除了上篇文章《文檔在線預覽(一)通過將txt、word、pdf轉(zhuǎn)成圖片實現(xiàn)在線預覽功能》說的將文檔轉(zhuǎn)成圖片的實現(xiàn)方式外,還有轉(zhuǎn)成pdf,前端通過pdf.js、pdfobject.js等插件來實現(xiàn)在線預覽,以及本文將要說到的將文檔轉(zhuǎn)成html的方式來實現(xiàn)在線預覽。代碼基于 aspose-words(用于word轉(zhuǎn)html),pdfbox(用于pdf轉(zhuǎn)html),所以事先需要在項目里下面兩個依賴:
<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; }
驗證結(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(); }
驗證結(jié)果:
二、將文件轉(zhuǎn)換成html,并生成html文件
有時我們是需要的不僅僅返回html字符串,而是需要生成一個html文件這時應該怎么做呢?一個改動量小的做法就是使用org.apache.commons.io包下的FileUtils工具類寫入目標地址:
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:\\書籍\\電子書\\小說\\歷史小說\\最后的可汗.doc"); FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");
除此之外,還可以對上面的代碼進行一些調(diào)整,已實現(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); // 新建一個空白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(); } }
驗證結(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文件驗證結(jié)果:
文字版PDF文件驗證結(jié)果:
到此這篇關(guān)于Java實現(xiàn)word/pdf轉(zhuǎn)html并在線預覽的文章就介紹到這了,更多相關(guān)Java在線預覽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實現(xiàn)Word轉(zhuǎn)PDF的全過程
- Java調(diào)用py或者exe文件實現(xiàn)Word轉(zhuǎn)PDF
- Java實現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF
- Java實現(xiàn)WORD和PDF互相轉(zhuǎn)換以及數(shù)據(jù)填充示例
- java將word轉(zhuǎn)pdf的方法示例詳解
- Java將Word文檔轉(zhuǎn)換為PDF文件的幾種常用方法總結(jié)
- Java中Word與PDF轉(zhuǎn)換為圖片的方法詳解
- Java將Word轉(zhuǎn)換成PDF的常用用法
- 探討Java 將Markdown文件轉(zhuǎn)換為Word和PDF文檔
- Java將word文件轉(zhuǎn)成pdf文件的操作方法
- Java實現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF的兩種方法
相關(guān)文章
SpringBoot中yml的數(shù)據(jù)綁定示例
本文主要介紹了SpringBoot中yml的數(shù)據(jù)綁定示例,借助于YAML的簡潔語法和結(jié)構(gòu)化特性,我們能夠輕松地管理應用程序的配置信息,使得配置文件更加清晰易讀,感興趣的可以了解一下2023-11-11使用@PropertySource讀取配置文件通過@Value進行參數(shù)注入
這篇文章主要介紹了使用@PropertySource讀取配置文件通過@Value進行參數(shù)注入,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03服務性能優(yōu)化之mybatis-plus開啟與關(guān)閉SQL日志打印方法
這篇文章主要介紹了在Mybatis-plus中開啟和關(guān)閉控制臺SQL日志打印,在`application.properties`文件中,可以通過配置來實現(xiàn)SQL日志的開啟和關(guān)閉,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-12-12