一、如何用java将pdf文件转换成word文件
需要用到插件jacob,自己去下载吧。
import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class D2P {private ActiveXComponent wordCom = null;private Object wordDoc = null;private final Variant False = new Variant(false);private final Variant True = new Variant(true);/** *//** *//** *//*** 打开word文档** @param filePath* word文档* @return 返回word文档对象*/public boolean openWord(String filePath) {//建立ActiveX部件wordCom = new ActiveXComponent("Word.Application");try {//返回wrdCom.Documents的DispatchDispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDocwordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,new Object[] { filePath }, new int[1]).toDispatch();return true;} catch (Exception ex) {ex.printStackTrace();}return false;}/** *//** *//** *//*** 关闭word文档*/public void closeWord() {//关闭word文件wordCom.invoke("Quit", new Variant[] {});}/** *//** *//** *//*** * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 *** @param sourceFilePath* 源文件路径 ** @param destinPSFilePath* 首先生成的PS文件路径 ** @param destinPDFFilePath* 生成PDF文件路径*/public void docToPDF(String sourceFilePath, String destinPSFilePath,String destinPDFFilePath) {if (!openWord(sourceFilePath)) {closeWord();return;}//建立Adobe Distiller的com对象ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1");try {//设置当前使用的打印机,我的Adobe Distiller打印机名字为"Adobe PDF"wordCom.setProperty("ActivePrinter", new Variant("MS Publisher Color Printer"));//设置printout的参数,将word文档打印为postscript文档。目前只使用了前5个参数,如果要使用更多的话可以参考MSDN的office开发相关api//是否在后台运行Variant Background = False;//是否追加打印Variant Append = False;//打印所有文档int wdPrintAllDocument = 0;Variant Range = new Variant(wdPrintAllDocument);//输出的postscript文件的路径Variant OutputFileName = new Variant(destinPSFilePath);Dispatch.callN((Dispatch) wordDoc, "PrintOut", new Variant[] {Background, Append, Range, OutputFileName });System.out.println("由word文档打印为ps文档成功!");//调用Distiller对象的FileToPDF方法所用的参数,详细内容参考Distiller Api手册//作为输入的ps文档路径Variant inputPostScriptFilePath = new Variant(destinPSFilePath);//作为输出的pdf文档的路径Variant outputPDFFilePath = new Variant(destinPDFFilePath);//定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件Variant PDFOption = new Variant("");//调用FileToPDF方法将ps文档转换为pdf文档Dispatch.callN(distiller, "FileToPDF", new Variant[] {inputPostScriptFilePath, outputPDFFilePath, PDFOption });System.out.println("由ps文档转换为pdf文档成功!");} catch (Exception ex) {ex.printStackTrace();} finally {closeWord();wordCom=null;//释放在程序线程中引用的其它com,比如Adobe PDFDistillerComThread.Release();}}public static void main(String[] argv) {D2P d2p = new D2P();d2p.docToPDF("d:/12.doc", "d:/1p.ps", "d:/1p.pdf");//这里是你建一个叫12.doc的word文档,生成的文档将在D盘下//1p.ps和1p.pdf(这是我们要的)}}。
二、如何用纯java代码实现word转pdf
几种方案:方法一:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。
方法2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便地址:需要注意中文字体的写入问题。方法3:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。
jodconverter的下载地址:首先要安装openOffice,下载地址:安装完后要启动openOffice的服务,具体启动方法请自行google方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件SaveAsPDFandXPS下载地址:jacob 包下载地址:。
三、java 怎么把pdf转成word
可以用PDFBox
至于生成word,用POI;HTML的话,自己解析就可以了
PDFBox是一个开源的可以操作PDF文档的Java PDF类库。它可以创建一个新PDF文档,操作现有PDF文档并提取文档中的内容。
它具有以下特性:
1.将一个PDF文档转换输出为一个文本文件。
2.可以从文本文件创建一个PDF文档。
3.加密/解密PDF文档。
4.向已有PDF文档中追加内容。
5.可以从PDF文档生成一张图片。
6.可以与Jakarta Lucene搜索引擎的整合
四、java 有关word,excel,pdf转换成html 有几种方式
java将Word/Excel/PDF文件转换成HTML整理 项目开发过程中,需求涉及到了各种文档转换为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”】 新建类:1public class JacobUtil 2{ 3 public static final int WORD_HTML = 8; 4 5 public static final int WORD_TXT = 7; 6 7 public static final int EXCEL_HTML = 44; 8 9 /** *//** 10 * WORD转HTML 11 * @param docfile WORD文件全路径 12 * @param htmlfile 转换后HTML存放路径 13 */ 14 public static void wordToHtml(String docfile, String htmlfile) 15 { 16 ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word 17 try 18 { 19 app.setProperty("Visible", new Variant(false)); 20 Dispatch docs = app.getProperty("Documents").toDispatch(); 21 Dispatch doc = Dispatch.invoke( 22 docs, 23 "Open", 24 Dispatch.Method, 25 new Object[] { docfile, new Variant(false), 26 new Variant(true) }, new int[1]).toDispatch(); 27 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { 28 htmlfile, new Variant(WORD_HTML) }, new int[1]); 29 Variant f = new Variant(false); 30 Dispatch.call(doc, "Close", f); 31 } 32 catch (Exception e) 33 { 34 e.printStackTrace(); 35 } 36 finally 37 { 38 app.invoke("Quit", new Variant[] {}); 39 } 40 } 41 42 /** *//** 43 * EXCEL转HTML 44 * @param xlsfile EXCEL文件全路径 45 * @param htmlfile 转换后HTML存放路径 46 */ 47 public static void excelToHtml(String xlsfile, String htmlfile) 48 { 49 ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word 50 try 51 { 52 app.setProperty("Visible", new Variant(false)); 53 Dispatch excels = app.getProperty("Workbooks").toDispatch(); 54 Dispatch excel = Dispatch.invoke( 55 excels, 56 "Open", 57 Dispatch.Method, 58 new Object[] { xlsfile, new Variant(false), 59 new Variant(true) }, new int[1]).toDispatch(); 60 Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] { 61 htmlfile, new Variant(EXCEL_HTML) }, new int[1]); 62 Variant f = new Variant(false); 63 Dispatch.call(excel, "Close", f); 64 } 65 catch (Exception e) 66 { 67 e.printStackTrace(); 68 } 69 finally 70 { 71 app.invoke("Quit", new Variant[] {}); 72 } 73 } 74 75} 76 当时我在找转换控件时,发现网易也转载了一偏关于Jacob使用帮助,但其中出现了比较严重的错误:String htmlfile = "C:\\AA"; 只指定到了文件夹一级,正确写法是String htmlfile = "C:\\AA\\xxx.html"; 到此WORD/EXCEL转换HTML就已经差不多了,相信大家应该很清楚了:) 二、使用XPDF将PDF转换为HTML1、下载xpdf最新版本,地址:http://www.foolabs.com/xpdf/download.html 我下载的是xpdf-3.02pl2-win32.zip2、下载中文支持包 我下载的是xpdf-chinese-simplified.tar.gz3、下载pdftohtml支持包 地址:http://sourceforge.net/projects/pdftohtml/ 我下载的是:pdftohtml-0.39-win32.tar.gz4、解压调试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代码 #----- begin Chinese Simplified support package cidToUnicode Adobe-GB1 xpdf-chinese-simplified\Adobe-GB1.cidToUnicode unicodeMap ISO-2022-CN xpdf-chinese-simplified\ISO-2022-CN.unicodeMap unicodeMap EUC-CN xpdf-chinese-simplified\EUC-CN.unicodeMap unicodeMap GBK xpdf-chinese-simplified\GBK.unicodeMap cMapDir Adobe-GB1 xpdf-chinese-simplified\CMap toUnicodeDir xpdf-chinese-simplified\CMap fontDir C:\WINDOWS\Fonts displayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf #----- end Chinese Simplified support 。
五、格式转换如何将pdf格式转word格式
PDF转DOC软件SolidConverterPDF 下载地址: 相关介绍: 可以用ScanSoft PDF Converte,安装完成后不须任何设置,它会自动整合到Word中。
当我们在Word中点击“打开”菜单时,在“打开”对话框的“文件类型”下拉菜单中可以看到“PDF”选项,这就意味着我们可以用Word直接打开PDF文档了! ScanSoft PDF Converter的工作原理其实很简单,它先捕获PDF文档中的信息,分离文字、图片、表格和卷,再将它们统一成Word格式。 由于Word在打开 PDF文档时,会将PDF格式转换成DOC格式,因此打开速度会较一般的文件慢。
打开时会显示PDF Converter转换进度。转换完毕后可以看到,文档中的文字格式、版面设计保持了原汁原味,没有发生任何变化,表格和图片也完整地保存下来了,可以轻松进行编辑。
除了能够在Word中直接打开PDF文档外,右击PDF文档,在弹出菜单中选择“Open PDF in Word”命令也可打开该文件。另外,它还会在Outlook中加入一个工具按钮,如果收到的电子邮件附件中有PDF文档,就可以直接点击该按钮将它转换成Word文件。
有时我们在网上搜索到PDF格式的文件,同样可以通过右键菜单的相关命令直接在Word中打开它。 。
六、如何用纯java代码实现word转pdf
几种方案:
方法一:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。
方法2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便
地址:http://www.maxstocker.com/jdoctopdf/downloads.php
需要注意中文字体的写入问题。
方法3:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。
jodconverter的下载地址:http://www.artofsolving.com/opensource/jodconverter
首先要安装openOffice,下载地址:http://www.openoffice.org/download/index.html
安装完后要启动openOffice的服务,具体启动方法请自行google
方法4:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf)
Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件
SaveAsPDFandXPS下载地址:http://www.microsoft.com/zh-cn/download/details.aspx?id=7
jacob 包下载地址:http://sourceforge.net/projects/jacob-project/
七、java代码doc转pdf提高效率的方法
使用了jacob.jar来调用activex控件,本机需安装WPS或pdfcreator001 package experiments; 002 003 import com.jacob.activeX.ActiveXComponent; 004 import com.jacob.com.Dispatch; 005 import com.jacob.com.DispatchEvents; 006 import com.jacob.com.Variant; 007 import java.io.File; 008 import java.util.logging.Level; 009 import java.util.logging.Logger; 010 011 public class Doc2Pdf { 012 013 public static Converter newConverter(String name) { 014 if (name.equals("wps")) { 015 return new Wps(); 016 } else if (name.equals("pdfcreator")) { 017 return new PdfCreator(); 018 } 019 return null; 020 } 021 022 public synchronized static boolean convert(String word, String pdf) { 023 return newConverter("pdfcreator").convert(word, pdf); 024 } 025 026 public static interface Converter { 027 028 public boolean convert(String word, String pdf); 029 } 030 031 public static class Wps implements Converter { 032 033 public synchronized boolean convert(String word, String pdf) { 034 File pdfFile = new File(pdf); 035 File wordFile = new File(word); 036 ActiveXComponent wps = null; 037 try { 038 wps = new ActiveXComponent("wps.application"); 039 ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", newVariant(wordFile.getAbsolutePath())); 040 doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath())); 041 doc.invoke("Close"); 042 doc.safeRelease(); 043 } catch (Exception ex) { 044 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex); 045 return false; 046 } catch (Error ex) { 047 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex); 048 return false; 049 } finally { 050 if (wps != null) { 051 wps.invoke("Terminate"); 052 wps.safeRelease(); 053 } 054 } 055 return true; 056 } 057 } 058 059 public static class PdfCreator implements Converter { 060 061 public static final int STATUS_IN_PROGRESS = 2; 062 public static final int STATUS_WITH_ERRORS = 1; 063 public static final int STATUS_READY = 0; 064 private ActiveXComponent pdfCreator; 065 private DispatchEvents dispatcher; 066 private volatile int status; 067 private Variant defaultPrinter; 068 069 private void init() { 070 pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator"); 071 dispatcher = new DispatchEvents(pdfCreator, this); 072 pdfCreator.setProperty("cVisible", new Variant(false)); 073 pdfCreator.invoke("cStart", new Variant[]{newVariant("/NoProcessingAtStartup"), new Variant(true)}); 074 setCOption("UseAutosave", 1); 075 setCOption("UseAutosaveDirectory", 1); 076 setCOption("AutosaveFormat", 0); // 0 = PDF 077 defaultPrinter = pdfCreator.getProperty("cDefaultPrinter"); 078 status = STATUS_IN_PROGRESS; 079 pdfCreator.setProperty("cDefaultprinter", "PDFCreator"); 080 pdfCreator.invoke("cClearCache"); 081 pdfCreator.setProperty("cPrinterStop", false); 082 } 083 084 private void setCOption(String property, Object value) { 085 Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]); 086 } 087 088 private void close() { 089 if (pdfCreator != null) { 090 pdfCreator.setProperty("cDefaultprinter", defaultPrinter); 091 pdfCreator.invoke("cClearCache"); 092 pdfCreator.setProperty("cPrinterStop", true); 093 pdfCreator.invoke("cClose"); 094 pdfCreator.safeRelease(); 095 pdfCreator = null; 096 } 097 if (dispatcher != null) { 098 dispatcher.safeRelease(); 099 dispatcher = null; 100 } 101 } 102 103 public synchronized boolean convert(String word, String pdf) { 104 File pdfFile = new File(pdf); 105 File wordFile = new File(word); 106 try { 107 init(); 108 setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath()); 109 if (pdfFile.exists()) { 110 pdfFile.delete(); 111 } 112 pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath()); 113 int seconds = 0; 114 while (isInProcess()) { 115 seconds++; 116 if (seconds > 30) { // timeout 117 throw new Exception("convertion timeout!"); 118 } 119 Thread.sleep(1000); 120 } 121 if (isWithErrors()) return false; 122 // 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名) 123 // 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名 124 File outputFile = newFile(pdfCreator.getPropertyAsString("cOutputFilename")); 125 if (outputFile.exists()) { 126 outputFile.renameTo(pdfFile); 127 } 128 } catch (InterruptedException ex) { 129 Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex); 130 return false; 。
转载请注明出处51数据库 » pdf转wordjava代码
未婚夫的未婚妻13494116