java poi生.docx_java - 使用Apache poi生成.docx时,如何为页脚添加间距? - 堆栈内存溢出...

您的图片显示了来自Openoffice或Libreoffice Writer的页面样式对话框,而不是主要由apache poi产生的Word中的页面样式对话框。 但尽管如此:

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

例:

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();

}

}

这使我的Writer达到了精确的0.8“页脚间距。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值