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

poi操作word 同时被 3 个专栏收录
1 篇文章 0 订阅
27 篇文章 0 订阅
25 篇文章 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

  • 3
    点赞
  • 20
    评论
  • 12
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值