Search in sources :

Example 16 with WordprocessingMLPackage

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;
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) AlternativeFormatInputPart(org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart) Document(org.jsoup.nodes.Document) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Body(org.docx4j.wml.Body) File(java.io.File)

Example 17 with WordprocessingMLPackage

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");
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 18 with WordprocessingMLPackage

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);
}
Also used : UnzippedPartStore(org.docx4j.openpackaging.io3.stores.UnzippedPartStore) Save(org.docx4j.openpackaging.io3.Save) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) File(java.io.File)

Example 19 with WordprocessingMLPackage

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();
}
Also used : StringBuilderWriter(org.apache.commons.io.output.StringBuilderWriter) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 20 with WordprocessingMLPackage

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"));
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) File(java.io.File)

Aggregations

WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)67 File (java.io.File)50 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)22 Docx4JException (org.docx4j.openpackaging.exceptions.Docx4JException)11 XHTMLImporterImpl (org.docx4j.convert.in.xhtml.XHTMLImporterImpl)9 OutputStream (java.io.OutputStream)8 ObjectFactory (org.docx4j.wml.ObjectFactory)8 Test (org.junit.Test)8 P (org.docx4j.wml.P)7 Text (org.docx4j.wml.Text)7 FileOutputStream (java.io.FileOutputStream)6 FileInputStream (java.io.FileInputStream)5 Body (org.docx4j.wml.Body)5 Tbl (org.docx4j.wml.Tbl)5 TraversalUtil (org.docx4j.TraversalUtil)4 NumberingDefinitionsPart (org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart)4 R (org.docx4j.wml.R)4 RPr (org.docx4j.wml.RPr)4 SpecExample (com.vladsch.flexmark.spec.SpecExample)3 IOException (java.io.IOException)3