Java 利用poi给word添加页眉页脚,页眉左边为公司logo图片,右为公司全称,页脚左为公司地址,右为页码总页数

25 篇文章 0 订阅
1 篇文章 0 订阅

参考链接: http://www.it1352.com/219785.html

1. 生成带公司logo的页眉
public void createHeader(XWPFDocument doc, String orgFullName, String logoFilePath) throws Exception {
		/*
		* 对页眉段落作处理,使公司logo图片在页眉左边,公司全称在页眉右边
		* */
		CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
		XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, 	sectPr);
		XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);

		XWPFParagraph paragraph = header.getParagraphArray(0);
		paragraph.setAlignment(ParagraphAlignment.LEFT);
		paragraph.setBorderBottom(Borders.THICK);

		CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
		tabStop.setVal(STTabJc.RIGHT);
		int twipsPerInch =  1440;
		tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));

		XWPFRun run = paragraph.createRun();
		setXWPFRunStyle(run,"新宋体",10);

		/*
		* 根据公司logo在ftp上的路径获取到公司到图片字节流
		* 添加公司logo到页眉,logo在左边
		* */
        if (StringUtils.isNotEmptyOrNull(logoFilePath)) {
            String imgFile = FileUploadUtil.getLogoFilePath(logoFilePath);
            byte[] bs = FtpUtil.downloadFileToIo(imgFile);
            InputStream is = new ByteArrayInputStream(bs);

            XWPFPicture picture = run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imgFile, Units.toEMU(80), Units.toEMU(45));

            String blipID = "";
            for(XWPFPictureData picturedata : header.getAllPackagePictures()) {	//这段必须有,不然打开的logo图片不显示
                blipID = header.getRelationId(picturedata);
            }
            picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID);
            run.addTab();
            is.close();
        }

		/*
		 * 添加字体页眉,公司全称
		 * 公司全称在右边
		 * */
		if (StringUtils.isNotEmptyOrNull(orgFullName)) {
            run = paragraph.createRun();
            run.setText(orgFullName);
			setXWPFRunStyle(run,"新宋体",10);
        }
	}

2. 生成带公司地址和电话的页脚
public void createFooter(XWPFDocument document, String telephone, String orgAddress) throws Exception {
		/*
		* 生成页脚段落
		* 给段落设置宽度为占满一行
		* 为公司地址和公司电话左对齐,页码右对齐创造条件
		* */
		CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
		XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
		XWPFFooter footer =  headerFooterPolicy.createFooter(STHdrFtr.DEFAULT);
		XWPFParagraph paragraph = footer.getParagraphArray(0);
		paragraph.setAlignment(ParagraphAlignment.LEFT);
		paragraph.setVerticalAlignment(TextAlignment.CENTER);
		paragraph.setBorderTop(Borders.THICK);
		CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
		tabStop.setVal(STTabJc.RIGHT);
		int twipsPerInch =  1440;
		tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));

		/*
		* 给段落创建元素
		* 设置元素字面为公司地址+公司电话
		* */
		XWPFRun run = paragraph.createRun();
		run.setText((StringUtils.isNotEmptyOrNull(orgAddress) ? orgAddress : "") + (StringUtils.isNotEmptyOrNull(telephone) ? "  " + telephone: ""));
		setXWPFRunStyle(run,"仿宋",10);
		run.addTab();

		/*
		* 生成页码
		* 页码右对齐
		* */
		run = paragraph.createRun();
		run.setText("第");
		setXWPFRunStyle(run,"仿宋",10);

		run = paragraph.createRun();
		CTFldChar fldChar = run.getCTR().addNewFldChar();
		fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));

		run = paragraph.createRun();
		CTText ctText = run.getCTR().addNewInstrText();
		ctText.setStringValue("PAGE  \\* MERGEFORMAT");
		ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
		setXWPFRunStyle(run,"仿宋",10);

		fldChar = run.getCTR().addNewFldChar();
		fldChar.setFldCharType(STFldCharType.Enum.forString("end"));

		run = paragraph.createRun();
		run.setText("页 总共");
		setXWPFRunStyle(run,"仿宋",10);

		run = paragraph.createRun();
		fldChar = run.getCTR().addNewFldChar();
		fldChar.setFldCharType(STFldCharType.Enum.forString("begin"));

		run = paragraph.createRun();
		ctText = run.getCTR().addNewInstrText();
		ctText.setStringValue("NUMPAGES  \\* MERGEFORMAT ");
		ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
		setXWPFRunStyle(run,"仿宋",10);

		fldChar = run.getCTR().addNewFldChar();
		fldChar.setFldCharType(STFldCharType.Enum.forString("end"));

		run = paragraph.createRun();
		run.setText("页");
		setXWPFRunStyle(run,"仿宋",10);

	}

3.其中设置段落元素字体的方法
/**
	 * 设置页脚的字体样式
	 * 
	 * @param r1 段落元素
	 */
	private void setXWPFRunStyle(XWPFRun r1,String font,int fontSize) {
		r1.setFontSize(fontSize);
		CTRPr rpr = r1.getCTR().isSetRPr() ? r1.getCTR().getRPr() : r1.getCTR().addNewRPr();
		CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
		fonts.setAscii(font);
		fonts.setEastAsia(font);
		fonts.setHAnsi(font);
	}

各种IT书籍书目及下载链接
https://blog.csdn.net/dh1027/article/details/89327978

  • 4
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
要在 Word 文档中添加页眉页脚,可以使用 Apache POI 的 XWPF API。下面是一个简单的示例代码,说明如何使用 XWPF API 在 Word 文档中添加页眉页脚: ```java import org.apache.poi.xwpf.usermodel.*; import java.io.*; public class WordDocument { public static void main(String[] args) throws Exception { // 创建一个新的 Word 文档 XWPFDocument document = new XWPFDocument(); // 添加一个新的页面 XWPFParagraph para = document.createParagraph(); XWPFRun run = para.createRun(); run.setText("This is a new page."); // 添加页眉 CTP ctP = CTP.Factory.newInstance(); XWPFParagraph headerParagraph = new XWPFParagraph(ctP, document); XWPFHeader header = document.createHeader(XWPFHeaderFooterPolicy.DEFAULT); header.getParagraphArray(0).setBorderBottom(Borders.SINGLE); // 设置页眉的边框 header.getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER); // 设置页眉的对齐方式 CTText ctText = CTText.Factory.newInstance(); ctText.setStringValue("This is the header text."); headerParagraph.getCTP().setPPr(header.getParagraphArray(0).getCTP().getPPr()); ctP.setRArray(0, headerParagraph.createRun().getCTR()); // 添加页脚 CTP ctP2 = CTP.Factory.newInstance(); XWPFParagraph footerParagraph = new XWPFParagraph(ctP2, document); XWPFFooter footer = document.createFooter(XWPFHeaderFooterPolicy.DEFAULT); footer.getParagraphArray(0).setBorderTop(Borders.SINGLE); // 设置页脚的边框 footer.getParagraphArray(0).setAlignment(ParagraphAlignment.CENTER); // 设置页脚的对齐方式 CTText ctText2 = CTText.Factory.newInstance(); ctText2.setStringValue("This is the footer text."); footerParagraph.getCTP().setPPr(footer.getParagraphArray(0).getCTP().getPPr()); ctP2.setRArray(0, footerParagraph.createRun().getCTR()); // 保存 Word 文档 FileOutputStream out = new FileOutputStream("example.docx"); document.write(out); out.close(); } } ``` 在上面的示例代码中,我们创建了一个新的 Word 文档,并添加了一个新的页面。然后,我们通过 XWPF API 添加页眉页脚。最后,我们将 Word 文档保存到本地文件系统中。 需要注意的是,上面的示例代码中,我们使用了 XWPFHeaderFooterPolicy.DEFAULT,这表示我们在默认的页眉页脚位置添加了新的页眉页脚。如果你想要在不同的位置添加页眉页脚,可以使用不同的 XWPFHeaderFooterPolicy。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值