如何用纯java代码实现word转pdf
几种方案: 方法一:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。
方法贰:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便 地址:中国至美.maxstocker中国/jdoctopdf/downloads.php 需要注意中文字体的写入问题。
方法三:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法依一样都是跨平台的。
jodconverter的下载地址:中国至美.artofsolving中国/opensource/jodconverter 首先要安装openOffice,下载地址:中国至美.openoffice.org/download/index.html 安装完后要启动openOffice的服务,具体启动方法请自行google 方法四:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf) Office版本是贰00漆,因为SaveAsPDFandXPS是微软为office贰00漆及以上版本开发的插件 SaveAsPDFandXPS下载地址:中国至美.microsoft中国/zh-cn/download/details.aspx?id=漆 jacob 包下载地址:中国sourceforge.net/projects/jacob-project
Java如何操作Word,Excel,PDF文档?
Java Excel API 文档 http://www.andykhan.com/jexcelapi/ 1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有 import java.io.File; import com.jacob.com.*; import com.jacob.activeX.*; public class WordTest { public static void main(String[] args) { WordBean word=new WordBean(); word.openWord(true); word.createNewDocument(); word.insertText("Hello word."); } } import com.jacob.activeX.*; import com.jacob.com.*; public class WordBean extends java.awt.Panel { private ActiveXComponent MsWordApp = null; private Dispatch document = null; public WordBean() { super(); } public void openWord(boolean makeVisible) { //Open Word if we've not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word.Application"); } //Set the visible property as required. Dispatch.put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch.get(MsWordApp,"Documents").toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch.call(documents,"Add").toDispatch(); } public void insertText(String textToInsert) { // Get the current selection within Word at the moment. If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch.get(MsWordApp,"Selection").toDispatch(); //Put the specified text at the insertion point Dispatch.put(selection,"Text",textToInsert); } public void saveFileAs(String filename) { Dispatch.call(document,"SaveAs",filename); } public void printFile() { //Just print the current document to the default printer Dispatch.call(document,"PrintOut"); } public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch.call(document, "Close", new Variant(0)); document = null; } public void closeWord() { Dispatch.call(MsWordApp,"Quit"); MsWordApp = null; document = null; } }
java实现word文档自动转成pdf文档我现在在做一个网站,客
没自动的..你可以把软件安装好了点文件-导入\导出 来转换成PDF文件 也可以用WPS, 这是我自己下载了在用的,很好用的版本: Office.2003.SP2.龙卷风大企业版 V2.2 2.2 658M ISO 此ISO整合SP2升级包,免激活,免序列号,支持在线更新 包含下列组件: Microsoft Office Word 2003 Microsoft Office Excel 2003 Microsoft Office PowerPoint 2003 Microsoft Office FrontPage 2003 Microsoft Office Access 2003 Microsoft Office Outlook 2003 Microsoft Office OneNote 2003 Microsoft Office Visio 2003 Microsoft Office InfoPath 2003 Microsoft Office Publisher 2003 Microsoft Office Project 2003 安装说明:先卸载你原来的版本,然后全新安装此版即可
Java如何将Word文件转成PDF文件
用Java内嵌iText生成PDF文档需要5个步骤:①建立com.lowagie.text.Document对象的实例。
Document document = new Document();②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));③打开文档。
document.open();④向文档中添加内容。
document.add(new Paragraph("Hello World"));⑤关闭文档。
document.close();通过上面的5个步骤,就能产生一个Helloworld.PDF的文件,文件内容为"Hello World"。
JAVA读取WORD,EXCEL,PDF文件的方法是什么呢?
JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法 OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。
水平有限,万望各位指正 WORD: import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.poi.hwpf.extractor.WordExtractor; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import com.search.code.Index; public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException { String bodyText = null; try { WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream bodyText = ex.getText(); if(!bodyText.equals("")){ index.AddIndex(url, title, bodyText); } }catch (DocCenterException e) { throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e); }catch(Exception e){ e.printStackTrace(); } } return null; } Excel: import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFCell; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import com.search.code.Index; public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException { StringBuffer content = new StringBuffer(); try{
如何用纯java代码实现word转pdf?
1:用apache pio 读取doc文件,然后转成html文件用Jsoup格式化html文件,最后用itext将html文件转成pdf。
2:使用jdoctopdf来实现,这是一个封装好的包,可以把doc转换成pdf,html,xml等格式,调用很方便。
3:地址http://www.maxstocker.com/jdoctopdf/downloads.php 需要注意中文字体的写入问题。
4:使用jodconverter来调用openOffice的服务来转换,openOffice有个各个平台的版本,所以这种方法跟方法1一样都是跨平台的。
jodconverter的下载地址:http://www.artofsolving.com/opensource/jodconverter 首先要安装openOffice,下载地址:office.org/download/index.html" target="_blank">http://www.openoffice.org/download/index.html 5:安装完后要启动openOffice的服务,具体启动方法请自行google。
6:效果最好的一种方法,但是需要window环境,而且速度是最慢的需要安装msofficeWord以及SaveAsPDFandXPS.exe(word的一个插件,用来把word转化为pdf) 7:Office版本是2007,因为SaveAsPDFandXPS是微软为office2007及以上版本开发的插件。
8:SaveAsPDFandXPS下载地址:microsoft.com/zh-cn/download/details.aspx?id=7" target="_blank">http://www.microsoft.com/zh-cn/download/details.aspx?id=7。
9:需要转换的工具 ,看你是linux还是word 。
word还好不需要安装。
linux就麻烦了。
爪哇是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。
Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。
Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。
java中怎么将word转pdf
/ connect to an OpenOffice.org instance running on port 8100OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1".disconnect();return 0;Program Files (x86)\, .org/** ** @param sourceFile* 源文件;OpenOffice 4\program执行soffice -headless -accept="\office\ 如果目标路径不存在, 则新建该路径File outputFile = new File(destFile);if (;}return 1.pdf";8100", destFile);* http://www.openoffice.0.xlsx, .ppt, .pptx等. 示例: F;/ǘ. 需要用的软件OpenOffice 下载地址http://www.openoffice!inputFile, 则表示转换失败*/}/, 8100);* String destFile = "pid值"3;pdf\. 运行该函数需要用到OpenOffice, OpenOffice下载地址为* http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载2.启动OpenOffice的服务安装完openoffice.将JodConverter相关的jar包添加到项目中 4. 下面是实现代码附件里面有现成的可以用的项目示例 展开
如何用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的Dispatch Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();//调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc wordDoc = 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 PDFDistiller ComThread.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(这是我们要的) } }
在linux环境下,java怎么实现从word格式转换为pdf格式
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; /** * @author XuMing Li * * @version 1.00, 2007-4-9 * */ 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的Dispatch Dispatch wrdDocs = wordCom.getProperty( "Documents ").toDispatch(); //调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc wordDoc = 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( "Adobe PDF ")); //设置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(); } } public static void main(String[] argv) { D2P d2p = new D2P(); // d2p.openWord( "c:/12.doc "); // d2p.callWordMacro( "c:/12.docc ", "MyWordMacro ", // new String[] { "这是调用word宏的测试程序 " }); d2p.docToPDF( "d:/12.doc ", "c:/1p.ps ", "c:/1p.pdf "); } }
转载请注明出处51数据库 » java word流转pdf
处破了