Search in sources :

Example 31 with MainDocumentPart

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

the class AddingAPageBreak method addPageBreak.

/**
 * 向文档添加一个换行符
 */
private static void addPageBreak() {
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    Br breakObj = new Br();
    breakObj.setType(STBrType.PAGE);
    P paragraph = factory.createP();
    paragraph.getContent().add(breakObj);
    documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}
Also used : Br(org.docx4j.wml.Br) P(org.docx4j.wml.P) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)

Example 32 with MainDocumentPart

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

the class WordprocessingMLTemplateWriter method writeToStream.

public static void writeToStream(WordprocessingMLPackage wmlPackage, OutputStream output) throws IOException, Docx4JException {
    Assert.notNull(wmlPackage, " wmlPackage is not specified!");
    Assert.notNull(output, " output is not specified!");
    InputStream input = null;
    try {
        // Document对象
        MainDocumentPart document = wmlPackage.getMainDocumentPart();
        // Document XML
        String documentXML = XmlUtils.marshaltoString(wmlPackage);
        // 转成字节输入流
        input = IOUtils.toBufferedInputStream(new ByteArrayInputStream(documentXML.getBytes()));
        // 输出模板
        IOUtils.copy(input, output);
    } finally {
        IOUtils.closeQuietly(input);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)

Example 33 with MainDocumentPart

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

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

the class HtmlToDOCDemo method replaceRichText.

public static void replaceRichText(WordprocessingMLPackage wordMLPackage, Map<String, String> richTextMap) throws Docx4JException, JAXBException {
    MainDocumentPart document = wordMLPackage.getMainDocumentPart();
    Map<String, List<Object>> textNodeMap = new HashMap<String, List<Object>>();
    findRichTextNode(textNodeMap, document.getContents().getBody(), null);
    Iterator<String> iterator = richTextMap.keySet().iterator();
    while (iterator.hasNext()) {
        String textTag = iterator.next();
        List<Object> textNodeList = textNodeMap.get(textTag);
        if (textNodeList != null && richTextMap.containsKey(textTag)) {
            List<Object> textObjList = convertToWmlObject(wordMLPackage, richTextMap.get(textTag));
            for (int i = 0, iSize = textNodeList.size(); i < iSize; i++) {
                Object nodeObject = textNodeList.get(i);
                if (nodeObject != null) {
                    // setWmlPprSetting(textNodeList.get(i), textObjList);
                    TraversalUtil.replaceChildren(nodeObject, textObjList);
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) ArrayList(java.util.ArrayList) List(java.util.List)

Example 35 with MainDocumentPart

use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(final Node node, final DocxRendererContext docx) {
    BasedSequence chars = node.getChars();
    MainDocumentPart mdp = docx.getDocxDocument();
    if (node instanceof Block) {
        docx.setBlockFormatProvider(new BlockFormatProviderBase<Node>(docx, docx.getDocxRendererOptions().LOOSE_PARAGRAPH_STYLE));
        docx.createP();
        docx.renderChildren(node);
    } else {
        docx.text(chars.unescape());
    }
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) EnumeratedReferenceBlock(com.vladsch.flexmark.ext.enumerated.reference.EnumeratedReferenceBlock) AsideBlock(com.vladsch.flexmark.ext.aside.AsideBlock) SimTocBlock(com.vladsch.flexmark.ext.toc.SimTocBlock) FootnoteBlock(com.vladsch.flexmark.ext.footnotes.FootnoteBlock) TocBlock(com.vladsch.flexmark.ext.toc.TocBlock)

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