use of org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart in project docx4j-template by vindell.
the class HtmlToDOCDemo method convertToWmlObject.
private static List<Object> convertToWmlObject(WordprocessingMLPackage wordMLPackage, String content) throws Docx4JException, JAXBException {
MainDocumentPart document = wordMLPackage.getMainDocumentPart();
// 获取Jsoup参数
String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_CONVERT_OUT_WMLTEMPLATE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME);
List<Object> wmlObjList = null;
String templateString = XmlUtils.marshaltoString(document.getContents().getBody());
System.out.println(templateString);
Body templateBody = document.getContents().getBody();
try {
document.getContents().setBody(XmlUtils.deepCopy(templateBody));
document.getContent().clear();
Document doc = Jsoup.parse(content);
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
// XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
AlternativeFormatInputPart part = document.addAltChunk(AltChunkType.Xhtml, doc.html().getBytes(Charset.forName(charsetName)));
WordprocessingMLPackage tempPackage = document.convertAltChunks();
File file = new File("d://temp.docx");
tempPackage.save(file);
wmlObjList = document.getContent();
// part.getOwningRelationshipPart().getSourceP().get
// wmlObjList = xhtmlImporter.convert(doc.html(), doc.baseUri());
} finally {
document.getContents().setBody(templateBody);
}
return wmlObjList;
}
use of org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart in project docx4j-template by vindell.
the class WMLPackageUtils method insertDocx.
// 插入文档
private static void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
try {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));
afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setBinaryData(bytes);
Relationship altChunkRel = main.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
main.addObject(chunk);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart in project docx4j-template by vindell.
the class Docx4jUtils method insertDocx.
// 插入文档
private void insertDocx(MainDocumentPart main, byte[] bytes, int chunkId) {
try {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/part" + chunkId + ".docx"));
// afiPart.setContentType(new ContentType(CONTENT_TYPE));
afiPart.setContentType(new ContentType(ContentTypes.APPLICATION_XML));
afiPart.setBinaryData(bytes);
Relationship altChunkRel = main.addTargetPart(afiPart);
CTAltChunk chunk = Context.getWmlObjectFactory().createCTAltChunk();
chunk.setId(altChunkRel.getId());
main.addObject(chunk);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations