将word文档转换为html、PDF等

时间:2022-11-11 21:57:53
在日常工作中我们常常要把数据导入word后,在做打印功能,一般打印在前台做的话会比在后台做客户体验更好一些,这个时候交给前台最好是html、pdf、或图片格式的数据,我的另一篇博客中讲解了怎么将PDF转换成图片,并且可以调整清晰度。
这些方法都是我在工作学习中在网络上借鉴各位前辈的经验代码,总结而来,给大家提供个方便,如有侵权请联系博主删除,谢谢。

一、 将word转化为html
依赖jar包
将word文档转换为html、PDF等

/** * @Title convert2Html * @description 生成合同 * @param src file 读取的文件 * @param outPutFile File 输出的文件 * @param encoding String * @return String */
           public static String convert2Html(File src, File outPutFile, String encoding)  
                   throws TransformerException, IOException,  
                   ParserConfigurationException {  
               HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(src));  
               WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(  
                       DocumentBuilderFactory.newInstance().newDocumentBuilder()  
                               .newDocument());  
               /**************图片 有可能出现合同章等图片 如果确定不会出现图片以下不需要考虑************/
                wordToHtmlConverter.setPicturesManager( new PicturesManager()  
                {  
                    public String savePicture( byte[] content,  
                            PictureType pictureType, String suggestedName,  
                            float widthInches, float heightInches )  
                    {  
                        return "test/"+suggestedName;  //保存图片的目录
                    }  
                } );  
               wordToHtmlConverter.processDocument(wordDocument);  
               //save pictures 
               List pics=wordDocument.getPicturesTable().getAllPictures();  
               if(pics!=null){  
                   for(int i=0;i<pics.size();i++){  
                       Picture pic = (Picture)pics.get(i);  
                       try {  
                           pic.writeImageContent(new FileOutputStream("test/"  
                                   + pic.suggestFullFileName()));  
                       } catch (FileNotFoundException e) {  
                           e.printStackTrace();  
                       }    
                   }  
               }  
               /***********************************end 图片********************/

               Document htmlDocument = wordToHtmlConverter.getDocument();  
               ByteArrayOutputStream out = new ByteArrayOutputStream();  
               DOMSource domSource = new DOMSource(htmlDocument);  
               StreamResult streamResult = new StreamResult(out);  

               TransformerFactory tf = TransformerFactory.newInstance();  
               Transformer serializer = tf.newTransformer();  
               //serializer.setOutputProperty(OutputKeys.ENCODING, "gbk"); 
               serializer.setOutputProperty(OutputKeys.INDENT, "yes");  
               serializer.setOutputProperty(OutputKeys.METHOD, "html");  
               serializer.transform(domSource, streamResult);  
               out.close();  
               String content ="";// "<%@ page pageEncoding=\"gbk\"%>";
               content += new String(out.toByteArray());
               content=content.replace("<head>", "<head><title>"
                    + "P2P互联网居间平台</title><link rel=\"shortcut icon\" href=\"favicon.ico\" type"
                    + "=\"image/x-icon\"/>");
               writeFile(content, outPutFile);  
               return content;
           }  

/** * @Title writeFile * @description 使用输出流打印一份文件 * @param content 文件内容 * @param file 输出的文件路径 */
               public static void writeFile(String content, File file) {  
                   FileOutputStream fos = null;  
                   BufferedWriter bw = null;  
                   try {  
                       fos = new FileOutputStream(file);  
                       bw = new BufferedWriter(new OutputStreamWriter(fos));  
                       bw.write(content);  
                   } 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) {  
                       }  
                   }  
               }

二、将word转换成PDF
依赖jar包
将word文档转换为html、PDF等

/** * word转为PDF */
           public synchronized static void doc2pdf(String orgAddr,String newAdd) {

                if (!getLicense()) { //验证License 若不验证则转化出的pdf文档会有水印产生
                    return;
                }
                try {
                    long old = System.currentTimeMillis();
                    File file = new File(newAdd); // 新建一个空白pdf文档
                    FileOutputStream os = new FileOutputStream(file);
                    com.aspose.words.Document doc = new com.aspose.words.Document(orgAddr); // Address是将要被转化的word文档
                    doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,
                                                    // OpenDocument, PDF, EPUB, XPS, SWF
                                                    // 相互转换
                    long now = System.currentTimeMillis();
                    System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

           /** * 验证License 若不验证则转化出的pdf文档会有水印产生 * @return */
           public static boolean getLicense() {
                boolean result = false;
                try {
                String iss =  WordUtil.class.getClassLoader().getResource("//").getPath()+"PgPubk.key";
                    InputStream is = ReadLicense.getStringInputStream();
                             //WordUtil.class.getClassLoader().getResourceAsStream("license.xml");// 
                    System.out.println("PATH: ");
                    License aposeLic = new License();
                    aposeLic.setLicense(is);
                    result = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }

用这个编辑器,代码格式不好调整,看着怪怪的,大家凑合着看吧。