Search in sources :

Example 1 with PictureType

use of org.apache.poi.hwpf.usermodel.PictureType 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 PictureType

use of org.apache.poi.hwpf.usermodel.PictureType in project poi by apache.

the class AbstractWordConverter method processDrawnObject.

protected void processDrawnObject(HWPFDocument doc, CharacterRun characterRun, Element block) {
    if (getPicturesManager() == null)
        return;
    // TODO: support headers
    OfficeDrawing officeDrawing = doc.getOfficeDrawingsMain().getOfficeDrawingAt(characterRun.getStartOffset());
    if (officeDrawing == null) {
        logger.log(POILogger.WARN, "Characters #" + characterRun + " references missing drawn object");
        return;
    }
    byte[] pictureData = officeDrawing.getPictureData();
    if (pictureData == null)
        // usual shape?
        return;
    float width = (officeDrawing.getRectangleRight() - officeDrawing.getRectangleLeft()) / AbstractWordUtils.TWIPS_PER_INCH;
    float height = (officeDrawing.getRectangleBottom() - officeDrawing.getRectangleTop()) / AbstractWordUtils.TWIPS_PER_INCH;
    final PictureType type = PictureType.findMatchingType(pictureData);
    String path = getPicturesManager().savePicture(pictureData, type, "s" + characterRun.getStartOffset() + "." + type, width, height);
    processDrawnObject(doc, characterRun, officeDrawing, path, block);
}
Also used : OfficeDrawing(org.apache.poi.hwpf.usermodel.OfficeDrawing) PictureType(org.apache.poi.hwpf.usermodel.PictureType)

Example 3 with PictureType

use of org.apache.poi.hwpf.usermodel.PictureType in project poi by apache.

the class TestWordToHtmlConverter method getHtmlText.

private static String getHtmlText(final String sampleFileName, boolean emulatePictureStorage) throws Exception {
    HWPFDocument hwpfDocument = new HWPFDocument(POIDataSamples.getDocumentInstance().openResourceAsStream(sampleFileName));
    Document newDocument = XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument();
    WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument);
    if (emulatePictureStorage) {
        wordToHtmlConverter.setPicturesManager(new PicturesManager() {

            @Override
            public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) {
                return suggestedName;
            }
        });
    }
    wordToHtmlConverter.processDocument(hwpfDocument);
    StringWriter stringWriter = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.setOutputProperty(OutputKeys.METHOD, "html");
    transformer.transform(new DOMSource(wordToHtmlConverter.getDocument()), new StreamResult(stringWriter));
    return stringWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) Document(org.w3c.dom.Document) HWPFDocument(org.apache.poi.hwpf.HWPFDocument) StringWriter(java.io.StringWriter) PictureType(org.apache.poi.hwpf.usermodel.PictureType)

Example 4 with PictureType

use of org.apache.poi.hwpf.usermodel.PictureType 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

PictureType (org.apache.poi.hwpf.usermodel.PictureType)4 Transformer (javax.xml.transform.Transformer)3 DOMSource (javax.xml.transform.dom.DOMSource)3 StreamResult (javax.xml.transform.stream.StreamResult)3 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)3 Document (org.w3c.dom.Document)3 TransformerFactory (javax.xml.transform.TransformerFactory)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 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 StringWriter (java.io.StringWriter)1 List (java.util.List)1 OfficeDrawing (org.apache.poi.hwpf.usermodel.OfficeDrawing)1 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)1