1. 如何用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(这是我们要的)}}。
2. 如何用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搜索引擎的整合
3. 如何用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(这是我们要的) } }。
4. java怎么输出pdf格式的文件
java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。
代码如下:public class createPdf { //自己做的一个简单例子,中间有图片之类的 //先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F); public void getPDFdemo() throws DocumentException, IOException{ //这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见: //首先 //字体的定义:这里用的是自带的jar里面的字体 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); // 当然你也可以用你电脑里面带的字体库 //BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //定义字体 注意在最新的包里面 颜色是封装的 Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54)); //生成pdf的第一个步骤: //保存本地指定路径 saveLocal(); document.open(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); // PdfWriter writer = PdfWriter.getInstance(document, ba); document.open(); //获取此编译的文件路径 String path = this.getClass().getClassLoader().getResource("").getPath(); //获取根路径 String filePath = path.substring(1, path.length()-15); //获取图片路径 找到你需要往pdf上生成的图片 //这里根据自己的获取的路径写 只要找到图片位置就可以 String picPath = filePath +"\\WebContent" +"\\images\\"; //往PDF中添加段落 Paragraph pHeader = new Paragraph(); pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1))); //pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 ); document.add(pHeader);//在文档中加入你写的内容 //获取图片 Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png"); //定义图片在文档中显示的绝对位置 img2.scaleAbsolute(137.0F, 140.0F); img2.setAbsolutePosition(330.0F, 37.0F); //将图片添加到文档中 document.add(img2); //关闭文档 document.close(); /*//设置文档保存的文件名 response.setHeader("Content-disposition", "attachment;filename=\""+ new String(("CCF会员资格确认函.pdf").getBytes("GBK"),"ISO-8859-1") + "\""); //设置类型 response.setContentType("application/pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush();*/ } public static void main(String[]args) throws DocumentException, IOException{ createPdf pdf= new createPdf(); pdf.getPDFdemo(); } //指定一个文件进行保存 这里吧文件保存到D盘的text.pdf public void saveLocal() throws IOException, DocumentException{ //直接生成PDF 制定生成到D盘test.pdf File file = new File("D:\\text2.pdf"); file.createNewFile(); PdfWriter.getInstance(document, new FileOutputStream(file)); } }。
5. 如何用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(这是我们要的) }}。
6. 如何用java实现导出PDF格式的文档
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; public class PdfTest { public static void main(String[] args) throws Exception { Document pdfDoc = new Document(); // 将要生成的 pdf 文件的路径输出流 FileOutputStream pdfFile = new FileOutputStream(new File("F:/study/test/firstPdf.pdf")); // pdf 文件中的一个文字段落 Paragraph paragraph = new Paragraph("My first PDF file with an image 。
"); Image image = Image.getInstance("F:/study/test/洛克 李.jpg"); // 用 Document 对象、File 对象获得 PdfWriter 输出流对象 PdfWriter.getInstance(pdfDoc, pdfFile); pdfDoc.open(); // 打开 Document 文档 // 添加一个文字段落、一张图片 pdfDoc.add(paragraph); pdfDoc.add(image); pdfDoc.close(); } }。
7. java怎么输出pdf格式的文件
java导出pdf需要用到iText库,iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。
代码如下:public class createPdf { //自己做的一个简单例子,中间有图片之类的 //先建立Document对象:相对应的 这个版本的jar引入的是com.lowagie.text.Document Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F); public void getPDFdemo() throws DocumentException, IOException{ //这个导出用的是 iTextAsian.jar 和iText-2.1.3.jar 属于比较老的方法。 具体下在地址见: //首先 //字体的定义:这里用的是自带的jar里面的字体 BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false); // 当然你也可以用你电脑里面带的字体库 //BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.EMBEDDED); //定义字体 注意在最新的包里面 颜色是封装的 Font fontChinese8 = new Font(bfChinese, 10.0F, 0, new Color(59, 54, 54)); //生成pdf的第一个步骤: //保存本地指定路径 saveLocal(); document.open(); ByteArrayOutputStream ba = new ByteArrayOutputStream(); // PdfWriter writer = PdfWriter.getInstance(document, ba); document.open(); //获取此编译的文件路径 String path = this.getClass().getClassLoader().getResource("").getPath(); //获取根路径 String filePath = path.substring(1, path.length()-15); //获取图片路径 找到你需要往pdf上生成的图片 //这里根据自己的获取的路径写 只要找到图片位置就可以 String picPath = filePath +"\\WebContent" +"\\images\\"; //往PDF中添加段落 Paragraph pHeader = new Paragraph(); pHeader.add(new Paragraph(" 你要生成文字写这里", new Font(bfChinese, 8.0F, 1))); //pHeader.add(new Paragraph("文字", 字体 可以自己写 也可以用fontChinese8 之前定义好的 ); document.add(pHeader);//在文档中加入你写的内容 //获取图片 Image img2 = Image.getInstance(picPath +"ccf-stamp-new.png"); //定义图片在文档中显示的绝对位置 img2.scaleAbsolute(137.0F, 140.0F); img2.setAbsolutePosition(330.0F, 37.0F); //将图片添加到文档中 document.add(img2); //关闭文档 document.close(); /*//设置文档保存的文件名 response.setHeader("Content-disposition", "attachment;filename=\""+ new String(("CCF会员资格确认函.pdf").getBytes("GBK"),"ISO-8859-1") + "\""); //设置类型 response.setContentType("application/pdf"); response.setContentLength(ba.size()); ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush();*/ } public static void main(String[]args) throws DocumentException, IOException{ createPdf pdf= new createPdf(); pdf.getPDFdemo(); } //指定一个文件进行保存 这里吧文件保存到D盘的text.pdf public void saveLocal() throws IOException, DocumentException{ //直接生成PDF 制定生成到D盘test.pdf File file = new File("D:\\text2.pdf"); file.createNewFile(); PdfWriter.getInstance(document, new FileOutputStream(file)); } }。
8. java如何根据word模板生成word文档
先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。将jacob.jar加入项目。
/** Java2word.java** Created on 2007年8月13日, 上午10:32** To change this template, choose Tools | Template Manager* and open the template in the editor.*//** 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。* word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……,以免执行错误的替换。
* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、.jpg、.gif)。* 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。
*/package com.word.util;/**** @author kdl*/import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class Java2word { private boolean saveOnExit;/** * word文档 */ Dispatch doc = null;/** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents;/** * 构造函数 */public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null)documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; }/** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch();} /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i="" ++)="" dispatch.call(selection,"moveup");}="" *="" *="" 把选定内容或插入点向下移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" movedown(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"movedown");}="" *="" *="" 把选定内容或插入点向左移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveleft(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" {="" dispatch.call(selection,"moveleft");}="" }="" *="" *="" 把选定内容或插入点向右移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */public="" void="" moveright(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)dispatch.call(selection,"moveright");="" }="" *="" *="" 把插入点移动到文件首位置="" *="" @param="" selection="" dispatch="" 插入点="" */="" public="" void="" movestart(dispatch="" selection)="" {dispatch.call(selection,"homekey",new="" variant(6));="" }/**="" *="" 从选定内容或插入点开始查找文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" tofindtext="" string="" 要查找的文本="" *="" @return="" boolean="" true-查找到并选中该文本,false-未查找到文本="" */="" public="" boolean="" find(dispatch="" selection,string="" tofindtext)="" {="" 从selection所在位置开始查询dispatch="" find="word.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="" dispatch="" 选定内容="" *="" @param="" newtext="" string="" 替换为文本="" */="" public="" void="">
9. java如何根据word模板生成word文档
先下载jacob_1.10.1.zip。
解压后将jacob.dll放到windows/system32下面或\j2sdk\bin下面。 将jacob.jar加入项目。
/* * Java2word.java * * Created on 2007年8月13日, 上午10:32 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /* * 传入数据为HashMap对象,对象中的Key代表word模板中要替换的字段,Value代表用来替换的值。 * word模板中所有要替换的字段(即HashMap中的Key)以特殊字符开头和结尾,如:$code$、$date$……, 以免执行错误的替换。
* 所有要替换为图片的字段,Key中需包含image或者Value为图片的全路径(目前只判断文件后缀名为:.bmp、.jpg、.gif)。 * 要替换表格中的数据时,HashMap中的Key格式为“table$R@N”,其中:R代表从表格的第R行开始替换,N代表 word模板中的第N张表格;Value为ArrayList对象,ArrayList中包含的对象统一为String[],一条String[]代 表一行数据,ArrayList中第一条记录为特殊记录,记录的是表格中要替换的列号,如:要替换第一列、第三列、第五列的数据,则第一条记录为String[3] {“1”,”3”,”5”}。
*/ package com.word.util; /** * * @author kdl */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Java2word { private boolean saveOnExit; /** * word文档 */ Dispatch doc = null; /** * word运行程序对象s */ private ActiveXComponent word; /** * 所有word文档 */ private Dispatch documents; /** * 构造函数 */ public Java2word() { if(word==null){ word = new ActiveXComponent("Word.Application"); word.setProperty("Visible",new Variant(false)); } if(documents==null) documents = word.getProperty("Documents").toDispatch(); saveOnExit = false; } /** * 设置参数:退出时是否保存 * @param saveOnExit boolean true-退出时保存文件,false-退出时不保存文件 */ public void setSaveOnExit(boolean saveOnExit) { this.saveOnExit = saveOnExit; } /** * 得到参数:退出时是否保存 * @return boolean true-退出时保存文件,false-退出时不保存文件 */ public boolean getSaveOnExit() { return saveOnExit; } /** * 打开文件 * @param inputDoc String 要打开的文件,全路径 * @return Dispatch 打开的文件 */ public Dispatch open(String inputDoc) { return Dispatch.call(documents,"Open",inputDoc).toDispatch(); //return Dispatch.invoke(documents,"Open",Dispatch.Method,new Object[]{inputDoc},new int[1]).toDispatch(); } /** * 选定内容 * @return Dispatch 选定的范围或插入点 */ public Dispatch select() { return word.getProperty("Selection").toDispatch(); } /** * 把选定内容或插入点向上移动 * @param selection Dispatch 要移动的内容 * @param count int 移动的距离 */ public void moveUp(Dispatch selection,int count) { for(int i = 0;i < count;i="" ++)="" dispatch.call(selection,"moveup");="" }="" *="" *="" 把选定内容或插入点向下移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" movedown(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"movedown");="" }="" *="" *="" 把选定内容或插入点向左移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveleft(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" {="" dispatch.call(selection,"moveleft");="" }="" }="" *="" *="" 把选定内容或插入点向右移动="" *="" @param="" selection="" dispatch="" 要移动的内容="" *="" @param="" count="" int="" 移动的距离="" */="" public="" void="" moveright(dispatch="" selection,int="" count)="" {="" for(int="" i="0;i">< count;i="" ++)="" dispatch.call(selection,"moveright");="" }="" *="" *="" 把插入点移动到文件首位置="" *="" @param="" selection="" dispatch="" 插入点="" */="" public="" void="" movestart(dispatch="" selection)="" {="" dispatch.call(selection,"homekey",new="" variant(6));="" }="" *="" *="" 从选定内容或插入点开始查找文本="" *="" @param="" selection="" dispatch="" 选定内容="" *="" @param="" tofindtext="" string="" 要查找的文本="" *="" @return="" boolean="" true-查找到并选中该文本,false-未查找到文本="" */="" public="" boolean="" find(dispatch="" selection,string="" tofindtext)="" {="" 从selection所在位置开始查询="" dispatch="" find="word.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="" dispatch="" 选定内容="" *="" @param="" newtext="" string="" 替换为文本="" */="" public="" void="">
转载请注明出处51数据库 » javapdf模板导出word