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

Java實現word/pdf轉html并在線預覽

 更新時間:2023年05月30日 10:51:00   作者:知北游z  
這篇文章主要為大家詳細介紹了如何利用Java語言實現word、pdf文件轉html并在線預覽的功能,文中的示例代碼講解詳細,需要的可以參考一下

實現文檔在線預覽的方式除了上篇文章《文檔在線預覽(一)通過將txt、word、pdf轉成圖片實現在線預覽功能》說的將文檔轉成圖片的實現方式外,還有轉成pdf,前端通過pdf.js、pdfobject.js等插件來實現在線預覽,以及本文將要說到的將文檔轉成html的方式來實現在線預覽。代碼基于 aspose-words(用于word轉html),pdfbox(用于pdf轉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>

一、將文件轉換成html字符串

1、將word文件轉成html字符串

public static String wordToHtmlStr(String wordPath) {
        try {
            Document doc = new Document(wordPath); // Address是將要被轉化的word文檔
            String htmlStr = doc.toString();
            return htmlStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

驗證結果:

2、將pdf文件轉成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();
    }

驗證結果:

二、將文件轉換成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>

相關代碼:

String htmlStr = FileConvertUtil.pdfToHtmlStr("D:\\書籍\\電子書\\小說\\歷史小說\\最后的可汗.doc");
FileUtils.write(new File("D:\\test\\doc.html"), htmlStr, "utf-8");

除此之外,還可以對上面的代碼進行一些調整,已實現生成html文件,代碼調整如下:

2、將word文件轉換成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是將要被轉化的word文檔
            HtmlSaveOptions options = new HtmlSaveOptions();
            options.setExportImagesAsBase64(true);
            options.setExportRelativeFontSize(true);
            doc.save(os, options);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

驗證結果:

3、將pdf文件轉換成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文件驗證結果:

文字版PDF文件驗證結果:

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

相關文章

最新評論

林芝县| 临城县| 商河县| 乐安县| 依安县| 昌宁县| 乳源| 新绛县| 安图县| 乌拉特前旗| 武川县| 遂平县| 孟连| 上虞市| 东阿县| 收藏| 宁海县| 梅州市| 章丘市| 项城市| 湟源县| 成都市| 揭西县| 屏边| 宁南县| 梧州市| 锦屏县| 西藏| 昌都县| 类乌齐县| 台北县| 东乡族自治县| 呼和浩特市| 苏尼特右旗| 大悟县| 海门市| 景泰县| 新营市| 通江县| 镇宁| 稻城县|