Search in sources :

Example 6 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.

the class Docx4j_创建批注_S3_Test method main.

public static void main(String[] args) throws Exception {
    Docx4j_创建批注_S3_Test t = new Docx4j_创建批注_S3_Test();
    WordprocessingMLPackage wordMLPackage = t.createWordprocessingMLPackage();
    MainDocumentPart mp = wordMLPackage.getMainDocumentPart();
    ObjectFactory factory = Context.getWmlObjectFactory();
    t.testCreateComment(wordMLPackage, mp, factory);
    t.saveWordPackage(wordMLPackage, new File("f:/saveFile/temp/sys_" + System.currentTimeMillis() + ".docx"));
}
Also used : ObjectFactory(org.docx4j.wml.ObjectFactory) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File)

Example 7 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method setDocumentBackGround.

/**
 * @Description: 设置页面背景色
 */
public void setDocumentBackGround(WordprocessingMLPackage wordPackage, ObjectFactory factory, String color) throws Exception {
    MainDocumentPart mdp = wordPackage.getMainDocumentPart();
    CTBackground bkground = mdp.getContents().getBackground();
    if (StringUtils.isNotBlank(color)) {
        if (bkground == null) {
            bkground = factory.createCTBackground();
            bkground.setColor(color);
        }
        mdp.getContents().setBackground(bkground);
    }
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CTBackground(org.docx4j.wml.CTBackground)

Example 8 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.

the class Docx4j_替换模板 method replaceTemplateDocx.

public void replaceTemplateDocx() throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    HashMap<String, String> staticMap = getStaticData();
    // 替换普通变量
    documentPart.variableReplace(staticMap);
    Document document = (Document) documentPart.getContents();
    Body body = document.getBody();
    MyTblFinder tblFinder = new MyTblFinder();
    new TraversalUtil(body, tblFinder);
    // 替换表格模板第三行数据
    Tbl firstTbl = tblFinder.getTbls().get(0);
    List<Object> trObjList = firstTbl.getContent();
    Tr tr = (Tr) trObjList.get(2);
    replaceTrTotalData(tr, getTotalData());
    int lvlIndex = 3;
    // 替换表格模板第四行数据
    int lvlTotalSize = 4;
    tr = (Tr) trObjList.get(lvlIndex);
    List<String[]> lvDataList = getLvDataList(lvlTotalSize);
    replaceTrData(firstTbl, tr, lvDataList, lvlIndex);
    // 重新获取表格数据
    trObjList = firstTbl.getContent();
    int sexTotalSize = 2;
    // 替换表格模板第五行数据
    tr = (Tr) trObjList.get(lvlIndex + lvlTotalSize);
    List<String[]> sexDataList = getSexDataList(2);
    replaceTrSexData(tr, sexDataList.get(0));
    // 替换表格模板第六行数据
    tr = (Tr) trObjList.get(4 + lvlTotalSize);
    replaceTrSexData(tr, sexDataList.get(1));
    // 替换表格模板第七行数据
    tr = (Tr) trObjList.get(5 + lvlTotalSize);
    int nationTotalSize = 56;
    List<String[]> nationDataList = getNationDataList(nationTotalSize);
    replaceTrData(firstTbl, tr, nationDataList, lvlIndex + lvlTotalSize + sexTotalSize);
    // 合并层次单元格 层次位于第3行
    mergeCellsVertically(firstTbl, 0, lvlIndex, lvlIndex + lvlTotalSize - 1);
    // 合并民族单元格 层次+性别2行
    mergeCellsVertically(firstTbl, 0, lvlIndex + lvlTotalSize + sexTotalSize, lvlIndex + lvlTotalSize + sexTotalSize + nationTotalSize - 1);
    // 替换图表数据
    String[] chartArr = getChartData();
    replacePieChartData(wordMLPackage, chartArr);
    // 保存结果
    saveWordPackage(wordMLPackage, outputfilepath);
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) Document(org.docx4j.wml.Document) File(java.io.File) TraversalUtil(org.docx4j.TraversalUtil) MyTblFinder(org.docx4j.template.wml.MyTblFinder) Body(org.docx4j.wml.Body) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 9 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project docx4j-template by vindell.

the class SdtFinder 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 sdtFinder = new SdtFinder();
    new TraversalUtil(body, sdtFinder);
    for (Child sdtChild : sdtFinder.sdtList) {
        if (sdtChild instanceof SdtRun) {
            SdtRun sdtRun = (SdtRun) sdtChild;
            SdtPr sdtPr = sdtRun.getSdtPr();
            printSdtPrContent(sdtPr);
            // System.out.println(XmlUtils.marshaltoString(sdtPr, true,
            // true));
            SdtContent sdtContent = sdtRun.getSdtContent();
            System.out.println("-----------p content=" + getContentAccessorContent(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=" + getContentAccessorContent(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=" + getContentAccessorContent(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 10 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart 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)

Aggregations

MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)36 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)22 File (java.io.File)19 P (org.docx4j.wml.P)13 ObjectFactory (org.docx4j.wml.ObjectFactory)7 Text (org.docx4j.wml.Text)7 Body (org.docx4j.wml.Body)5 Br (org.docx4j.wml.Br)5 R (org.docx4j.wml.R)5 RPr (org.docx4j.wml.RPr)5 Tbl (org.docx4j.wml.Tbl)5 TraversalUtil (org.docx4j.TraversalUtil)4 Relationship (org.docx4j.relationships.Relationship)4 Tr (org.docx4j.wml.Tr)4 Tc (org.docx4j.wml.Tc)3 Child (org.jvnet.jaxb2_commons.ppp.Child)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2