0
我想设置页眉和页脚的位置。顶部标题:45.4 pt,底部页脚:28.4 pt,因为我们可以在标题中看到&页脚工具菜单,当我们打开一个文件时。提前致谢! 如何在诗词(XWPF)中设置页眉和页脚位置?
我想设置页眉和页脚的位置。顶部标题:45.4 pt,底部页脚:28.4 pt,因为我们可以在标题中看到&页脚工具菜单,当我们打开一个文件时。提前致谢! 如何在诗词(XWPF)中设置页眉和页脚位置?
为此,您需要设置页边距。为此,您需要org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPageMar
。而为了使用这个,我们需要完整ooxml-schemas-1.3.jar
如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
document.write(new FileOutputStream("CreateWordHeaderFooterTopBottom.docx"));
document.close();
}
}
注特殊测量单位Twip
= TW一个的entieths我 NCH P oint。