jacob excel转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程序在已经存在的word文档中插入图片并加密保存为pdf格式的文档...
我曾经也尝试使用纯java技术去解析word文档,并且使用了apache的jacob,POI等项目,但是由于Microsoft Word使用的doc不是标准DOC文件,而是自己加处理过的,所以现在解析微软的doc都只能靠破解与猜解,据我所知,现在的技术只能从word中提取出来文字,所以使用纯java不太可能实现。
如果使用windows平台的话,可以选择使用微软的一些word控件来达到目的.
JACOB类使用问题
public class Jacob { /** * 打开文件 * * @param documents * @param inputDocPath * @return */ private Dispatch open(Dispatch documents, String inputDocPath) { return Dispatch.call(documents, "Open", inputDocPath).toDispatch(); } /** * 选定内容 * * @param word * @return */ private Dispatch select(ActiveXComponent word) { return word.getProperty("Selection").toDispatch(); } /** * 把插入点移动到文件首位置 * * @param selection */ private void moveStart(Dispatch selection) { Dispatch.call(selection, "HomeKey", new Variant(6)); } /** * 从选定内容或插入点开始查找文本 * * @param selection * 选定内容 * @param toFindText * 要查找的文本 * @return true:查找到并选中该文本;false:未查找到文本。
*/ private boolean find(Dispatch selection, String toFindText) { // 从selection所在位置开始查询 Dispatch find = Dispatch.call(selection, "Find").toDispatch(); // 设置要查找的内容 Dispatch.put(find, "Text", toFindText); // 向前查找 Dispatch.put(find, "Forward", "True"); // 设置格式 Dispatch.put(find, "format", "True"); // 大小写匹配 Dispatch.put(find, "MatchCase", "True"); // 全字匹配 Dispatch.put(find, "MatchWholeWord", "True"); // 查找并选中 return Dispatch.call(find, "Execute").getBoolean(); } /** * 把选定内容替换为设定文本 * * @param selection * @param newText */ private void replace(Dispatch selection, String newText) { Dispatch.put(selection, "Text", newText); } /** * 全局替换 * * @param selection * @param oldText * @param replaceObj */ private void replaceAll(Dispatch selection, String oldText, Object replaceObj) { moveStart(selection); String newText = (String) replaceObj; while (find(selection, oldText)) { replace(selection, newText); Dispatch.call(selection, "MoveRight"); } } /** * 打印 * * @param document */ private void print(Dispatch document) { Dispatch.call(document, "PrintOut"); } /** * 保存文件 * * @param word * @param outputPath */ private void save(ActiveXComponent word, String outputPath) { Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(), "FileSaveAs", outputPath); } /** * 关闭文件 * * @param doc */ private void close(Dispatch doc) { Dispatch.call(doc, "Close", new Variant(true)); } /** * 保存打印doc文档 * * @param inputDocPath * @param outPutDocPath * @param data * @param isPrint */ public void saveDoc(String inputDocPath, String outPutDocPath, HashMap data, boolean isPrint) { // 初始化com的线程 ComThread.InitSTA(); // word运行程序对象 ActiveXComponent word = new ActiveXComponent("Word.Application"); // 文档对象 Dispatch wordObject = (Dispatch) word.getObject(); // 设置属性 Variant(true)表示word应用程序可见 Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false)); // word所有文档 Dispatch documents = word.getProperty("Documents").toDispatch(); // 打开文档 Dispatch document = this.open(documents, inputDocPath); Dispatch selection = this.select(word); Iterator keys = data.keySet().iterator(); String oldText; Object newValue; while (keys.hasNext()) { oldText = (String) keys.next(); newValue = data.get(oldText); this.replaceAll(selection, oldText, newValue); } // 是否打印 if (isPrint) { this.print(document); } this.save(word, outPutDocPath); this.close(document); word.invoke("Quit", new Variant[0]); // 关闭com的线程 ComThread.Release(); } }
在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 "); } }
如何使用WPS把Word文档转换为PDF文档并生成目录
单击左上角的”WPS文档“,然后把鼠标放在”文件另存(A)“上,不要单击哦,再然后单击弹出的”输出为PDF格式(F)“。
如下图所示,紧接上个步骤,软件会弹出”输出 Adobe PDF 文件“选项卡,没有特别要求默认设置就可以,设置完之后点击”确定“按钮。
稍等片刻就可生成PDF格式的文档了。
下面用PDF阅读软件看下生成的PDF格式的文档,效果如下图所示(特别注意生成的目录):下面讨论一种特殊情况,那就是把正文中的一些语句生产PDF目录。
还是用上面的文档,但我把所有内容样式设为“正文”。
为了把内容为的目录一到四生产目录但样式有不设为标题样式,新建一种基于正文的样式并命名为”样式1“,然后把正文要生产目录的语句样式设为“样式1”。
在生产PDF的操作中,其中上面的第三步,软件弹出”输出 Adobe PDF 文件“选项卡要多勾一个勾。
http://jingyan.baidu.com/article/636f38bb255a9fd6b84610ed.html
如何在html网页中显示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内容
// 表格类型ListtableList = doc.getTables();for (int i = 0; i System.out.println(i);XWPFTable table = tableList.get(i);System.out.println(table.getText());}获取表格中内容可以用这个,但是你说的格式是什么意思,每个字的字体之类的吗?
转载请注明出处51数据库 » word pdf jacob