Example usage for org.apache.poi.hwpf.usermodel Picture suggestFullFileName

List of usage examples for org.apache.poi.hwpf.usermodel Picture suggestFullFileName

Introduction

In this page you can find the example usage for org.apache.poi.hwpf.usermodel Picture suggestFullFileName.

Prototype

public String suggestFullFileName() 

Source Link

Document

Tries to suggest a filename: hex representation of picture structure offset in "Data" stream plus extension that is tried to determine from first byte of picture's content.

Usage

From source file:javaapplication1.utils.MyWordToHtml.java

public static void convert(String path, String file) throws Throwable {
    InputStream input = new FileInputStream(path + file);
    HWPFDocument wordDocument = new HWPFDocument(input);
    WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
            DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
    MyPictureManager pictureManager = new MyPictureManager();
    wordToHtmlConverter.setPicturesManager(pictureManager);

    wordToHtmlConverter.processDocument(wordDocument);
    List<?> pics = wordDocument.getPicturesTable().getAllPictures();
    File dir = new File("D:\\pics");
    dir.mkdir();/*from   ww w  .j  ava  2  s  . c  o m*/
    if (pics != null) {
        for (int i = 0; i < pics.size(); i++) {
            Picture pic = (Picture) pics.get(i);
            try {
                pic.writeImageContent(new FileOutputStream(path + "pics/" + pic.suggestFullFileName()));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    Document htmlDocument = wordToHtmlConverter.getDocument();
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    DOMSource domSource = new DOMSource(htmlDocument);
    StreamResult streamResult = new StreamResult(outStream);

    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);
    outStream.close();

    String content = new String(outStream.toByteArray());

    writeFile(content, path + "result.html", "UTF-8");
}