如何在新的docx文档中创建页眉/页脚? [英] How to create a header/footer in new docx document?
本文介绍了如何在新的docx文档中创建页眉/页脚?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用XWPF jars(apache poi)在docx文档(一个新文档而不是现有文档)上创建页眉和页脚。
I would like to create a header and footer on a docx document (a new one and not existing one) with XWPF jars (apache poi).
当我使用 XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
策略为空,所以我会知道如何在新文档中创建它。
When I use XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
policy is null, so I would know how to create it in a new document.
CustomXWPFDocument document = new CustomXWPFDocument();
XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy();
XWPFHeader head = policy.createHeader(policy.DEFAULT);
head.getListParagraph().get(0).createRun().setText("Hello Header World!");
CTP ctP1 = CTP.Factory.newInstance();
CTR ctR1 = ctP1.addNewR();
CTText t = ctR1.addNewT();
t.setStringValue("Paragraph in header");
XWPFParagraph p1 = new XWPFParagraph(ctP1, document);
XWPFParagraph[] pars = new XWPFParagraph[1];
pars[0] = p1;
policy.createHeader(policy.FIRST, pars);
推荐答案
您必须将一个section属性添加到 XWPFDocument doc
如果使用下面的代码不存在
You must add a section property to the XWPFDocument doc
if not already present by using following code
CTBody body = doc.getDocument().getBody();
CTSectPr sectPr = body.isSetSectPr()? body.getSectPr() : body.addNewSectPr();
这篇关于如何在新的docx文档中创建页眉/页脚?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文