Search in sources :

Example 56 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.

the class WordprocessingMLVelocityTemplate_Test method test.

@Test
public void test() throws Exception {
    variables.put("title", "变量替换测试");
    variables.put("content", "测试效果不错");
    WordprocessingMLPackage wordMLPackage = velocityTemplate.process("/tpl/rythm.tpl", variables);
    File outputDocx = new java.io.File("src/test/resources/output/velocityTemplate_output.docx");
    wordMLPackage.save(outputDocx);
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) Test(org.junit.Test)

Example 57 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.

the class WordprocessingMLWebitTemplate_Test method test.

@Test
public void test() throws Exception {
    variables.put("title", "变量替换测试");
    variables.put("content", "测试效果不错");
    WordprocessingMLPackage wordMLPackage = webitTemplate.process("/tpl/webit.tpl", variables);
    File outputDocx = new java.io.File("src/test/resources/output/webitTemplate_output.docx");
    wordMLPackage.save(outputDocx);
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) Test(org.junit.Test)

Example 58 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method loadWordprocessingMLPackageWithPwd.

/**
 * @Description:加载带密码WordprocessingMLPackage
 */
public WordprocessingMLPackage loadWordprocessingMLPackageWithPwd(String filePath, String password) throws Exception {
    OpcPackage opcPackage = WordprocessingMLPackage.load(new java.io.File(filePath), password);
    WordprocessingMLPackage wordMLPackage = (WordprocessingMLPackage) opcPackage;
    return wordMLPackage;
}
Also used : OpcPackage(org.docx4j.openpackaging.packages.OpcPackage) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 59 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.

the class SdtFinder_2 method printSdtContent.

public void printSdtContent(String filePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    Document wmlDocumentEl = (Document) mdp.getContents();
    Body body = wmlDocumentEl.getBody();
    SdtFinder_2 sdtFinder = new SdtFinder_2();
    new TraversalUtil(body, sdtFinder);
    for (Child sdtChild : sdtFinder.sdtList) {
        if (sdtChild instanceof SdtRun) {
            SdtRun sdtRun = (SdtRun) sdtChild;
            SdtPr sdtPr = sdtRun.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtRun.getSdtContent();
            System.out.println("-----------p content=" + getSdtContentContent(sdtContent));
        } else if (sdtChild instanceof CTSdtCell) {
            CTSdtCell sdtCell = (CTSdtCell) sdtChild;
            SdtPr sdtPr = sdtCell.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtCell.getSdtContent();
            System.out.println("-----------table content=" + getSdtContentContent(sdtContent));
        } else if (sdtChild instanceof SdtBlock) {
            SdtBlock sdtBlock = (SdtBlock) sdtChild;
            SdtPr sdtPr = sdtBlock.getSdtPr();
            printSdtPrContent(sdtPr);
            SdtContent sdtContent = sdtBlock.getSdtContent();
            System.out.println("-----------sdtblock content=" + getSdtContentContent(sdtContent));
        }
    }
}
Also used : SdtPr(org.docx4j.wml.SdtPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Document(org.docx4j.wml.Document) CTSdtCell(org.docx4j.wml.CTSdtCell) TraversalUtil(org.docx4j.TraversalUtil) SdtRun(org.docx4j.wml.SdtRun) SdtContent(org.docx4j.wml.SdtContent) File(java.io.File) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) SdtBlock(org.docx4j.wml.SdtBlock)

Example 60 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage in project docx4j-template by vindell.

the class HtmlConverter method xhtml2word.

/**
 * 将 {@link org.jsoup.nodes.Document} 对象转为 {@link org.docx4j.openpackaging.packages.WordprocessingMLPackage}
 * xhtml to word
 *
 * @param doc
 * @return
 * @throws Exception
 */
protected WordprocessingMLPackage xhtml2word(Document doc) throws Exception {
    // A4纸,//横版:true
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.valueOf("A4"), true);
    // 配置中文字体
    configSimSunFont(wordMLPackage);
    XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
    // 导入 xhtml
    wordMLPackage.getMainDocumentPart().getContent().addAll(xhtmlImporter.convert(doc.html(), doc.baseUri()));
    return wordMLPackage;
}
Also used : XHTMLImporterImpl(org.docx4j.convert.in.xhtml.XHTMLImporterImpl) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

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