如何在 poi word (XWPF) 中设置页眉和页脚位置? [英] How to set header and Footer position in poi word (XWPF)?

查看:578
本文介绍了如何在 poi word (XWPF) 中设置页眉和页脚位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置页眉和页脚的位置.从顶部的页眉:45.4 pt,从底部的页脚:28.4 pt,正如我们在 Header & 中看到的那样打开word文件时的页脚工具菜单.提前致谢!

I want to set header and footer's position. Header from top: 45.4 pt, Footer from bottom: 28.4 pt as we can see in Header & Footer Tools menu when we open a word file. Thanks in advance!

推荐答案

为此,您需要设置页边距.为此,您将需要 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar.为了使用它,我们需要 ooxml-schemas-1.3.jar"nofollow noreferrer">https://poi.apache.org/faq.html#faq-N10025.

For this you will need setting the page margins. For this you will need org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar. And for using this we need the fully ooxml-schemas-1.3.jar as mentioned in https://poi.apache.org/faq.html#faq-N10025.

示例:

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 CreateWordHeaderFooterTopBottom {

 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 header start
  XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

  XWPFParagraph paragraph = header.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  XWPFRun run = paragraph.createRun();  
  run.setText("Header");

  // create footer start
  XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);

  paragraph = footer.createParagraph();
  paragraph.setAlignment(ParagraphAlignment.CENTER);

  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.setBottom(BigInteger.valueOf(1440));

  pageMar.setHeader(BigInteger.valueOf(908)); //45.4 pt * 20 = 908 = 45.4 pt header from top
  pageMar.setFooter(BigInteger.valueOf(568)); //28.4 pt * 20 = 568 = 28.4 pt footer from bottom

  FileOutputStream out = new FileOutputStream("CreateWordHeaderFooterTopBottom.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

注意特殊的测量单位 Twip = TWInch P 点的十分之一.

Note the special measurement unit Twip = TWentieths of an Inch Point.

这篇关于如何在 poi word (XWPF) 中设置页眉和页脚位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆