Docx4j 添加页码

    Office 2007 之后使用 XML 格式来存储文档数据,这个变化其实是非常不错的。如何操作这些XML生成文件呢?或许你还在使用 POI,也有可能你在使用已经放弃更新的 iText (只是doc已经放弃更新,PDF持续更新),但是我觉得,你可以选择使用 Docx4j,这是个不错的开源软件,国内很少使用而已。

    下面就简单的使用 docx4j添加页码的代码做一个简单的介绍。

/**
     * 创建页脚(分页信息)
     */
    public static void createFooter(WordprocessingMLPackage wordMLPackage) throws Exception {
        Relationship relationship = DocxUtil.createFootPart(wordMLPackage);
        DocxUtil.createFootReference(wordMLPackage, relationship);
    }

    /**
     * 创建页脚的组件
     */
    public static Relationship createFootPart(
            WordprocessingMLPackage wordprocessingMLPackage)
            throws Exception {

        FooterPart footerPart = new FooterPart();
        Relationship rel = wordprocessingMLPackage.getMainDocumentPart()
                .addTargetPart(footerPart);

        // After addTargetPart, so image can be added properly
        footerPart.setJaxbElement(createFooterPageNr());

        return rel;
    }

    /**
     * 创建页脚的组件
     */
    public static void createFootReference(
            WordprocessingMLPackage wordprocessingMLPackage,
            Relationship relationship)
            throws InvalidFormatException {

        ObjectFactory factory = Context.getWmlObjectFactory();
        List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();

        SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
        // There is always a section wrapper, but it might not contain a sectPr
        if (sectPr == null) {
            sectPr = factory.createSectPr();
            wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
            sections.get(sections.size() - 1).setSectPr(sectPr);
        }

        // footer references
        FooterReference footerReference = factory.createFooterReference();
        footerReference.setId(relationship.getId());
        footerReference.setType(HdrFtrRef.DEFAULT);
        sectPr.getEGHdrFtrReferences().add(footerReference);
    }


    /**
     * 添加页脚,带有页码
     *
     * @return 页脚对象
     */
    public static Ftr createFooterPageNr() {
        ObjectFactory factory = Context.getWmlObjectFactory();
        Ftr ftr = factory.createFtr();
        P paragraph = factory.createP();
        RPr fontRPr = getRPr("宋体", "000000", "20", STHint.EAST_ASIA, false);
        R run = factory.createR();
        run.setRPr(fontRPr);
        paragraph.getContent().add(run);

        paragraph.getContent().add(getTextField("第"));
        paragraph.getContent().add(getFieldBegin());
        paragraph.getContent().add(getPageNumberField());
        paragraph.getContent().add(getFieldEnd());
        paragraph.getContent().add(getTextField("页"));


        paragraph.getContent().add(getTextField(" 总共"));
        paragraph.getContent().add(getFieldBegin());
        paragraph.getContent().add(getTotalPageNumberField());
        paragraph.getContent().add(getFieldEnd());
        paragraph.getContent().add(getTextField("页"));
        setCellContentStyle(paragraph, JcEnumeration.CENTER);

        ftr.getContent().add(paragraph);

        return ftr;
    }

生成 docx 文档,这个代码来自Git

	public static void main(String[] args) throws Exception {
		
		WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
		MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
		
//		mdp.addParagraphOfText("hello world");
//		mdp.addParagraphOfText("welcome");


		mdp.addStyledParagraphOfText("Title","hello world");
		mdp.addStyledParagraphOfText("Subtitle","welcome");
		mdp.addParagraphOfText("today is good day . welcome all students come here.");


		ProtectDocument protection = new ProtectDocument(wordMLPackage);
		protection.restrictEditing(STDocProtect.READ_ONLY, "foobaa");
		
        DocxUtil.createFooter(wordMLPackage);
		
		String filename = System.getProperty("user.dir") + "/OUT_hello.docx";
		Docx4J.save(wordMLPackage, new java.io.File(filename), Docx4J.FLAG_SAVE_ZIP_FILE);
		System.out.println("Saved " + filename);
						
	}

 

 

 

转载于:https://my.oschina.net/heweipo/blog/702784

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值