gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:05:55 27 4
gpt4 key购买 nike

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

最佳答案

您的图片显示了来自 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 英寸。

关于java - 使用 Apache poi 生成 .docx 时如何为页脚添加间距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41740451/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com