利用poi实现word转换html
利用POI将word转换成html实现在线阅读
利用POI将word转换成html实现在线阅读
一、分析
通过网上找资料,发现用java实现word在线阅读有以下的实现方式:
1 |
Word=>PDF(OpenOffice+JodConverter)=>SWF(pdf2swf)=>FlexPaper浏览 |
2 |
Word=>PDF(MSOffice+JACOB)=>SWF(pdf2swf)=>FlexPaper浏览 |
3 |
Word =>SWF (FlashPaper)=> FlexPaper浏览 |
4 |
Word=>SWF(print2flash)=> FlexPaper浏览 |
5 |
用第三方收费组件:PageOffice |
6 |
1)利用POI把Word2003转换成html; 2)利用OpenOffice+JodConverter将word2003转换成html |
前4种方式,目标都是一致的,就是都将word文档转换成flash文件,只是中间的实现不大一样。前两种方式比较麻烦,都是先转成PDF,再转成SWF,最后用FlexPaper浏览。两种比较快捷,可直接将源文件转为SWF,用FlexPaper浏览。第二种方式用到的jacob是微软的组件,在linux平台下基本是无望的了,第一个淘汰。由于FlashPaper不是开源工具,加之Win8系统不兼容(我现在用的系统),所以就没采用第三种实现方式。Print2flash是开源工具,即使公司产品中用到也不会出现版权纠纷,遗憾的是没找到如何用程序控制该工具转换文件的命令。所以第3,4种方式也淘汰了。通过下载,预使用,发现第5种方式用PageOffice是最省时省力的,也能将word文档完美的展现,但是,要钱!!好吧,一提到钱,此种实现只能暂作废。
后面一开始是想用OpenOffice+JodConverter实现转swf的,后面在逛百度文库的时候,发现一个让我很好奇的东西。就是,百度文库里的文档基本上都用html进行展示了,也就是说,我们上传的word文档,百度对其做了html转换的处理,与页面的嵌合也相当的好。这让我想到,我们的项目中是否也可以用此方式实现word的在线预览呢。
基于这个想法,我到谷歌找相关的资料,发现将word转html的开源工具没几个。其中,介绍得比较多的就是用POI进行转换,但是,由于POI对word的处理功能相当的弱,因此,开启了使用POI将wordàhtml的艰苦历程(后面发现网上有介绍用OpenOffice+JodConverter将word2003转换成html的方式,但是,我没有深究,有兴趣的同学可以去观望一下http://www.cnblogs.com/codeplus/archive/2011/10/22/2220952.html):
二、实现
1. POI介绍:
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“可怜的模糊实现”。
Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。Apache POI 提供Java操作Excel解决方案(适用于Excel97-2008)。
基本结构:
HSSF -提供读写Microsoft Excel XLS格式档案的功能。
XSSF -提供读写Microsoft Excel OOXML XLSX格式档案的功能。
HWPF -提供读写Microsoft Word DOC格式档案的功能。
HSLF -提供读写Microsoft PowerPoint格式档案的功能。
HDGF -提供读Microsoft Visio格式档案的功能。
HPBF -提供读Microsoft Publisher格式档案的功能。
HSMF -提供读Microsoft Outlook格式档案的功能。
其实,POI比较拿手的是处理Excel表格,即上面的HSSF及XSSF,我们的很多项目,只要涉及报表的,基本上都有用到它吧。用对于HWPF即处理DOC的包,功能就没有那么健全了,且API也不完善。
2. poi相关包及依赖包配置。
3. 处理流程图:
1) 主体流程:
2) 进行word文档解释转换子流程
3) 处理表格子流程(略)
4) 处理图片子流程(略)
4. 代码实现
- packagecom;
- importjava.awt.image.BufferedImage;
- importjava.io.BufferedWriter;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileNotFoundException;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.OutputStream;
- importjava.io.OutputStreamWriter;
- importjavax.imageio.ImageIO;
- importorg.apache.poi.hwpf.HWPFDocument;
- importorg.apache.poi.hwpf.model.PicturesTable;
- importorg.apache.poi.hwpf.usermodel.CharacterRun;
- importorg.apache.poi.hwpf.usermodel.Paragraph;
- importorg.apache.poi.hwpf.usermodel.Picture;
- importorg.apache.poi.hwpf.usermodel.Range;
- importorg.apache.poi.hwpf.usermodel.Table;
- importorg.apache.poi.hwpf.usermodel.TableCell;
- importorg.apache.poi.hwpf.usermodel.TableIterator;
- importorg.apache.poi.hwpf.usermodel.TableRow;
- importorg.apache.xmlbeans.impl.piccolo.io.FileFormatException;
- /**
- * @Description: 利用poi将word简单的转换成html文件
- * @author 柯颖波
- * @date 2013-12-20 上午09:32:44
- * @version v1.0
- */
- publicclassWord2Html {
- /**
- * 回车符ASCII码
- */
- privatestaticfinalshortENTER_ASCII =13;
- /**
- * 空格符ASCII码
- */
- privatestaticfinalshortSPACE_ASCII =32;
- /**
- * 水平制表符ASCII码
- */
- privatestaticfinalshortTABULATION_ASCII =9;
- privatestaticString htmlText ="";
- privatestaticString htmlTextTbl ="";
- privatestaticintcounter =0;
- privatestaticintbeginPosi =0;
- privatestaticintendPosi =0;
- privatestaticintbeginArray[];
- privatestaticintendArray[];
- privatestaticString htmlTextArray[];
- privatestaticbooleantblExist =false;
- /**
- * 项目路径
- */
- privatestaticString projectRealPath ="";
- /**
- * 临时文件路径
- */
- privatestaticString tempPath ="/upfile/"+ File.separator +"transferFile"+ File.separator;
- /**
- * word文档名称
- */
- privatestaticString wordName ="";
- publicstaticvoidmain(String argv[]) {
- try{
- wordToHtml("F:\\SVN\\BobUtil\\web\\", "2012年高考广东数学(文)试卷解析(精析word版)(学生版).doc");
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 读取每个文字样式
- *
- * @param fileName
- * @throws Exception
- */
- privatestaticvoidgetWordAndStyle(String fileName)throwsException {
- FileInputStream in = newFileInputStream(newFile(fileName));
- HWPFDocument doc = newHWPFDocument(in);
- Range rangetbl = doc.getRange();// 得到文档的读取范围
- TableIterator it = newTableIterator(rangetbl);
- intnum =100;
- beginArray = newint[num];
- endArray = newint[num];
- htmlTextArray = newString[num];
- tblExist = false;
- // 取得文档中字符的总数
- intlength = doc.characterLength();
- // 创建图片容器
- PicturesTable pTable = doc.getPicturesTable();
- // 创建段落容器
- htmlText = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>"
- + doc.getSummaryInformation().getTitle()
- + "</title></head><body><div style='margin:60px;text-align:center;'><div style='width:620px;text-align:left;line-height:24px;'>";
- // 创建临时字符串,好加以判断一串字符是否存在相同格式
- if(it.hasNext()) {
- readTable(it, rangetbl);
- }
- intcur =0;
- String tempString = "";
- for(inti =0; i < length -1; i++) {
- // 整篇<span class="wp_keywordlink"><a target=_blank href="http://www.it-crazy.com/" title="文章" target="_blank">文章</a></span>的字符通过一个个字符的来判断,range为得到文档的范围
- Range range = newRange(i, i +1, doc);
- CharacterRun cr = range.getCharacterRun(0);
- // beginArray=new int[num];
- // endArray=new int[num];
- // htmlTextArray=new String[num];
- if(tblExist) {
- if(i == beginArray[cur]) {
- htmlText += tempString + htmlTextArray[cur];
- tempString = "";
- i = endArray[cur] - 1;
- cur++;
- continue;
- }
- }
- if(pTable.hasPicture(cr)) {
- htmlText += tempString;
- // 读写图片
- try{
- readPicture(pTable, cr);
- } catch(Exception e) {
- e.printStackTrace();
- }
- tempString = "";
- } else{
- Range range2 = newRange(i +1, i +2, doc);
- // 第二个字符
- CharacterRun cr2 = range2.getCharacterRun(0);
- charc = cr.text().charAt(0);
- // System.out.println(c);
- // /System.out.println(i+"::"+range.getEndOffset()+"::"+range.getStartOffset()+"::"+c);
- // 判断是否为回车符
- if(c == ENTER_ASCII) {
- tempString += "<br/>";
- }
- // 判断是否为空格符
- elseif(c == SPACE_ASCII)
- tempString += " ";
- // 判断是否为水平制表符
- elseif(c == TABULATION_ASCII)
- tempString += " ";
- // 比较前后2个字符是否具有相同的格式
- booleanflag = compareCharStyle(cr, cr2);
- if(flag)
- tempString += cr.text();
- else{
- String fontStyle = "<span style=\"font-family:"+ cr.getFontName() +";font-size:"
- + cr.getFontSize() / 2+"pt;";
- if(cr.isBold())
- fontStyle += "font-weight:bold;";
- if(cr.isItalic())
- fontStyle += "font-style:italic;";
- if(cr.isStrikeThrough())
- fontStyle += "text-decoration:line-through;";
- intfontcolor = cr.getIco24();
- int[] rgb =newint[3];
- if(fontcolor != -1) {
- rgb[0] = (fontcolor >>0) &0xff;// red;
- rgb[1] = (fontcolor >>8) &0xff;// green
- rgb[2] = (fontcolor >>16) &0xff;// blue
- }
- fontStyle += "color: rgb("+ rgb[0] +","+ rgb[1] +","+ rgb[2] +");";
- htmlText += fontStyle + "\">"+ tempString + cr.text() +"</span>";
- tempString = "";
- }
- }
- }
- htmlText += tempString + "</div></div></body></html>";
- // System.out.println(htmlText);
- }
- /**
- * 读写文档中的表格
- *
- * @param pTable
- * @param cr
- * @throws Exception
- */
- privatestaticvoidreadTable(TableIterator it, Range rangetbl)throwsException {
- htmlTextTbl = "";
- // 迭代文档中的表格
- counter = -1;
- while(it.hasNext()) {
- tblExist = true;
- htmlTextTbl = "";
- Table tb = (Table) it.next();
- beginPosi = tb.getStartOffset();
- endPosi = tb.getEndOffset();
- // System.out.println("............"+beginPosi+"...."+endPosi);
- counter = counter + 1;
- // 迭代行,默认从0开始
- beginArray[counter] = beginPosi;
- endArray[counter] = endPosi;
- htmlTextTbl += "<table border='1' cellpadding='0' cellspacing='0' >";
- for(inti =0; i < tb.numRows(); i++) {
- TableRow tr = tb.getRow(i);
- htmlTextTbl += "<tr align='center'>";
- // 迭代列,默认从0开始
- for(intj =0; j < tr.numCells(); j++) {
- TableCell td = tr.getCell(j);// 取得单元格
- intcellWidth = td.getWidth();
- // 取得单元格的内容
- for(intk =0; k < td.numParagraphs(); k++) {
- Paragraph para = td.getParagraph(k);
- CharacterRun crTemp = para.getCharacterRun(0);
- String fontStyle = "<span style=\"font-family:"+ crTemp.getFontName() +";font-size:"
- + crTemp.getFontSize() / 2+"pt;color:"+ crTemp.getColor() +";";
- if(crTemp.isBold())
- fontStyle += "font-weight:bold;";
- if(crTemp.isItalic())
- fontStyle += "font-style:italic;";
- String s = fontStyle + "\">"+ para.text().toString().trim() +"</span>";
- if(s =="") {
- s = " ";
- }
- // System.out.println(s);
- htmlTextTbl += "<td width="+ cellWidth +">"+ s +"</td>";
- // System.out.println(i + ":" + j + ":" + cellWidth + ":" + s);
- } // end for
- } // end for
- } // end for
- htmlTextTbl += "</table>";
- htmlTextArray[counter] = htmlTextTbl;
- } // end while
- }
- /**
- * 读写文档中的图片
- *
- * @param pTable
- * @param cr
- * @throws Exception
- */
- privatestaticvoidreadPicture(PicturesTable pTable, CharacterRun cr)throwsException {
- // 提取图片
- Picture pic = pTable.extractPicture(cr, false);
- BufferedImage image = null;// 图片对象
- // 获取图片样式
- intpicHeight = pic.getHeight() * pic.getAspectRatioY() /100;
- intpicWidth = pic.getAspectRatioX() * pic.getWidth() /100;
- if(picWidth >500) {
- picHeight = 500* picHeight / picWidth;
- picWidth = 500;
- }
- String style = " style='height:"+ picHeight +"px;width:"+ picWidth +"px'";
- // 返回POI建议的图片文件名
- String afileName = pic.suggestFullFileName();
- //单元测试路径
- String directory = "images/"+ wordName +"/";
- //项目路径
- //String directory = tempPath + "images/" + wordName + "/";
- makeDir(projectRealPath, directory);// 创建文件夹
- intpicSize = cr.getFontSize();
- intmyHeight =0;
- if(afileName.indexOf(".wmf") >0) {
- OutputStream out = newFileOutputStream(newFile(projectRealPath + directory + afileName));
- out.write(pic.getContent());
- out.close();
- afileName = Wmf2Png.convert(projectRealPath + directory + afileName);
- File file = newFile(projectRealPath + directory + afileName);
- try{
- image = ImageIO.read(file);
- } catch(Exception e) {
- e.printStackTrace();
- }
- intpheight = image.getHeight();
- intpwidth = image.getWidth();
- if(pwidth >500) {
- htmlText += "<img style='width:"+ pwidth +"px;height:"+ myHeight +"px'"+" src=\""+ directory
- + afileName + "\"/>";
- } else{
- myHeight = (int) (pheight / (pwidth / (picSize *1.0)) *1.5);
- htmlText += "<img style='vertical-align:middle;width:"+ picSize *1.5+"px;height:"+ myHeight
- + "px'"+" src=\""+ directory + afileName +"\"/>";
- }
- } else{
- OutputStream out = newFileOutputStream(newFile(projectRealPath + directory + afileName));
- // pic.writeImageContent(out);
- out.write(pic.getContent());
- out.close();
- // 处理jpg或其他(即除png外)
- if(afileName.indexOf(".png") == -1) {
- try{
- File file = newFile(projectRealPath + directory + afileName);
- image = ImageIO.read(file);
- picHeight = image.getHeight();
- picWidth = image.getWidth();
- if(picWidth >500) {
- picHeight = 500* picHeight / picWidth;
- picWidth = 500;
- }
- style = " style='height:"+ picHeight +"px;width:"+ picWidth +"px'";
- } catch(Exception e) {
- // e.printStackTrace();
- }
- }
- htmlText += "<img "+ style +" src=\""+ directory + afileName +"\"/>";
- }
- if(pic.getWidth() >450) {
- htmlText += "<br/>";
- }
- }
- privatestaticbooleancompareCharStyle(CharacterRun cr1, CharacterRun cr2) {
- booleanflag =false;
- if(cr1.isBold() == cr2.isBold() && cr1.isItalic() == cr2.isItalic()
- && cr1.getFontName().equals(cr2.getFontName()) && cr1.getFontSize() == cr2.getFontSize()) {
- flag = true;
- }
- returnflag;
- }
- /**
- * 写文件(成功返回true,失败则返回false)
- *
- * @param s
- * 要写入的内容
- * @param filePath
- * 文件
- */
- privatestaticbooleanwriteFile(String s, String filePath) {
- FileOutputStream fos = null;
- BufferedWriter bw = null;
- s = s.replaceAll("EMBED","").replaceAll("Equation.DSMT4","");
- try{
- makeDir(projectRealPath, tempPath);// 创建文件夹
- File file = newFile(filePath);
- if(file.exists()) {
- returnfalse;
- }
- fos = newFileOutputStream(file);
- bw = newBufferedWriter(newOutputStreamWriter(fos,"utf-8"));
- bw.write(s);
- // System.out.println(filePath + "文件写入成功!");
- } catch(FileNotFoundException fnfe) {
- fnfe.printStackTrace();
- } catch(IOException ioe) {
- ioe.printStackTrace();
- } finally{
- try{
- if(bw !=null)
- bw.close();
- if(fos !=null)
- fos.close();
- } catch(IOException ie) {
- ie.printStackTrace();
- }
- }
- returntrue;
- }
- /**
- * 根据路径名生成多级路径
- *
- * @param url
- * 参数要以"\classes\cn\qtone\"或者"/classes/cn/qtone/"
- */
- privatestaticString makeDir(String root, String url) {
- String[] sub;
- url = url.replaceAll("\\/","\\\\");
- if(url.indexOf("\\") > -1) {
- sub = url.split("\\\\");
- } else{
- return"-1";
- }
- File dir = null;
- try{
- dir = newFile(root);
- for(inti =0; i < sub.length; i++) {
- if(!dir.exists() && !sub[i].equals("")) {
- dir.mkdir();
- }
- File dir2 = newFile(dir + File.separator + sub[i]);
- if(!dir2.exists()) {
- dir2.mkdir();
- }
- dir = dir2;
- }
- } catch(Exception e) {
- e.printStackTrace();
- return"-1";
- }
- returndir.toString();
- }
- /**
- * 将word文档转化,返回转化后的文件路径
- *
- * @param projectPath
- * 项目路径
- * @param relativeFilePath
- * 文件相对路径
- * @return 返回生成的htm路径(如果出错,则返回null)
- */
- publicstaticString wordToHtml(String projectPath, String relativeFilePath) {
- String resultPath = null;
- projectRealPath = projectPath;// 项目路径
- String filePath = "";
- // System.out.println(projectRealPath + tempPath);
- // System.out.println(makeDir(projectRealPath, tempPath));
- try{
- File file = newFile(projectPath + relativeFilePath);
- if(file.exists()) {
- if(file.getName().indexOf(".doc") == -1|| file.getName().indexOf(".docx") >0) {
- thrownewFileFormatException("请确认文件格式为doc!");
- } else{
- wordName = file.getName();
- wordName = wordName.substring(0, wordName.indexOf("."));
- filePath = projectRealPath + tempPath + wordName + ".htm";
- synchronized(relativeFilePath) {// 处理线程同步问题
- File ff = newFile(filePath);
- if(!ff.exists()) {// 如果不存在则进行转换
- getWordAndStyle(projectPath + relativeFilePath);
- writeFile(htmlText, filePath);
- }
- }
- resultPath = tempPath + wordName + ".htm";
- }
- } else{
- thrownewFileNotFoundException("没找到相关文件!");
- }
- } catch(NullPointerException e) {
- e.printStackTrace();
- } catch(FileNotFoundException e) {
- e.printStackTrace();
- } catch(Exception e) {
- e.printStackTrace();
- }
- returnresultPath;
- }
- }
- packagecom;
- importjava.io.ByteArrayInputStream;
- importjava.io.ByteArrayOutputStream;
- importjava.io.File;
- importjava.io.FileInputStream;
- importjava.io.FileOutputStream;
- importjava.io.InputStream;
- importjava.io.OutputStream;
- importjava.util.Scanner;
- importjava.util.zip.GZIPOutputStream;
- importjavax.xml.parsers.DocumentBuilder;
- importjavax.xml.parsers.DocumentBuilderFactory;
- importjavax.xml.transform.OutputKeys;
- importjavax.xml.transform.Transformer;
- importjavax.xml.transform.TransformerFactory;
- importjavax.xml.transform.dom.DOMSource;
- importjavax.xml.transform.stream.StreamResult;
- importnet.arnx.wmf2svg.gdi.svg.SvgGdi;
- importnet.arnx.wmf2svg.gdi.wmf.WmfParser;
- importorg.apache.batik.transcoder.TranscoderInput;
- importorg.apache.batik.transcoder.TranscoderOutput;
- importorg.apache.batik.transcoder.TranscodingHints;
- importorg.apache.batik.transcoder.image.PNGTranscoder;
- importorg.apache.batik.transcoder.wmf.tosvg.WMFTranscoder;
- importorg.apache.commons.lang.StringUtils;
- importorg.w3c.dom.Document;
- importorg.w3c.dom.Element;
- publicclassWmf2Png {
- publicstaticvoidmain(String[] args)throwsException {
- // convert("F:\\SVN\\BobUtil\\web\\25177.wmf");
- // System.out.println((20 / (21 * 1.0)));
- // svgToPng("F:\\SVN\\BobUtil\\web\\25177.svg", "F:\\SVN\\BobUtil\\web\\25177.png");
- }
- /**
- * @Description: 进行转换
- * @param filePath
- * 文件路径
- * @return 设定文件
- */
- publicstaticString convert(String filePath) {
- String pngFile = "";
- File wmfFile = newFile(filePath);
- try{
- if(!wmfFile.getName().contains(".wmf")) {
- thrownewException("请确认输入的文件类型是wmf");
- }
- // wmf -> svg
- String svgFile = filePath.replace("wmf","svg");
- wmfToSvg(filePath, svgFile);
- // 对svg做预出理
- PreprocessSvgFile(svgFile);
- // svg -> png
- pngFile = filePath.replace("wmf","png");
- svgToPng(svgFile, pngFile);
- // 删除 svg
- File file = newFile(svgFile);
- if(file.exists()) {
- file.delete();
- }
- // 删除 wmf
- if(wmfFile.exists()) {
- wmfFile.delete();
- }
- } catch(Exception e) {
- try{
- e.printStackTrace();
- wmfToJpg(filePath);
- } catch(Exception e1) {
- e1.printStackTrace();
- }
- }
- returnwmfFile.getName().replace("wmf","png");
- }
- /**
- * 将wmf转换为svg
- *
- * @param src
- * @param dest
- */
- publicstaticvoidwmfToSvg(String src, String dest)throwsException {
- booleancompatible =false;
- try{
- InputStream in = newFileInputStream(src);
- WmfParser parser = newWmfParser();
- finalSvgGdi gdi =newSvgGdi(compatible);
- parser.parse(in, gdi);
- Document doc = gdi.getDocument();
- OutputStream out = newFileOutputStream(dest);
- if(dest.endsWith(".svgz")) {
- out = newGZIPOutputStream(out);
- }
- output(doc, out);
- } catch(Exception e) {
- throwe;
- }
- }
- /**
- * @Description: 输出svg文件
- * @param doc
- * @param out
- * @throws Exception
- * 设定文件
- */
- privatestaticvoidoutput(Document doc, OutputStream out)throwsException {
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer();
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");
- transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD SVG 1.0//EN");
- transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
- "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
- transformer.transform(newDOMSource(doc),newStreamResult(out));
- out.flush();
- out.close();
- out = null;
- }
- /**
- * @Description:对svg文件做预处理(这里主要是调整大小,先缩小10倍,如果还大于默认值,则按比例缩小)
- * @param svgFile
- * @throws Exception
- * 设定文件
- */
- privatestaticvoidPreprocessSvgFile(String svgFile)throwsException {
- intdefaultWeight =500;// 默认宽度
- FileInputStream inputs = newFileInputStream(svgFile);
- Scanner sc = newScanner(inputs,"UTF-8");
- ByteArrayOutputStream os = newByteArrayOutputStream();
- while(sc.hasNextLine()) {
- String ln = sc.nextLine();
- if(!ln.startsWith("<!DOCTYPE")) {
- os.write((ln + "\r\n").getBytes());
- }
- }
- os.flush();
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder;
- builder = factory.newDocumentBuilder();
- Document doc = null;
- try{
- doc = builder.parse(newByteArrayInputStream(os.toByteArray()));
- } catch(Exception e) {
- inputs = newFileInputStream(svgFile);
- os = newByteArrayOutputStream();
- intnoOfByteRead =0;
- while((noOfByteRead = inputs.read()) != -1) {
- os.write(noOfByteRead);
- }
- os.flush();
- doc = builder.parse(newByteArrayInputStream(os.toByteArray()));
- } finally{
- os.close();
- inputs.close();
- }
- intheight = Integer.parseInt(((Element) doc.getElementsByTagName("svg").item(0)).getAttribute("height"));
- intwidth = Integer.parseInt(((Element) doc.getElementsByTagName("svg").item(0)).getAttribute("width"));
- intnewHeight =0;// 新高
- intnewWidth =0;// 新宽
- newHeight = height / 10;// 高缩小10倍
- newWidth = width / 10;// 宽缩小10倍
- // 如果缩小10倍后宽度还比defaultHeight大,则进行调整
- if(newWidth > defaultWeight) {
- newWidth = defaultWeight;
- newHeight = defaultWeight * height / width;
- }
- ((Element) doc.getElementsByTagName("svg").item(0)).setAttribute("width", String.valueOf(newWidth));
- ((Element) doc.getElementsByTagName("svg").item(0)).setAttribute("height", String.valueOf(newHeight));
- OutputStream out = newFileOutputStream(svgFile);
- output(doc, out);
- }
- /**
- * 将svg图片转成png图片
- *
- * @param filePath
- * @throws Exception
- */
- publicstaticvoidsvgToPng(String svgPath, String pngFile)throwsException {
- File svg = newFile(svgPath);
- FileInputStream wmfStream = newFileInputStream(svg);
- ByteArrayOutputStream imageOut = newByteArrayOutputStream();
- intnoOfByteRead =0;
- while((noOfByteRead = wmfStream.read()) != -1) {
- imageOut.write(noOfByteRead);
- }
- imageOut.flush();
- imageOut.close();
- wmfStream.close();
- ByteArrayOutputStream jpg = newByteArrayOutputStream();
- FileOutputStream jpgOut = newFileOutputStream(pngFile);
- byte[] bytes = imageOut.toByteArray();
- PNGTranscoder t = newPNGTranscoder();
- TranscoderInput in = newTranscoderInput(newByteArrayInputStream(bytes));
- TranscoderOutput out = newTranscoderOutput(jpg);
- t.transcode(in, out);
- jpgOut.write(jpg.toByteArray());
- jpgOut.flush();
- jpgOut.close();
- imageOut = null;
- jpgOut = null;
- }
- /**
- * 将wmf图片转成png图片(备用方法,即当上面的转换失败时用这个)
- *
- * @param filePath
- * @throws Exception
- */
- publicstaticString wmfToJpg(String wmfPath)throwsException {
- //先wmf-->svg
- File wmf = newFile(wmfPath);
- FileInputStream wmfStream = newFileInputStream(wmf);
- ByteArrayOutputStream imageOut = newByteArrayOutputStream();
- intnoOfByteRead =0;
- while((noOfByteRead = wmfStream.read()) != -1) {
- imageOut.write(noOfByteRead);
- }
- imageOut.flush();
- imageOut.close();
- wmfStream.close();
- // WMFHeaderProperties prop = new WMFHeaderProperties(wmf);
- WMFTranscoder transcoder = newWMFTranscoder();
- TranscodingHints hints = newTranscodingHints();
- transcoder.setTranscodingHints(hints);
- TranscoderInput input = newTranscoderInput(newByteArrayInputStream(imageOut.toByteArray()));
- ByteArrayOutputStream svg = newByteArrayOutputStream();
- TranscoderOutput output = newTranscoderOutput(svg);
- transcoder.transcode(input, output);
- //再svg-->png
- ByteArrayOutputStream jpg = newByteArrayOutputStream();
- String jpgFile = StringUtils.replace(wmfPath, "wmf","png");
- FileOutputStream jpgOut = newFileOutputStream(jpgFile);
- byte[] bytes = svg.toByteArray();
- PNGTranscoder t = newPNGTranscoder();
- TranscoderInput in = newTranscoderInput(newByteArrayInputStream(bytes));
- TranscoderOutput out = newTranscoderOutput(jpg);
- t.transcode(in, out);
- jpgOut.write(jpg.toByteArray());
- jpgOut.flush();
- jpgOut.close();
- returnjpgFile;
- }
- }
重点难点解释探讨:
1) 读取表格部分:
a) 找出表格的开始与结束标记;
b) 遍历整个表格内容,逐个单元格的内容取出并追加到变量中。
2) 读取图片部分
a) 图片文件的格式问题。
如果图片格式为png或者jpg,则可以直接进行处理并加入标签中,前台的html展示没有问题,但是,如果图片格式为wmf(详细看附录1),则html无法对基解释,那么我们只能对其进行转换格式:
百度后,网上很多说法都建议用batik工具包进行格式转换,其实思路就是:wmfàsvgàpng。查阅相关资料(如附录2),发现其处理svg文件的能力相当的强,即从svg—>png这一步是比较完美的。但是,在处理wmf—>svg这一步却导致部分图像丢失,即失真的情况,且很严重。查看相关的api看是否参数设置问题,但是无论怎么设置,结果还是不尽人意。一度想放弃,找别的包。
后来,无意中,在csdn中有网友建议先用wmf2svg工具类将wmf转换为svg,再用batik将svg转换为png。Very
good!!有了这个思路,感觉已经看到署光了。
类写出来后,进行类型转换测试,确实效果很好,完全没有失真。于是将其嵌入word—>html这个工具类中。再用各种包含了wmf图片的文档进行测试。生成的html文件,基本没有问题,当时那个开心啊!!(我去,程序员也就这德行)
好景不长,放到正式项目进行测试过程中,发现有个别文档一进行转换,服务器就跨了,直接报内存溢出。通过排查检测,原来就是进行图片转换过程中,将内存给挤爆了。奇怪了,虽然知道图片处理是比较耗内存,但也没想到1G的内存,一下子就被挤爆(刚跑起来占去300M左右,一跑word转换功能,不过一会就报OutOfMemorry)。
一度怀疑,是不是batik这个工具包是不是有bug,处理不了大的svg。还将问题放上了bakit的官网。后来,查看相关资料后,发现是wmf2svg工具生成的svg的高与宽都太大了,举个例子:15040* 13088,宽高都达到上万级别,结果得到的象素是上亿的,不爆内存才怪。
用dom工具,将每一个生成的svg文件再进行预处理,即将其高与宽都先缩小一倍,如果宽度依然比500要大,则将其设成500,并将高也按比例缩小。经过此步骤生成的svg再用batik进行转换就没有任何问题了。
到这里,差不多已经解决图片转换的问题了,但是,在使用过程中,发现wmf2svg这个工具也不是很稳定,偶尔会报异常,并且,我测试发现,报异常的这个wmf用之前batik直接进行wmf—>svgàpng的方案可以成功生成没有失真的png,于是,在wmf2svg的产生异常进行捕捉,并调用了wmfToJpg(String wmfPath)的备用方法。到此,大部分的wmf转换问题已经解决。
b) 生成html文本的<img />标签的width与height问题。
如果图片格式原本为png的话,直接用
- // 获取图片样式
- intpicHeight = pic.getHeight() * pic.getAspectRatioY() / 100;
- intpicWidth = pic.getAspectRatioX() * pic.getWidth() /100;
即可以将图片的宽与高设置与word文档一致;但是,发果wmf格式,要分两种情况分析:
Ø 如果转换生成的png宽度不小于500,则将期作为一般图片处理:
- BufferedImage image = ImageIO.read(file);
- intpheight = image.getHeight();
- intpwidth = image.getWidth();
Ø 如果转换生成的png宽度小于500,则认为是一般的公式,则应该与它旁边的字体宽度相近,这里设成字体的1.5倍宽度,高度为:
- myHeight= (int) (pheight / (pwidth / (picSize *1.0)) *1.5);
如果图片即非wmf与非png(如jpg)的情况下,上面获取高与宽的方法不起作用,不知道是不是POI的bug。只能按以下方式处理:
- BufferedImage image = ImageIO.read(file);
- intpheight = image.getHeight();
- intpwidth = image.getWidth();
即跟上面处理wmf的第一种方式一致。
三、结束语
讲到这,将word转换成html的处理也大体上讲完了。这几天的边学边用,特别是真正能解决问题的时候,非常有成就感。其实,上面的处理还存在以下的问题待解决的:
1)读取表格部分:
a) 表格中如果再含有表格,POI无法进行很好的区分,比如,有一个两行两列的表格中,第一行第一列中又包含了一个两行两列的表格,那POI会将此表格解释成:第一行为2+2*2 = 6个单元格;第二行为2个单元格,这样解释出来的表格就很怪异了。
b) 表格中有果有合并单格的情况,程序暂未做此处理(后续看不能优化),表格也很怪异。
c) 表格中如果有图像,程序没有做相应的处理。
2)读取图片部分:
a) 有部分wmf->png的方式有个别图片还是没有转换成功,会报异常,但没有影响整体的功能;
b) word有部分公式生成的图片无法识别模式,不知道是不是POI无法将其解释,还是其他原因,就是有文档,生成没有后缀的图片文件,且这部分文件无法读取,用图片工具也打不开,暂时未找到很好的解决方案。
3)读取word的目录:
在读取目录会出现将格式化符号也解释出来。
4)其他未知的一些问题,反正,就觉得用POI来解释word是件很坚苦的事情,如果全是文本还好,如果里面包含图片,表格,公式等这些对象的时候,POI就显得太弱了。
附:
1. wmf文件:
MicrosoftOffice 的剪贴画使用的就是这个格式。
Wmf是WindowsMetafile 的缩写,简称图元文件,它是微软公司定义的一种Windows平台下的图形文件格式。
wmf格式文件的特点如下:
1) wmf格式文件是MicrosoftWindows操作平台所支持的一种图形格式文件,目前,其它操作系统尚不支持这种格式,如Unix、Linux等。
2) 与bmp格式不同,wmf格式文件是和设备无关的,即它的输出特性不依赖于具体的输出设备。
3) 其图象完全由Win32 API所拥有的GDI函数来完成。
4) wmf格式文件所占的磁盘空间比其它任何格式的图形文件都要小得多。
5) 在建立图元文件时,不能实现即画即得,而是将GDI调用记录在图元文件中,之后,在GDI环境中重新执行,才可显示图象。
6) 显示图元文件的速度要比显示其它格式的图象文件慢,但是它形成图元文件的速度要远大于其它格式。
2. Batik介绍
Batik是使用svg格式图片来实现各种功能的应用程序以及Applet提供的一个基于java的工具包。
通过Batik,你可以在JAVA可以使用的地方操作SVG文档,您还可以在你的应用程序使用Batik模块来生成, 处理和转码SVG图像。Batik很容易让基于Java的应用程序或小程序来处理SVG内容。 例如,使用Batik的SVG的发生器模块 ,Java应用程序或小程序可以很轻松地导出SVG格式的图形到。用Batik的SVG的查看组件,应用程序或小程序可以很容易地集成SVG的浏览和交互功能。另一种可能性是使用Batik的模块转换成各种格式SVG的通过,如光栅图像(JPEG,PNG或TIFF格式)或其它矢量格式(EPS或PDF格式,后两者由于转码器由Apache
FOP提供)。 Batik工程创建的目的是为开发者提供一系列可以结合或单独使用来支持特殊的svg解决方案的核心模块。模块主要有SVGParser,SVGGernerator,SVGDOM。Batik工程的其他目的是使它具有高度的扩展性。
(SVG的规范:可缩放矢量图形(SVG),是一个W3C的推荐标准。 它定义了丰富的2D图形的XML语法,其中包括诸如透明度功能,几何形状,滤镜效果(阴影,灯光效果等),脚本和动画)
利用poi实现word转换html相关推荐
- poi处理word内容的公式_利用poi操作word文档
关键字:POI JAVA 批注 总页数 总字符数 一:认识POI Apache POI是一个开源的利用Java读写Excel.WORD等微软OLE2组件文档的项目.最新的3.5版本有很多改进,加入了对 ...
- 利用poi操作word文档(针对docx格式)
一:认识POI Apache POI是一个开源的利用Java读写Excel.WORD等微软OLE2组件文档的项目.最新的3.5版本有很多改进,加入了对采用OOXML格式的Office 2007支持, ...
- 利用poi读取word模板文件生成新的word文档
利用poi读取word模板文件生成新的word文档 利用poi读取word模板文件,并回填逻辑数据,生成并导出需要的word文档源码.解决模板读取异常问题,提供wordUtils工具类(各种功能实现) ...
- java word模板poi生成文件_利用poi读取word模板文件生成新的word文档
利用poi读取word模板文件生成新的word文档 利用poi读取word模板文件,并回填逻辑数据,生成并导出需要的word文档源码.解决模板读取异常问题,提供wordUtils工具类(各种功能实现) ...
- java利用POI替换word文档中的标签
java利用POI替换word文档中的标签 <dependency><groupId>org.apache.poi</groupId><artifactId& ...
- Java利用poi生成word(包含插入图片,动态表格,行合并)
Java利用poi生成word(包含插入图片,动态表格,行合并) 测试模板样式: 图表 1 Word生成结果: 图表 2 需要的jar包:(具体jar可自行去maven下载) Test测试类: imp ...
- java利用poi导出word文档
项目中,有时候需要使用poi实现将固定数据导入word中 效果图: 代码: package poiword;import java.io.File; import java.io.FileOutput ...
- 利用poi向word模板填充数据
java基于POI实现向word模板填充数据 在做项目的时候遇到需要将多张表单导出为word,就想到了这个方法. 注意:XWPFDocument不支持doc类型文档,做模板的时候要另存为docx. 示 ...
- poi 顺序解析word_利用POI读取word、Excel文件的最佳实践教程
前言 POI是 Apache 旗下一款读写微软家文档声名显赫的类库.应该很多人在做报表的导出,或者创建 word 文档以及读取之类的都是用过 POI.POI 也的确对于这些操作带来很大的便利性.我最近 ...
- java利用POI在word中绘制折线图
1.poi的简介 Apache POI 简介是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office(Excel.WORD ...
最新文章
- ADO.NET 2.0中的SqlCommand.ExecutePageReader
- 第十一届山东省大学生程序设计竞赛 L. Construction of 5G Base Stations(概率期望,递推前缀和优化)
- 【设计模式】原型模式 ( 概念简介 | 使用场景 | 优缺点 | 基本用法 )
- ubuntu部署git
- android 获取屏幕的宽高
- FPGA图像处理的开发流程
- 【ArcGIS Pro风暴】Data Interoperability Tools快速将CASS等高线dwg转为shp案例教程
- pandas groupby count_数据分析14-利用pandas进行数据分组
- 通用的类-可直接存储的Dictionary,可以被JSON或NSUserDefaults
- android 来电自动接听和自动挂断
- 如何短期通过2022年3月PMP考试?
- springboot activiti 7 和activiti 6 配置详解
- 一篇文章带你认识什么是数学建模
- 计算机网络共享自动关,启用网络发现,重新打开“高级共享设置”对话框,显示仍是关闭状态...
- Python open()函数详解:打开指定文件
- 自动驾驶上的三种感知传感器(激光、毫米波雷达和摄像头)优缺点比较
- 北京某公司IBM X3650M3存储崩溃的解决过程
- 金融级湖仓一体架构——SequoiaDB巨杉数据库初探
- scrapy 爬取链家二手房数据
- malloc()函数与free()函数的使用
热门文章
- Windows安全设置-当前的安全设置不允许从该位置下载文件
- 什么是软件危机?它有哪些典型表现?为什么会出现软件危机?
- Github删除历史提交记录的方法
- 高德网络定位之“移动WiFi识别”
- BugkuCTF:宽带泄露;隐写2
- 论文阅读:SPR:Supervised Personalized Ranking Based on Prior Knowledge for Recommendation
- 【013】故宫博物院数字文物库-让文物随时可赏
- K-mer频率分布图代码实现
- 【数字电路抢救】2 逻辑函数的代数化简 3逻辑表达式的化简
- Ubuntu配置静态IP以及interfaces配置不生效问题解决