use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class HtmlToDOCDemo method main.
public static void main(String[] args) throws Exception {
// 加载模板
InputStream template = HtmlToDOCDemo.class.getResourceAsStream("kcjj.xml");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(template);
PhysicalFontUtils.setSimSunFont(wordMLPackage);
// 加载Html数据
String jxdg = IOUtils.toString(HtmlToDOCDemo.class.getResourceAsStream("data.txt"));
Map<String, String> richTextMap = new HashMap<String, String>();
richTextMap.put("jxdg", jxdg);
replaceRichText(wordMLPackage, richTextMap);
File file = new File("d://result4.docx");
wordMLPackage.save(file);
System.out.println("success");
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class Word_解压_Unzip_S3_Test2 method unzipWord.
public void unzipWord(String fileName, String outFilePath) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(fileName));
File baseDir = new File(outFilePath);
baseDir.mkdir();
UnzippedPartStore ups = new UnzippedPartStore(baseDir);
ups.setSourcePartStore(wordMLPackage.getSourcePartStore());
Save saver = new Save(wordMLPackage, ups);
saver.save(null);
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class WordprocessingMLPackageExtractor method extract.
public String extract(File inputfile) throws Exception {
WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(inputfile);
StringBuilderWriter output = new StringBuilderWriter();
try {
this.extract(wmlPackage, output);
} finally {
IOUtils.closeQuietly(output);
}
return output.toString();
}
use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.
the class AddingAnInlineImage method main.
/**
* 像往常一样, 我们创建了一个包(package)来容纳文档.
* 然后我们创建了一个指向将要添加到文档的图片的文件对象.为了能够对图片做一些操作, 我们将它转换
* 为字节数组. 最后我们将图片添加到包中并保存这个包(package).
*/
public static void main(String[] args) throws Exception {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
File file = new File("src/main/resources/iProfsLogo.png");
byte[] bytes = convertImageToByteArray(file);
addImageToPackage(wordMLPackage, bytes);
wordMLPackage.save(new java.io.File("src/main/files/HelloWord7.docx"));
}
Aggregations