Search in sources :

Example 1 with PicturesManager

use of org.apache.poi.hwpf.converter.PicturesManager in project wechat by dllwh.

the class WordUtil method convertHtmlByWord2003.

/**
 * @方法描述: 将word2003转换为html文件
 * @param sourceFile
 *            源word文件路径
 * @param parentPath
 *            目标文件路径
 * @param saveFileName
 *            目标文件名称
 * @param charsetName
 *            编码
 * @return
 * @throws Exception
 */
public static boolean convertHtmlByWord2003(String sourceFile, String parentPath, String saveFileName, String encode) throws Exception {
    if (StringUtils.isBlank(encode)) {
        encode = "UTF-8";
    }
    File imgPath = new File(parentPath);
    if (!imgPath.exists()) {
        // 图片目录不存在则创建
        imgPath.mkdirs();
    }
    // 创建一个文档
    HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(sourceFile));
    // 对普通文本的操作
    WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
    // 对图片的操作: 图片在html文件上的相对路径
    wordToHtmlConverter.setPicturesManager(new PicturesManager() {

        public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
            return suggestedName;
        }
    });
    // 保存图片
    List<Picture> 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(parentPath + pic.suggestFullFileName()));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    // 解析word文档
    wordToHtmlConverter.processDocument(wordDocument);
    Document htmlDocument = wordToHtmlConverter.getDocument();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    DOMSource domSource = new DOMSource(htmlDocument);
    StreamResult streamResult = new StreamResult(output);
    // 下面都是转换
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer serializer = tf.newTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, encode);
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "html");
    serializer.transform(domSource, streamResult);
    // 调用writeFile类
    writeFile(new String(output.toByteArray()), parentPath + File.separator + saveFileName, encode);
    IOUtils.closeQuietly(output);
    return false;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) PicturesManager(org.apache.poi.hwpf.converter.PicturesManager) WordToHtmlConverter(org.apache.poi.hwpf.converter.WordToHtmlConverter) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Picture(org.apache.poi.hwpf.usermodel.Picture) PictureType(org.apache.poi.hwpf.usermodel.PictureType) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 2 with PicturesManager

use of org.apache.poi.hwpf.converter.PicturesManager in project portal by ixinportal.

the class WordToHtmlTest method convert2Html.

// word 转 html
public static void convert2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException {
    HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(// WordToHtmlUtils.loadDoc(new
    fileName));
    // FileInputStream(inputFile));
    // 兼容2007 以上版本
    // XSSFWorkbook xssfwork=new XSSFWorkbook(new
    // FileInputStream(fileName));
    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);
            System.out.println();
            try {
                pic.writeImageContent(new FileOutputStream("D:/test/" + pic.suggestFullFileName()));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    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, "UTF-8");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.METHOD, "HTML");
    serializer.transform(domSource, streamResult);
    out.close();
    writeFile(new String(out.toByteArray()), outPutFile);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Document(org.w3c.dom.Document) PicturesManager(org.apache.poi.hwpf.converter.PicturesManager) WordToHtmlConverter(org.apache.poi.hwpf.converter.WordToHtmlConverter) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Picture(org.apache.poi.hwpf.usermodel.Picture) PictureType(org.apache.poi.hwpf.usermodel.PictureType) List(java.util.List)

Aggregations

Transformer (javax.xml.transform.Transformer)2 TransformerFactory (javax.xml.transform.TransformerFactory)2 DOMSource (javax.xml.transform.dom.DOMSource)2 StreamResult (javax.xml.transform.stream.StreamResult)2 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)2 PicturesManager (org.apache.poi.hwpf.converter.PicturesManager)2 WordToHtmlConverter (org.apache.poi.hwpf.converter.WordToHtmlConverter)2 Picture (org.apache.poi.hwpf.usermodel.Picture)2 PictureType (org.apache.poi.hwpf.usermodel.PictureType)2 Document (org.w3c.dom.Document)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 List (java.util.List)1 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)1