use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class WMLPackageUtils method mergeDocx.
public static InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException, IOException {
WordprocessingMLPackage target = null;
final File generated = File.createTempFile("generated", ".docx");
int chunkId = 0;
Iterator<InputStream> it = streams.iterator();
while (it.hasNext()) {
InputStream is = it.next();
if (is != null) {
if (target == null) {
// Copy first (master) document
OutputStream os = new FileOutputStream(generated);
os.write(IOUtils.toByteArray(is));
os.close();
target = WordprocessingMLPackage.load(generated);
} else {
// Attach the others (Alternative input parts)
insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(is), chunkId++);
}
}
}
if (target != null) {
target.save(generated);
return new FileInputStream(generated);
} else {
return null;
}
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class XHTMLImporterUtils method handle.
public static WordprocessingMLPackage handle(WordprocessingMLPackage wmlPackage, Document doc, boolean fragment, boolean altChunk) throws IOException, Docx4JException {
// 设置转换模式
// 转为 xhtml 格式
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
if (altChunk) {
// Document对象
MainDocumentPart document = wmlPackage.getMainDocumentPart();
// 获取Jsoup参数
String charsetName = Docx4jProperties.getProperty(Docx4jConstants.DOCX4J_JSOUP_PARSE_CHARSETNAME, Docx4jConstants.DEFAULT_CHARSETNAME);
// 设置转换模式
// 转为 xhtml 格式
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.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;
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class WordprocessingMLTemplateWriter method writeToString.
public String writeToString(File docFile) throws IOException, Docx4JException {
WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(docFile);
StringBuilderWriter output = new StringBuilderWriter();
try {
this.writeToWriter(wmlPackage, output);
} finally {
IOUtils.closeQuietly(output);
}
return output.toString();
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4jUtils method docxToHtml.
/**
* 把docx转成html
* @param docxFilePath
* @param htmlPath
* @throws Exception
*/
public static void docxToHtml(String docxFilePath, String htmlPath) throws Exception {
OutputStream output = null;
try {
//
WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new File(docxFilePath));
WMLPACKAGE_BUILDER.configChineseFonts(wmlPackage).configSimSunFont(wmlPackage);
WMLPACKAGE_WRITER.writeToHtml(wmlPackage, htmlPath);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
IOUtils.closeQuietly(output);
}
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Docx4jUtils method mergeDocx.
public InputStream mergeDocx(final List<InputStream> streams) throws Docx4JException, IOException {
WordprocessingMLPackage target = null;
final File generated = File.createTempFile("generated", ".docx");
int chunkId = 0;
Iterator<InputStream> it = streams.iterator();
while (it.hasNext()) {
InputStream is = it.next();
if (is != null) {
if (target == null) {
// Copy first (master) document
OutputStream os = new FileOutputStream(generated);
os.write(IOUtils.toByteArray(is));
os.close();
target = WordprocessingMLPackage.load(generated);
} else {
// Attach the others (Alternative input parts)
insertDocx(target.getMainDocumentPart(), IOUtils.toByteArray(is), chunkId++);
}
}
}
if (target != null) {
target.save(generated);
return new FileInputStream(generated);
} else {
return null;
}
}
Aggregations