使用Apache poi生成.docx时如何为页脚添加间距?

时间:2017-01-19 11:15:43

标签: java apache-poi

这是我想要的图像(页脚间距设置为80)。生成XWPFDocument时如何使用Apache Poi进行操作? enter image description here

1 个答案:

答案 0 :(得分:2)

您的图片显示的是来自Openoffice或Libreoffice Writer的页面样式对话框,而不是来自import java.io.*; import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar; import java.math.BigInteger; public class CreateWordHeaderFooterSpacing { public static void main(String[] args) throws Exception { XWPFDocument document = new XWPFDocument(); // create header-footer XWPFHeaderFooterPolicy headerFooterPolicy = document.getHeaderFooterPolicy(); if (headerFooterPolicy == null) headerFooterPolicy = document.createHeaderFooterPolicy(); // create footer start XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT); XWPFParagraph paragraph = footer.getParagraphArray(0); paragraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = paragraph.createRun(); run.setText("Footer"); CTSectPr sectPr = document.getDocument().getBody().getSectPr(); if (sectPr == null) sectPr = document.getDocument().getBody().addNewSectPr(); CTPageMar pageMar = sectPr.getPgMar(); if (pageMar == null) pageMar = sectPr.addNewPgMar(); pageMar.setLeft(BigInteger.valueOf(720)); //720 TWentieths of an Inch Point (Twips) = 720/20 = 36 pt = 36/72 = 0.5" pageMar.setRight(BigInteger.valueOf(720)); pageMar.setTop(BigInteger.valueOf(1440)); //1440 Twips = 1440/20 = 72 pt = 72/72 = 1" pageMar.setFooter(BigInteger.valueOf(720)); //0.5" footer margin long notPrintableBottomPageRange = (long)(0.038888*72*20); //0.038888" gap for non printable bottom page range pageMar.setBottom(BigInteger.valueOf(1152+720+notPrintableBottomPageRange)); //1152 Twips = 1152/20/72 = 0.8" //bottom margin = 0.8" footer spacing + 0.5" footer margin + 0.038888" gap for non printable bottom page range document.write(new FileOutputStream("CreateWordHeaderFooterSpacing.docx")); document.close(); } } 主要用于Word的页面样式对话框。但尽管如此:

所谓的"页脚 - 间距"在Writer中是Word中页面底部边距和页脚边距之间的差异。但请注意,底部有一个不可打印的页面范围,这取决于打印机。还必须考虑这一差距。

示例:

<assembly>
   <content>
      <add x="1" y="2"></add>
      <xml path="C:/abc.xml">
         <add z="1">
            <dummyNode>Content to Insert</dummyNode>
         </add>
      </xml>
   </content>
</assembly>

这导致我的作家精确到0.8&#34;页脚间距。