pdf 转html java_java将Word/Excel/PDF文件转换成HTML整理

本文介绍了如何使用Jacob库将Word和Excel转换为HTML,以及利用XPDF将PDF转换为HTML,包括详细步骤和注意事项,适用于项目中文档转换的需求。
摘要由CSDN通过智能技术生成

项目开发过程中,需求涉及到了各种文档转换为HTML或者网页易显示格式,现在将实现方式整理如下:

一、使用Jacob转换Word,Excel为HTML

“JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32 libraries。”

首先下载Jacob包,JDK1.5以上需要使用Jacob1.9版本(JDK1.6尚未测试),与先前的Jacob1.7差别不大

1、将压缩包解压后,Jacob.jar添加到Libraries中;

2、将Jacob.dll放至“WINDOWS\SYSTEM32”下面。

需要注意的是:

【使用IDE启动Web服务器时,系统读取不到Jacob.dll,例如用MyEclipse启动Tomcat,就需要将dll文件copy到MyEclipse安装目录的“jre\bin”下面。

一般系统没有加载到Jacob.dll文件时,报错信息为:“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】

新建类:

1

82441051_1.gifpublic class JacobUtil   

2

82441051_2.gif{   

3

82441051_3.gif    public static final int WORD_HTML = 8;   

4

82441051_3.gif  

5

82441051_3.gif    public static final int WORD_TXT = 7;   

6

82441051_3.gif  

7

82441051_3.gif    public static final int EXCEL_HTML = 44;   

8

82441051_3.gif  

9

82441051_4.gif    /**1082441051_3.gif     * WORD转HTML1182441051_3.gif     *@paramdocfile WORD文件全路径1282441051_3.gif     *@paramhtmlfile 转换后HTML存放路径1382441051_5.gif*/  

14

82441051_3.gif    public static void wordToHtml(String docfile, String htmlfile)   

15

82441051_4.gif    {   

16

82441051_3.gif        ActiveXComponent app = new ActiveXComponent("Word.Application"); //启动word1782441051_3.gif        try  

18

82441051_4.gif        {   

19

82441051_3.gif            app.setProperty("Visible", new Variant(false));   

20

82441051_3.gif            Dispatch docs = app.getProperty("Documents").toDispatch();   

21

82441051_3.gif            Dispatch doc = Dispatch.invoke(   

22

82441051_3.gif                    docs,   

23

82441051_3.gif                    "Open",   

24

82441051_3.gif                    Dispatch.Method,   

25

82441051_4.gif                    new Object[] { docfile, new Variant(false),   

26

82441051_5.gif                            new Variant(true) }, new int[1]).toDispatch();   

27

82441051_4.gif            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {   

28

82441051_5.gif                    htmlfile, new Variant(WORD_HTML) }, new int[1]);   

29

82441051_3.gif            Variant f = new Variant(false);   

30

82441051_3.gif            Dispatch.call(doc, "Close", f);   

31

82441051_5.gif        }   

32

82441051_3.gif        catch (Exception e)   

33

82441051_4.gif        {   

34

82441051_3.gif            e.printStackTrace();   

35

82441051_5.gif        }   

36

82441051_3.gif        finally  

37

82441051_4.gif        {   

38

82441051_4.gif            app.invoke("Quit", new Variant[] {});   

39

82441051_5.gif        }   

40

82441051_5.gif    }   

41

82441051_3.gif  

42

82441051_4.gif    /**4382441051_3.gif     * EXCEL转HTML4482441051_3.gif     *@paramxlsfile EXCEL文件全路径4582441051_3.gif     *@paramhtmlfile 转换后HTML存放路径4682441051_5.gif*/  

47

82441051_3.gif    public static void excelToHtml(String xlsfile, String htmlfile)   

48

82441051_4.gif    {   

49

82441051_3.gif        ActiveXComponent app = new ActiveXComponent("Excel.Application"); //启动word5082441051_3.gif        try  

51

82441051_4.gif        {   

52

82441051_3.gif            app.setProperty("Visible", new Variant(false));   

53

82441051_3.gif            Dispatch excels = app.getProperty("Workbooks").toDispatch();   

54

82441051_3.gif            Dispatch excel = Dispatch.invoke(   

55

82441051_3.gif                    excels,   

56

82441051_3.gif                    "Open",   

57

82441051_3.gif                    Dispatch.Method,   

58

82441051_4.gif                    new Object[] { xlsfile, new Variant(false),   

59

82441051_5.gif                            new Variant(true) }, new int[1]).toDispatch();   

60

82441051_4.gif            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {   

61

82441051_5.gif                    htmlfile, new Variant(EXCEL_HTML) }, new int[1]);   

62

82441051_3.gif            Variant f = new Variant(false);   

63

82441051_3.gif            Dispatch.call(excel, "Close", f);   

64

82441051_5.gif        }   

65

82441051_3.gif        catch (Exception e)   

66

82441051_4.gif        {   

67

82441051_3.gif            e.printStackTrace();   

68

82441051_5.gif        }   

69

82441051_3.gif        finally  

70

82441051_4.gif        {   

71

82441051_4.gif            app.invoke("Quit", new Variant[] {});   

72

82441051_5.gif        }   

73

82441051_5.gif    }   

74

82441051_3.gif  

75

82441051_6.gif}  

76

82441051_1.gif

当时我在找转换控件时,发现网易也转载了一偏关于Jacob使用帮助,但其中出现了比较严重的错误:String htmlfile = "C:\\AA";

只指定到了文件夹一级,正确写法是String htmlfile = "C:\\AA\\xxx.html";

到此WORD/EXCEL转换HTML就已经差不多了,相信大家应该很清楚了:)

二、使用XPDF将PDF转换为HTML

2、下载中文支持包

3、下载pdftohtml支持包

4、解压调试

1) 先将xpdf-3.02pl2-win32.zip解压,解压后的内容可根据需要进行删减,如果只需要转换为txt格式,其他的exe文件可以删除,只保留pdftotext.exe,以此类推;

2) 然后将xpdf-chinese-simplified.tar.gz解压到刚才xpdf-3.02pl2-win32.zip的解压目录;

3) 将pdftohtml-0.39-win32.tar.gz解压,pdftohtml.exe解压到xpdf-3.02pl2-win32.zip的解压目录;

4) 目录结构:

+---[X:\xpdf]

|-------各种转换用到的exe文件

|

|-------xpdfrc

|

+------[X:\xpdf\xpdf-chinese-simplified]

|

|

+-------很多转换时需要用到的字符文件

xpdfrc:此文件是用来声明转换字符集对应路径的文件

5) 修改xpdfrc文件(文件原名为sample-xpdfrc)

修改文件内容为:Txt代码

d7d454600f77758df47d870acbf00b9c.png 

7a1e7afc0f2addbbdb746966b60e9e4a.png

82441051_1.gif#----- begin Chinese Simplified support package   

82441051_1.gifcidToUnicode    Adobe-GB1       xpdf-chinese-simplified\Adobe-GB1.cidToUnicode   

82441051_1.gifunicodeMap      ISO-2022-CN     xpdf-chinese-simplified\ISO-2022-CN.unicodeMap   

82441051_1.gifunicodeMap      EUC-CN          xpdf-chinese-simplified\EUC-CN.unicodeMap   

82441051_1.gifunicodeMap  GBK    xpdf-chinese-simplified\GBK.unicodeMap   

82441051_1.gifcMapDir         Adobe-GB1       xpdf-chinese-simplified\CMap   

82441051_1.giftoUnicodeDir                    xpdf-chinese-simplified\CMap   

82441051_1.giffontDir  C:\WINDOWS\Fonts   

82441051_1.gifdisplayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf   

82441051_1.gif#----- end Chinese Simplified support package  

82441051_1.gif

6) 创建bat文件pdftohtml.bat(放置的路径不能包含空格)

内容为:Txt代码

d7d454600f77758df47d870acbf00b9c.png 

7a1e7afc0f2addbbdb746966b60e9e4a.png

82441051_1.gif@echo off   

82441051_1.gifset folderPath=%1  

82441051_1.gifset filePath=%2  

82441051_1.gifcd /d %folderPath%   

82441051_1.gifpdftohtml -enc GBK %filePath%   

82441051_1.gifexit7) 创建类

JAVA代码

82441051_1.gifpublic class ConvertPdf   

82441051_2.gif{   

82441051_3.gif    private static String INPUT_PATH;   

82441051_3.gif    private static String PROJECT_PATH;   

82441051_3.gif       

82441051_3.gif    public static void convertToHtml(String file, String project)   

82441051_4.gif    {   

82441051_3.gif        INPUT_PATH = file;   

82441051_3.gif        PROJECT_PATH = project;   

82441051_3.gif        if(checkContentType()==0)   

82441051_4.gif        {   

82441051_3.gif            toHtml();   

82441051_5.gif        }   

82441051_5.gif    }   

82441051_3.gif       

82441051_3.gif    private static int checkContentType()   

82441051_4.gif    {   

82441051_3.gif        String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(".") + 1, INPUT_PATH.length())   

82441051_3.gif                .toLowerCase();   

82441051_3.gif        if (type.equals("pdf"))   

82441051_3.gif            return 0;   

82441051_3.gif        else  

82441051_3.gif            return 9;   

82441051_5.gif    }   

82441051_3.gif       

82441051_3.gif    private static void toHtml()   

82441051_4.gif    {   

82441051_3.gif        if(new File(INPUT_PATH).isFile())   

82441051_4.gif        {   

82441051_3.gif            try  

82441051_4.gif            {   

82441051_3.gif                String cmd = "cmd /c start X:\\pdftohtml.bat \"" + PROJECT_PATH + "\" \"" + INPUT_PATH + "\"";   

82441051_3.gif                Runtime.getRuntime().exec(cmd);   

82441051_5.gif            }   

82441051_3.gif            catch (IOException e)   

82441051_4.gif            {   

82441051_3.gif                e.printStackTrace();   

82441051_5.gif            }   

82441051_5.gif        }   

82441051_5.gif    }   

82441051_3.gif       

82441051_6.gif}  

82441051_1.gif

String cmd = "....";此处代码是调用创建的bat文件进行转换

8) 测试转换

JAVA代码

d7d454600f77758df47d870acbf00b9c.png 

7a1e7afc0f2addbbdb746966b60e9e4a.png

82441051_1.gifpublic static void main(String[] args)   

82441051_2.gif{   

82441051_3.gif    ConvertPdf.convertToHtml("C:\\test.pdf", "X:\\xpdf");   

82441051_6.gif}  

82441051_1.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值