docx4j学习网站

docx4j学习网址: http://www.docx4java.org/trac/docx4j

一个简单的例子:

由于ooxml文档规范纳入ISO标准,所以世界一些开源组织对OOXML的java开发包的研发也越来越快,除了apache的POI项目宣布对office2007的支持,其他组织也开始加紧行动,相比行动比较迅速的有dev.plutext.org组织的docx4j这个包比较成熟些,docx提供3种方法来操作docx文档,现在我们可以来看看docx4j是怎么操作docx文档的:
Java代码 
package org.docx4j.samples; 
import java.io.File; 
import java.math.BigInteger; 
import org.docx4j.customxml.ObjectFactory; 
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; 
import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
import org.docx4j.openpackaging.io.SaveToZipFile; 
import org.docx4j.wml.STPageOrientation; 
 
/**
* 创建ooxml文档
* @素馨花
* @version 1.0
*/ 
public class CreateWordprocessingMLDocument { 
 
    public static void main(String[] args) throws Exception { 
         
        System.out.println( "Creating package.."); 
        //创建文档处理对象 
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); 
         
        //插入文字方法-1(快捷方法,忽略详细属性) 
            wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "赛灵通(www.xerllent.cn)工作文档标题"); 
            wordMLPackage.getMainDocumentPart().addParagraphOfText("赛灵通项目(Xerllent Projects)是一项基于j2ee技术的企业信息化系统研发计划!"); 
 
 
         
         
        //插入文字方法-2(对象构造法,可以操作任何属性) 
            /**
             *  To get bold text, you must set the run's rPr@w:b,
             *  so you can't use the createParagraphOfText convenience method
             *  org.docx4j.wml.P p = wordMLPackage.getMainDocumentPart().createParagraphOfText("text");//创建无格式文本代码段
             * */ 
 
            org.docx4j.wml.ObjectFactory factory = new org.docx4j.wml.ObjectFactory();//文档子对象工厂 
             
            org.docx4j.wml.P  p = factory.createP();//创建段落P 
 
                //创建文本段R内容 
                org.docx4j.wml.R  run = factory.createR();//创建文本段R 
                    org.docx4j.wml.Text  t = factory.createText();//创建文本段内容Text 
                    t.setValue("text"); 
                run.getRunContent().add(t);//Text添加到R 
                //设置文本段R属性,Optionally, set pPr/rPr@w:b 
                org.docx4j.wml.RPr rpr = factory.createRPr(); 
                    org.docx4j.wml.BooleanDefaultTrue b = new org.docx4j.wml.BooleanDefaultTrue();//创建带缺省值的boolen属性对象 
                    b.setVal(true);      
                rpr.setB(b); 
                run.setRPr(rpr);//设置文本段R属性 
             
            p.getParagraphContent().add(run);//R添加到P 
             
            // 创建默认的段落属性,并加入到段落对象中去 
            org.docx4j.wml.PPr ppr = factory.createPPr(); 
                org.docx4j.wml.ParaRPr paraRpr = factory.createParaRPr(); 
                ppr.setRPr(paraRpr); 
            p.setPPr( ppr );//段落属性PPr添加到P 
             
          //将P段落添加到文档里 
          wordMLPackage.getMainDocumentPart().addObject(p); 
         
         
            //动态插入打印页面及分栏设置,这时一个A3幅面,页面分2栏的设置,试卷页面 
                org.docx4j.wml.SectPr sp= factory.createSectPr(); 
                    org.docx4j.wml.SectPr.PgSz pgsz=factory.createSectPrPgSz();//<w:pgSz w:w="23814" w:h="16840" w:orient="landscape" w:code="8"/> 
                        pgsz.setW(BigInteger.valueOf(23814L)); 
                        pgsz.setH(BigInteger.valueOf(16840L)); 
                        pgsz.setOrient(STPageOrientation.LANDSCAPE); 
                        pgsz.setCode(BigInteger.valueOf(8L)); 
                    sp.setPgSz(pgsz); 
                     
                    org.docx4j.wml.SectPr.PgMar pgmar= factory.createSectPrPgMar();//<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/> 
                        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); 
 
                    org.docx4j.wml.CTColumns cols=factory.createCTColumns();//<w:cols w:num="2" w:space="425"/> 
                        cols.setNum(BigInteger.valueOf(2)); 
                        cols.setSpace(BigInteger.valueOf(425)); 
                    sp.setCols(cols); 
 
                    org.docx4j.wml.CTDocGrid grd=factory.createCTDocGrid();//<w:docGrid w:linePitch="360"/> 
                        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:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" ><w:r><w:rPr><w:b /></w:rPr><w:t>Bold, just at w:r level</w:t></w:r></w:p>"; 
            wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(str)); 
             
            //自定义标签转化的时候,必须加xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"语句 
            String str1 = "<w:sectPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:rsidR=\"00F10179\" w:rsidRPr=\"00CB557A\" w:rsidSect=\"001337D5\"><w:pgSz w:w=\"23814\" w:h=\"16840\" w:orient=\"landscape\" w:code=\"8\"/><w:pgMar w:top=\"1440\" w:right=\"1440\" w:bottom=\"1440\" w:left=\"1440\" w:header=\"720\" w:footer=\"720\" w:gutter=\"0\"/><w:cols w:num=\"2\" w:space=\"425\"/><w:docGrid w:linePitch=\"360\"/></w:sectPr>"; 
            wordMLPackage.getMainDocumentPart().addObject(org.docx4j.XmlUtils.unmarshalString(str1) ); 
             
            System.out.println( ".. done!"); 
         
         
        // Now save it  
        wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/sample-docs/bolds.docx") ); 
         
        System.out.println("Done."); 
                 
    } 
     

你可能感兴趣的:(docx)