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