Search in sources :

Example 26 with MainDocumentPart

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

the class Docx4j_Helper method testDocx4jSetPageSize.

public void testDocx4jSetPageSize() throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    String titleStr = "静夜思    李白";
    String str = "床前明月光,疑似地上霜。";
    String str2 = "举头望明月,低头思故乡。";
    P p = Docx4j_Helper.factory.createP();
    String rprStr = "<w:rPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rFonts w:hint=\"eastAsia\" w:ascii=\"Times New Roman\" w:hAnsi=\"Times New Roman\" w:eastAsia=\"宋体\"/><w:b/><w:color w:val=\"333333\"/><w:sz w:val=\"32\"/><w:szCs w:val=\"32\"/></w:rPr>";
    RPr rpr = (RPr) XmlUtils.unmarshalString(rprStr);
    setParagraphContent(p, rpr, titleStr);
    mdp.addObject(p);
    p = Docx4j_Helper.factory.createP();
    setParagraphContent(p, rpr, str);
    mdp.addObject(p);
    p = Docx4j_Helper.factory.createP();
    PPr pPr = Docx4j_Helper.factory.createPPr();
    // 设置文字方向
    SectPr sectPr = Docx4j_Helper.factory.createSectPr();
    TextDirection textDirect = Docx4j_Helper.factory.createTextDirection();
    // 文字方向:垂直方向从右往左
    textDirect.setVal("tbRl");
    sectPr.setTextDirection(textDirect);
    Type sectType = Docx4j_Helper.factory.createSectPrType();
    // 下一页
    sectType.setVal("nextPage");
    sectPr.setType(sectType);
    // 设置页面大小
    PgSz pgSz = Docx4j_Helper.factory.createSectPrPgSz();
    pgSz.setW(new BigInteger("8335"));
    pgSz.setH(new BigInteger("11850"));
    sectPr.setPgSz(pgSz);
    pPr.setSectPr(sectPr);
    p.setPPr(pPr);
    setParagraphContent(p, rpr, str2);
    mdp.addObject(p);
    p = createParagraphWithHAlign();
    setParagraphContent(p, rpr, titleStr);
    mdp.addObject(p);
    p = createParagraphWithHAlign();
    setParagraphContent(p, rpr, str);
    mdp.addObject(p);
    p = createParagraphWithHAlign();
    setParagraphContent(p, rpr, str2);
    mdp.addObject(p);
// Docx4j_Helper.saveWordPackage(wordMLPackage, outputfilepath);
}
Also used : P(org.docx4j.wml.P) Type(org.docx4j.wml.SectPr.Type) PPr(org.docx4j.wml.PPr) TextDirection(org.docx4j.wml.TextDirection) RPr(org.docx4j.wml.RPr) SectPr(org.docx4j.wml.SectPr) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) BigInteger(java.math.BigInteger) PgSz(org.docx4j.wml.SectPr.PgSz) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage)

Example 27 with MainDocumentPart

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

the class Docx4j_删除所有批注_S3_Test method removeAllComment.

// 这里2个路径可以一致
public void removeAllComment(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filePath));
    // 清空comments.xml内容
    Parts parts = wordMLPackage.getParts();
    HashMap<PartName, Part> partMap = parts.getParts();
    CommentsPart commentPart = (CommentsPart) partMap.get(new PartName("/word/comments.xml"));
    Comments comments = commentPart.getContents();
    List<Comment> commentList = comments.getComment();
    for (int i = 0, len = commentList.size(); i < len; i++) {
        commentList.remove(0);
    }
    // 清空document.xml文件中批注
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getContents();
    Body body = wmlDocumentEl.getBody();
    CommentFinder cf = new CommentFinder();
    new TraversalUtil(body, cf);
    for (Child commentElement : cf.commentElements) {
        System.out.println(commentElement.getClass().getName());
        Object parent = commentElement.getParent();
        List<Object> theList = ((ContentAccessor) parent).getContent();
        boolean removeResult = remove(theList, commentElement);
        System.out.println(removeResult);
    }
    wordMLPackage.save(new FileOutputStream(savePath));
}
Also used : WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) PartName(org.docx4j.openpackaging.parts.PartName) Parts(org.docx4j.openpackaging.parts.Parts) ContentAccessor(org.docx4j.wml.ContentAccessor) Body(org.docx4j.wml.Body) Child(org.jvnet.jaxb2_commons.ppp.Child) Comment(org.docx4j.wml.Comments.Comment) Comments(org.docx4j.wml.Comments) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) TraversalUtil(org.docx4j.TraversalUtil) Part(org.docx4j.openpackaging.parts.Part) MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) CommentsPart(org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart) FileOutputStream(java.io.FileOutputStream)

Example 28 with MainDocumentPart

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

the class Docx4j_工具类_S3_Test method getAllTbl.

/**
 * @Description:得到所有表格
 */
public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) {
    MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart();
    List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class);
    if (objList == null) {
        return null;
    }
    List<Tbl> tblList = new ArrayList<Tbl>();
    for (Object obj : objList) {
        if (obj instanceof Tbl) {
            Tbl tbl = (Tbl) obj;
            tblList.add(tbl);
        }
    }
    return tblList;
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) ArrayList(java.util.ArrayList) Tbl(org.docx4j.wml.Tbl)

Example 29 with MainDocumentPart

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

the class AddingPageNrToFooter method main.

/**
 *  First we create the package and the factory. Then we create the footer.
 *  Finally we add two pages with text to the document and save it.
 */
public static void main(String[] args) throws Exception {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
    Relationship relationship = createFooterPart();
    createFooterReference(relationship);
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    documentPart.addParagraphOfText("Hello World!");
    addPageBreak(documentPart);
    documentPart.addParagraphOfText("This is page 2!");
    wordMLPackage.save(new File("src/main/files/HelloWord15.docx"));
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) Relationship(org.docx4j.relationships.Relationship) File(java.io.File)

Example 30 with MainDocumentPart

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

the class AddingTableOfContent method main.

/**
 *  首先我们创建对象工厂和包并从包中抽出文档部件. 然后我们添加目录表, 后面跟着一些带有分类
 *  标题样式的段落. 最后我们保存包.
 */
public static void main(String[] args) throws Docx4JException {
    factory = Context.getWmlObjectFactory();
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
    addTableOfContent(documentPart);
    documentPart.addStyledParagraphOfText("Heading1", "Hello 1");
    documentPart.addStyledParagraphOfText("Heading2", "Hello 2");
    documentPart.addStyledParagraphOfText("Heading3", "Hello 3");
    documentPart.addStyledParagraphOfText("Heading1", "Hello 1");
    wordMLPackage.save(new File("src/main/files/HelloWord10.docx"));
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) 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