Search in sources :

Example 51 with WordprocessingMLPackage

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

the class Docx4j_SaveDocxImg_S3_Test method saveDocxImg.

/**
 * @Description: 提取word图片
 */
public void saveDocxImg(String filePath, String savePath) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(filePath));
    for (Entry<PartName, Part> entry : wordMLPackage.getParts().getParts().entrySet()) {
        if (entry.getValue() instanceof BinaryPartAbstractImage) {
            BinaryPartAbstractImage binImg = (BinaryPartAbstractImage) entry.getValue();
            // 图片minetype
            String imgContentType = binImg.getContentType();
            PartName pt = binImg.getPartName();
            String fileName = null;
            if (pt.getName().indexOf("word/media/") != -1) {
                fileName = pt.getName().substring(pt.getName().indexOf("word/media/") + "word/media/".length());
            }
            System.out.println(String.format("mimetype=%s,filePath=%s", imgContentType, pt.getName()));
            FileOutputStream fos = new FileOutputStream(savePath + fileName);
            ((BinaryPart) entry.getValue()).writeDataToOutputStream(fos);
            fos.close();
        }
    }
}
Also used : BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) PartName(org.docx4j.openpackaging.parts.PartName) BinaryPart(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart) Part(org.docx4j.openpackaging.parts.Part) FileOutputStream(java.io.FileOutputStream) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) BinaryPartAbstractImage(org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage) File(java.io.File)

Example 52 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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 53 with WordprocessingMLPackage

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

the class AddingAnInlineImageTest method main.

/**
 *  像往常一样, 我们创建了一个包(package)来容纳文档.
 *  然后我们创建了一个指向将要添加到文档的图片的文件对象.为了能够对图片做一些操作, 我们将它转换
 *  为字节数组. 最后我们将图片添加到包中并保存这个包(package).
 */
public static void main(String[] args) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    File file = new File("src/main/resources/iProfsLogo.png");
    byte[] bytes = WMLPackageUtils.imageToByteArray(file);
    WordprocessingMLPackageRender render = new WordprocessingMLPackageRender(wordMLPackage);
    render.addImageToPackage(bytes);
    wordMLPackage.save(new java.io.File("src/main/files/HelloWord7.docx"));
}
Also used : WordprocessingMLPackageRender(org.docx4j.template.io.WordprocessingMLPackageRender) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) File(java.io.File)

Example 54 with WordprocessingMLPackage

use of org.docx4j.openpackaging.packages.WordprocessingMLPackage 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)

Example 55 with WordprocessingMLPackage

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

the class CreateWordprocessingMLDocument method main.

public static void main(String[] args) throws Exception {
    // 创建文档处理对象
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    // 插入文字方法-1(快捷方法,忽略详细属性)
    wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "赛灵通(www.xerllent.cn)工作文档标题");
    wordMLPackage.getMainDocumentPart().addParagraphOfText("赛灵通项目(XerllentProjects)是一项基于j2ee技术的企业信息化系统研发计划!");
    // 插入文字方法-2(对象构造法,可以操作任何属性)
    /**
     * Togetboldtext,youmustsettherun'srPr@w:b,
     * soyoucan'tusethecreateParagraphOfTextconveniencemethod
     * org.docx4j.wml.Pp=wordMLPackage.getMainDocumentPart().
     * createParagraphOfText("text");//创建无格式文本代码段
     */
    // 文档子对象工厂
    org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();
    // 创建段落P
    org.docx4j.wml.P p = factory.createP();
    // 创建文本段R内容
    // 创建文本段R
    org.docx4j.wml.R run = factory.createR();
    // 创建文本段内容Text
    org.docx4j.wml.Text t = factory.createText();
    t.setValue("text");
    // Text添加到R
    run.getRunContent().add(t);
    // 设置文本段R属性,Optionally,setpPr/rPr@w:b
    org.docx4j.wml.RPr rpr = factory.createRPr();
    // 创建带缺省值的boolen属性对象
    org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();
    b.setVal(true);
    rpr.setB(b);
    // 设置文本段R属性
    run.setRPr(rpr);
    // R添加到P
    p.getParagraphContent().add(run);
    // 创建默认的段落属性,并加入到段落对象中去
    org.docx4j.wml.PPr ppr = factory.createPPr();
    org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr();
    ppr.setRPr(paraRpr);
    // 段落属性PPr添加到P
    p.setPPr(ppr);
    // 将P段落添加到文档里
    wordMLPackage.getMainDocumentPart().addObject(p);
    // 动态插入打印页面及分栏设置,这时一个A3幅面,页面分2栏的设置,试卷页面
    org.docx4j.wml.SectPr sp = factory.createSectPr();
    // <w:pgSzw:w="23814"w:h="16840"w:orient="landscape"w:code="8"/>
    org.docx4j.wml.SectPr.PgSz pgsz = factory.createSectPrPgSz();
    pgsz.setW(BigInteger.valueOf(23814L));
    pgsz.setH(BigInteger.valueOf(16840L));
    pgsz.setOrient(STPageOrientation.LANDSCAPE);
    pgsz.setCode(BigInteger.valueOf(8L));
    sp.setPgSz(pgsz);
    // <w:pgMarw:top="1440"w:right="1440"w:bottom="1440"w:left="1440"w:header="720"w:footer="720"w:gutter="0"/>
    org.docx4j.wml.SectPr.PgMar pgmar = factory.createSectPrPgMar();
    pgmar.setTop(BigInteger.valueOf(1440));
    pgmar.setRight(BigInteger.valueOf(1440));
    pgmar.setBottom(BigInteger.valueOf(1440));
    pgmar.setLeft(BigInteger.valueOf(1440));
    pgmar.setHeader(BigInteger.valueOf(720));
    pgmar.setFooter(BigInteger.valueOf(720));
    sp.setPgMar(pgmar);
    // <w:colsw:num="2"w:space="425"/>
    org.docx4j.wml.CTColumns cols = factory.createCTColumns();
    cols.setNum(BigInteger.valueOf(2));
    cols.setSpace(BigInteger.valueOf(425));
    sp.setCols(cols);
    // <w:docGridw:linePitch="360"/>
    org.docx4j.wml.CTDocGrid grd = factory.createCTDocGrid();
    grd.setLinePitch(BigInteger.valueOf(360));
    sp.setDocGrid(grd);
    wordMLPackage.getMainDocumentPart().addObject(sp);
    // 插入文字方法-3(更加简便快捷的插入内容方法,可以操作任何属性,但必须熟悉ooxml文档格式)
    // 自定义标签转化的时候,必须加xmlns:w=/"http://schemas.openxmlformats.org/wordprocessingml/2006/main/"语句
    String str = "<w:pxmlns:w=/\"http://schemas.openxmlformats.org/wordprocessingml/2006/main/\"><w:r><w:rPr><w:b/></w:rPr><w:t>Bold,justatw:rlevel</w:t></w:r></w:p>";
    wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(str));
    // 自定义标签转化的时候,必须加xmlns:w=/"http://schemas.openxmlformats.org/wordprocessingml/2006/main/"语句
    // "<w:sectPrxmlns:w=/\"http://schemas.openxmlformats.org/wordprocessingml/2006/main/\"w:rsidR=/\"00F10179/\"w:rsidRPr=/\"00CB557A/\"w:rsidSect=/\"001337D5/\"><w:pgSzw:w=/"23814/"w:h=/"16840/"w:orient=/"landscape/"w:code=/"8/"/><w:pgMarw:top=/"1440/"w:right=/"1440/"w:bottom=/"1440/"w:left=/"1440/"w:header=/"720/"w:footer=/"720/"w:gutter=/"0/"/><w:colsw:num=/"2/"w:space=/"425/"/><w:docGridw:linePitch=/"360/"/></w:sectPr>";
    String str1 = null;
    wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(str1));
    System.out.println("..done!");
    // Nowsaveit
    wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/sample-docs/bolds.docx"));
    System.out.println("Done.");
}
Also used : 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