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;
}
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);
}
Aggregations