需要从word文档的一部分中删除页码

时间:2013-11-05 20:29:06

标签: java xml docx4j

我需要从docx4j创建的word文档的最后一部分中删除页码。我们面临的问题是我们无法从上一节中删除引用。我创建了一个新的word文档,open,我查看了XML。试图重现没有成功,我无法删除参考。使用的代码下方:

private void addFooter(SectPr sectPr, boolean pageNumber) {
    Relationship rel = null;
    If (pageNumber) {
         FooterPart footerPart = new FooterPart((new PartName("/word/footer1.xml")));
         rel = getWordMLPackage().getMainDocumentPart().addTargetPart(footerPart);
         InputStream is = new FileInputStream(JSFUtils.carregarRecurso("/resources/docx/xml/footer1.xml"));
         Ftr ftr = (Ftr) XmlUtils.unmarshal(is);
         footerPart.setJaxbElement(ftr);
    } else {
         FooterPart footerPart = new FooterPart((new PartName("/word/footer2.xml")));
         rel = getWordMLPackage().getMainDocumentPart().addTargetPart(footerPart);
         InputStream is = new FileInputStream(JSFUtils.carregarRecurso("/resources/docx/xml/footer2.xml"));
         Ftr ftr = (Ftr) XmlUtils.unmarshal(is);
         footerPart.setJaxbElement(ftr); 
    }

    FooterReference footerReference = getObjectFactory().createFooterReference();
    footerReference.setId(rel.getId());
    footerReference.setType(HdrFtrRef.DEFAULT); 
    sectPr.getEGHdrFtrReferences().add(footerReference);
}

Xml“footer1.xml”

<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
       <w:p>
             <w:pPr>
                    <w:pStyle w:val="Footer" />
                    <w:jc w:val="right" />
             </w:pPr>
             <w:r>
                    <w:t xml:space="preserve">Page</w:t>
             </w:r>
             <w:fldSimple w:instr=" PAGE \* MERGEFORMAT ">
                    <w:r>
                           <w:rPr>
                                 <w:noProof />
                           </w:rPr>
                           <w:t>17</w:t>
                    </w:r>
             </w:fldSimple>
       </w:p>
</w:ftr>

Xml“footer2.xml”

<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
       <w:p>
             <w:pPr>
                    <w:jc w:val="right"/>
             </w:pPr>
             <w:r>
                    <w:t xml:space="preserve"> </w:t>
             </w:r>
       </w:p>
</w:ftr>

杰森。在请求的信息下面:

我们正在使用报表中的两个不同部分创建一个复杂的文档,因为我们需要更改页面方向以显示一些表格。

要创建我们使用的肖像部分:

public void addPortraitPage() {
   SectPr sectionPortrait = objectFactory.createSectPr();
   PgSz portrait = new PgSz();
   portrait.setOrient(STPageOrientation.PORTRAIT);
   portrait.setH(BigInteger.valueOf(16383));
   portrait.setW(BigInteger.valueOf(11906));   

   PgMar pgMar = new PgMar();
   pgMar.setGutter(new BigInteger("0"));
   pgMar.setFooter(new BigInteger("709"));
   pgMar.setHeader(new BigInteger("709"));
   pgMar.setLeft(new BigInteger("1701"));
   pgMar.setBottom(new BigInteger("851"));
   pgMar.setRight(new BigInteger("1134"));
   pgMar.setTop(new BigInteger("567"));

   sectionPortrait.setPgSz(portrait);
   sectionPortrait.setPgMar(pgMar);

   org.docx4j.wml.P p = objectFactory.createP();

   PPr createPPr = objectFactory.createPPr();
   createPPr.setSectPr(sectionPortrait);

   p.setPPr(createPPr);
   getMdp().addObject(p);

   getMdp().getJaxbElement().getBody().setSectPr(sectionPortrait);
   getMdp().getContent().add(sectionPortrait);

   addFooter(sectionPortrait, true);
}

上面的代码添加了包含页码的肖像页面。它很棒。下面我展示了用于插入没有页码编号的Landscape页面的代码:

public void addLandscapePage() {
    SectPr sectionLandscape = objectFactory.createSectPr();
    PgSz landscape = new PgSz();
    landscape.setOrient(STPageOrientation.LANDSCAPE);
    landscape.setH(BigInteger.valueOf(11906));
    landscape.setW(BigInteger.valueOf(16383));

    PgMar pgMar = new PgMar();
    pgMar.setGutter(new BigInteger("0"));
    pgMar.setFooter(new BigInteger("709"));
    pgMar.setHeader(new BigInteger("709"));
    pgMar.setLeft(new BigInteger("567"));
    pgMar.setBottom(new BigInteger("567"));
    pgMar.setRight(new BigInteger("567"));
    pgMar.setTop(new BigInteger("567"));

    sectionLandscape.setPgSz(landscape);
    sectionLandscape.setPgMar(pgMar);

    org.docx4j.wml.P p = objectFactory.createP();

    PPr createPPr = objectFactory.createPPr();
    createPPr.setSectPr(sectionLandscape);

    p.setPPr(createPPr);
    getMdp().addObject(p);

    getMdp().getJaxbElement().getBody().setSectPr(sectionLandscape);
    getMdp().getContent().add(sectionLandscape);

    addFooter(sectionLandscape, false);
}

带有页码编号的SecPr肖像:

<w:sectPr>
    <w:footerReference w:type="default" r:id="rId11"/>
    <w:pgSz w:w="11906" w:orient="portrait" w:h="16383"/>
    <w:pgMar w:gutter="0" w:footer="709" w:header="709" w:left="1701" w:bottom="851" w:right="1134" w:top="567"/>
</w:sectPr>

没有页码编号的SecPr肖像:

<w:sectPr>
    <w:footerReference w:type="default" r:id="rId227"/>
    <w:pgSz w:w="16383" w:orient="landscape" w:h="11906"/>
    <w:pgMar w:gutter="0" w:footer="709" w:header="709" w:left="567" w:bottom="567" w:right="567" w:top="567"/>
</w:sectPr>

查看文件的参考文献:

rId11:

<Relationship Id="rId11" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/>

rId227:

<Relationship Id="rId227" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer2.xml"/>

Document with footer

0 个答案:

没有答案