package bingosoft.metro.sendfile.utils;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.converter.core.BasicURIResolver;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.List;
public class HtmlGenerator{
public void wordToHtmlByDoc(String path,String fileName) throws Exception{
// WordprocessingMLPackage wordMLPackage= Docx4J.load(new File(path+fileName));
//
// HTMLSettings htmlSettings = Docx4J.createHTMLSettings();
// String imageFilePath="D:\\print\\";
// htmlSettings.setImageDirPath(imageFilePath);
// htmlSettings.setImageTargetUri( "images");
// htmlSettings.setWmlPackage(wordMLPackage);
//
// String userCSS = "html, body, div, span, h1, h2, h3, h4, h5, h6, p, a, img, ol, ul, li, table, caption, tbody, tfoot, thead, tr, th, td " +
// "{ margin: 0; padding: 0; border: 0;}" +
// "body {line-height: 1;} ";
//
// htmlSettings.setUserCSS(userCSS);
//
// OutputStream os;
//
// os = new FileOutputStream("D:\\print\\");
//
// Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true);
//
// Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
}
public static void wordToHtml(String path,String fileName) throws Exception{
File file = new File(path+fileName);
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//处理2007版本
if(file.getName().endsWith(".docx")||file.getName().endsWith(".DOCX")){
XWPFDocument xwpfDocument = new XWPFDocument(bufferedInputStream);
System.out.println(xwpfDocument.getTables());
File imageFolderFile = new File(path);
XHTMLOptions options = XHTMLOptions.create().URIResolver( new BasicURIResolver("./"));
options.setExtractor(new FileImageExtractor(imageFolderFile));
OutputStream out = new FileOutputStream(new File(path+fileName.substring(0,fileName.indexOf(".")+1)+"html"));
XHTMLConverter.getInstance().convert(xwpfDocument, out, options);
out.close();
}
else{
//处理2003版本
HWPFDocument hwpfDocument = new HWPFDocument(new POIFSFileSystem(file,false));
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
);
wordToHtmlConverter.setPicturesManager(new PicturesManager() {
@Override
public String savePicture(byte[] bytes, PictureType pictureType, String s, float v, float v1) {
return s;
}
});
wordToHtmlConverter.processDocument(hwpfDocument);
List pics = hwpfDocument.getPicturesTable().getAllPictures();
if(pics != null){
for(int i=0;i<pics.size();i++){
Picture pic = (Picture) pics.get(i);
try{
pic.writeImageContent(new FileOutputStream(path+pic.suggestFullFileName()));
}catch(Exception 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());
System.out.println(fileName.substring(0,fileName.indexOf(".")+1));
FileUtils.writeStringToFile(new File(path, fileName.substring(0,fileName.indexOf(".")+1)+"html"), content, "utf-8");
}
}
}