Search in sources :

Example 21 with WordprocessingMLPackage

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

Example 22 with WordprocessingMLPackage

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;
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 23 with WordprocessingMLPackage

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

Example 24 with WordprocessingMLPackage

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

Example 25 with WordprocessingMLPackage

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

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