org.docx4j.convert.in.xhtml.XHTMLImporterImpl Java Examples

The following examples show how to use org.docx4j.convert.in.xhtml.XHTMLImporterImpl. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XHTMLImporterUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public static WordprocessingMLPackage handle(WordprocessingMLPackage wmlPackage, Document doc,boolean fragment,boolean altChunk) throws IOException, Docx4JException {
	//设置转换模式
	doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);  //转为 xhtml 格式
	
	if(altChunk){
		//Document对象
		MainDocumentPart document = wmlPackage.getMainDocumentPart();
		//获取Jsoup参数
		String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_JSOUP_PARSE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME );
		//设置转换模式
		doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);  //转为 xhtml 格式
		//创建html导入对象
		//XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
		document.addAltChunk(AltChunkType.Xhtml, (fragment ? doc.body().html() : doc.html()) .getBytes(Charset.forName(charsetName)));
		//document.addAltChunk(type, bytes, attachmentPoint)
		//document.addAltChunk(type, is)
		//document.addAltChunk(type, is, attachmentPoint)
		WordprocessingMLPackage tempPackage = document.convertAltChunks();
		
		//返回处理后的WordprocessingMLPackage对象
		return tempPackage;
	}
	
	//创建html导入对象
	XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wmlPackage);
	//将xhtml转换为wmlPackage可用的对象
	List<Object> list = xhtmlImporter.convert((fragment ? doc.body().html() : doc.html()), doc.baseUri());
	//导入转换后的内容对象
	wmlPackage.getMainDocumentPart().getContent().addAll(list);
	//返回原WordprocessingMLPackage对象
	return wmlPackage;
}
 
Example #2
Source File: HtmlConverter.java    From docx4j-template with Apache License 2.0 3 votes vote down vote up
/**
 * 将 {@link org.jsoup.nodes.Document} 对象转为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage}
 * xhtml to word
 *
 * @param doc
 * @return
 * @throws Exception
 */
protected WordprocessingMLPackage xhtml2word(Document doc) throws Exception {

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.valueOf("A4"), true); //A4纸,//横版:true

    configSimSunFont(wordMLPackage); //配置中文字体

    XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);

    wordMLPackage.getMainDocumentPart().getContent().addAll( //导入 xhtml
            xhtmlImporter.convert(doc.html(), doc.baseUri()));


    return wordMLPackage;
}