java怎么实现在线打开word时给word加水印?
展开全部package com.ymo.word;import com.jacob.activeX.ActiveXComponent;import com.jacob.com.ComThread;import com.jacob.com.Dispatch;import com.jacob.com.Variant;public class TestJacobWord {private ActiveXComponent wrdCom = null;private Dispatch doc = null;private Dispatch activeDoc = null;private Dispatch docSelect = null;private Dispatch docs = null;private static TestJacobWord instance = null;private String docName = "";public static TestJacobWord getInstance() {if (instance == null) {instance = new TestJacobWord();}return instance;}private boolean initWord() {boolean flag = false;ComThread.InitSTA();wrdCom = new ActiveXComponent("word.Application");try {docs = wrdCom.getProperty("Documents").toDispatch();wrdCom.setProperty("Visible", new Variant(false));flag = true;} catch (Exception e) {flag = false;e.printStackTrace();}return flag;}private void createNewDocument() {doc = Dispatch.call(docs, "Add").toDispatch();docSelect = Dispatch.get(wrdCom, "Selection").toDispatch();}private void getActiveDoc() {activeDoc = wrdCom.getProperty("ActiveWindow").toDispatch();System.out.println(activeDoc.getProgramId());}private void openDocument(String docPath) {if (this.doc != null) {closeDocument();}this.doc = Dispatch.call(docs, "Open", docPath, new Variant(false),new Variant(false)).toDispatch();docSelect = Dispatch.get(wrdCom, "Selection").toDispatch();}private void closeDocument() {if (doc != null) {Dispatch.call(doc, "Save");Dispatch.call(doc, "Close", new Variant(true));doc = null;}}private void setImgWaterMark(String waterMarkPath) {Dispatch activePan = Dispatch.get(activeDoc, "ActivePane").toDispatch();Dispatch view = Dispatch.get(activePan, "View").toDispatch();Dispatch.put(view, "SeekView", new Variant(9));Dispatch headfooter = Dispatch.get(docSelect, "HeaderFooter").toDispatch();// 取得图形对象Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();Dispatch pic = Dispatch.call(shapes, "AddPicture", waterMarkPath).toDispatch();Dispatch.call(pic, "Select");Dispatch.put(pic, "Left", new Variant(10));Dispatch.put(pic, "Top", new Variant(200));Dispatch.put(pic, "Width", new Variant(150));Dispatch.put(pic, "Height", new Variant(80));Dispatch.put(view, "SeekView", new Variant(0));}public void setTextWaterMark(String waterMarkStr) {Dispatch activePan = Dispatch.get(activeDoc, "ActivePane").toDispatch();Dispatch view = Dispatch.get(activePan, "View").toDispatch();Dispatch.put(view, "SeekView", new Variant(9));Dispatch headfooter = Dispatch.get(docSelect, "HeaderFooter").toDispatch();Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();Dispatch selection = Dispatch.call(shapes, "AddTextEffect",new Variant(9), waterMarkStr, "宋体", new Variant(1),new Variant(false), new Variant(false), new Variant(0),new Variant(0)).toDispatch();Dispatch.call(selection, "Select");Dispatch shapeRange = Dispatch.get(docSelect, "ShapeRange").toDispatch();Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject1");Dispatch textEffect = Dispatch.get(shapeRange, "TextEffect").toDispatch();Dispatch.put(textEffect, "NormalizedHeight", new Boolean(false));Dispatch line = Dispatch.get(shapeRange, "Line").toDispatch();Dispatch.put(line, "Visible", new Boolean(false));Dispatch fill = Dispatch.get(shapeRange, "Fill").toDispatch();Dispatch.put(fill, "Visible", new Boolean(true));// 设置水印透明度Dispatch.put(fill, "Transparency", new Variant(0.5));Dispatch foreColor = Dispatch.get(fill, "ForeColor").toDispatch();Dispatch.put(foreColor, "RGB", new Variant(16711620));Dispatch.call(fill, "Solid");// 设置水印旋转Dispatch.put(shapeRange, "Rotation", new Variant(315));Dispatch.put(shapeRange, "LockAspectRatio", new Boolean(true));Dispatch.put(shapeRange, "Height", new Variant(117.0709));Dispatch.put(shapeRange, "Width", new Variant(468.2835));Dispatch.put(shapeRange, "Left", new Variant(-999995));Dispatch.put(shapeRange, "Top", new Variant(-999995));Dispatch wrapFormat = Dispatch.get(shapeRange, "WrapFormat").toDispatch();// 是否允许交叠Dispatch.put(wrapFormat, "AllowOverlap", new Variant(true));Dispatch.put(wrapFormat, "Side", new Variant(3));Dispatch.put(wrapFormat, "Type", new Variant(3));Dispatch.put(shapeRange, "RelativeHorizontalPosition", new Variant(0));Dispatch.put(shapeRange, "RelativeVerticalPosition", new Variant(0));Dispatch.put(view, "SeekView", new Variant(0));}private void clos...
在Java中如何实现word的插入功能
首先,打开Word,然后在“插入”菜单下的“图片”——“来自文件”中选择想要调入的文件。
其次,在Word中插入图片后,还可以通过Word的图片工具箱对该图片进行简单的编辑操作。
第三,完成对图片的插入和编辑后,点击Word菜单栏中的“文件”——“保存”即可。
用java怎么实现对word的在线编辑
WORD在线编辑的方案有几种。
1)采用网页插件,比如PageOffice,这种方案实际是调用本地Office进行编辑,确切的说是网页编辑而非在线编辑。
2)采用微软的OWA,这个是网页版的Office,好处确实是Word,缺点是网页版功能比本地版本弱,开发难以得到微软的支持,坑很多,碰到了也不知道怎么解决。
与之接近的是一起写,石墨这样的类Word编辑,这个就不是原生word格式,是格式转换后的在线编辑。
3)采用UZER.ME的SDK,UZER.ME支持网页内无插件使用各种原生应用,当然也包括Word,本地也无需安装Office,全平台支持。
缺点是需要服务器资源较多。
在线编辑主要还是前端的活,主要用JavaScript做,当然,上面几种方案后台用Java都是可以也是需要的。
如何用JAVA打开一个word文件啊?要求有完整程序,跪求啊
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.html5:安装完后要启动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 如何实现打开PDF文件,并把文件显示出来;就是实现文件打开...
展开全部 java打开PDF需要借助其他的jar包的,如果我没记错的话,然后查看相应的API接口。
应该是以流的形式进行读取,之前写过一点是往里面写数据的,生成html文然后写入PDF文件 工具有:ItextPdf、FlyingAndItext、pd4ml 这些是将html文转换为PDF文件的,读取的话直接用bufferread读取试试(我没写过!)...
在Java中如何实现word的插入功能
首先,打开Word,然后在“插入”菜单下的“图片”——“来自文件”中选择想要调入的文件。
其次,在Word中插入图片后,还可以通过Word的图片工具箱对该图片进行简单的编辑操作。
第三,完成对图片的插入和编辑后,点击Word菜单栏中的“文件”——“保存”即可。
...
java如何生成word文档功能是怎么实现的
将Word转Html的原理是这样的:1、客户上传Word文档到服务器2、服务器调用OpenOffice程序打开上传的Word文档3、OpenOffice将Word文档另存为Html格式4、Over至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MS Office,不过OpenOffice的优势是跨平台,你懂的。
恩,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。
下面就是规规矩矩的实现。
1、下载OpenOffice,2、下载Jodconverter 这是一个开启OpenOffice进行格式转化的第三方jar包。
3、泡杯热茶,等待下载。
4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\Program Files (x86)\OpenOffice.org 3\program>soffice -headless -accept="socket,port=8100;urp;"5、打开eclipse6、喝杯热茶,等待eclipse打开。
7、新建eclipse项目,导入Jodconverter/lib 下得jar包。
* commons-io * jodconverter * juh * jurt * ridl * slf4j-api * slf4j-jdk14 * unoil * xstream 8、Coding...查看代码package com.mzule.doc2html.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.net.ConnectException;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;import com.artofsolving.jodconverter.DocumentConverter;import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;/** * 将Word文档转换成html字符串的工具类 * * @author MZULE * */public class Doc2Html { public static void main(String[] args) { System.out .println(toHtmlString(new File("C:/test/test.doc"), "C:/test")); } /** * 将word文档转换成html文档 * * @param docFile * 需要转换的word文档 * @param filepath * 转换之后html的存放路径 * @return 转换之后的html文件 */ public static File convert(File docFile, String filepath) { // 创建保存html的文件 File htmlFile = new File(filepath + "/" + new Date().getTime() + ".html"); // 创建Openoffice连接 OpenOfficeConnection con = new SocketOpenOfficeConnection(8100); try { // 连接 con.connect(); } catch (ConnectException e) { System.out.println("获取OpenOffice连接失败..."); e.printStackTrace(); } // 创建转换器 DocumentConverter converter = new OpenOfficeDocumentConverter(con); // 转换文档问html converter.convert(docFile, htmlFile); // 关闭openoffice连接 con.disconnect(); return htmlFile; } /** * 将word转换成html文件,并且获取html文件代码。
* * @param docFile * 需要转换的文档 * @param filepath * 文档中图片的保存位置 * @return 转换成功的html代码 */ public static String toHtmlString(File docFile, String filepath) { // 转换word文档 File htmlFile = convert(docFile, filepath); // 获取html文件流 StringBuffer htmlSb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(htmlFile))); while (br.ready()) { htmlSb.append(br.readLine()); } br.close(); // 删除临时文件 htmlFile.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // HTML文件字符串 String htmlStr = htmlSb.toString(); // 返回经过清洁的html文本 return clearFormat(htmlStr, filepath); } /** * 清除一些不需要的html标记 * * @param htmlStr * 带有复杂html标记的html语句 * @return 去除了不需要html标记的语句 */ protected static String clearFormat(String htmlStr, String docImgPath) { // 获取body内容的正则 String bodyReg = ""; Pattern bodyPattern = Pattern.compile(bodyReg); Matcher bodyMatcher = bodyPattern.matcher(htmlStr); if (bodyMatcher.find()) { // 获取BODY内容,并转化BODY标签为DIV htmlStr = bodyMatcher.group().replaceFirst("", ""); } // 调整图片地址 htmlStr = htmlStr.replaceAll("转换成保留样式 // content = content.replaceAll("(]*>.*?)()", // ""); // 把转换成并删除样式 htmlStr = htmlStr.replaceAll("(]*)(>.*?)()", ""); // 删除不需要的标签 htmlStr = htmlStr .replaceAll( "]*?>", ""); // 删除不需要的属性 htmlStr = htmlStr .replaceAll( "]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>", ""); return htmlStr; }}
转载请注明出处51数据库 » java实现打开word
花落季16238097