`
53873039oycg
  • 浏览: 824343 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

[简单]poi 创建word2007脚注(FootNote)

    博客分类:
  • poi
阅读更多

         直接上代码:

        

import java.io.FileOutputStream;
import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFootnotes;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STFtnEdn;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;

public class POI_创建脚注_S3_Test {
	public static void main(String[] args) throws Exception {
		POI_创建脚注_S3_Test t = new POI_创建脚注_S3_Test();
		t.testAddFootnotesToDocument("f:/saveFile/temp/sys_"
				+ System.currentTimeMillis() + ".docx");
	}

	public void testAddFootnotesToDocument(String savePath) throws Exception {
		XWPFDocument xdoc = new XWPFDocument();
		XWPFParagraph p2 = xdoc.createParagraph();
		setParagraphTextStyleInfo(p2, false, "测试", "微软雅黑", "000000", "22",
				false, false, false, false, 0, null, false, 0, null);
		BigInteger noteId = BigInteger.valueOf(1);
		addFootNote(xdoc, p2, noteId, "参见1.1.1", null, null, "微软雅黑", "FF0000",
				"18", true, false, false, false, 0, null, false, 0,
				STVerticalAlignRun.SUPERSCRIPT);
		setParagraphTextStyleInfo(p2, true, "插", "微软雅黑", "000000", "22", false,
				false, false, false, 0, null, false, 0, null);

		noteId = noteId.add(BigInteger.ONE);
		addFootNote(xdoc, p2, noteId, "参见1.1.2", "[", "]", "微软雅黑", "000000",
				"18", false, false, false, false, 0, null, false, 0,
				STVerticalAlignRun.SUPERSCRIPT);
		setParagraphTextStyleInfo(p2, true, "入脚注", "微软雅黑", "000000", "22",
				false, false, false, false, 0, null, false, 0, null);

		noteId = noteId.add(BigInteger.ONE);
		addFootNote(xdoc, p2, noteId, "参见1.1.3", "[", "]", "微软雅黑", "000000",
				"18", false, false, false, false, 0, null, false, 0,
				STVerticalAlignRun.SUPERSCRIPT);
		saveDocument(xdoc, savePath);
	}

	public void addFootNote(XWPFDocument xdoc, XWPFParagraph p,
			BigInteger noteId, String noteContent, String notePrefix,
			String noteSuffix, String fontFamily, String colorVal,
			String fontSize, boolean isBlod, boolean isItalic,
			boolean isStrike, boolean isUnderLine, int underLineStyle,
			String underLineColor, boolean isHightLight, int hightLightValue,
			STVerticalAlignRun.Enum stRunEnum) {
		XWPFRun r1 = null;
		if (notePrefix != null) {
			r1 = p.createRun();
			setRunTextStyleInfo(r1, notePrefix, fontFamily, colorVal, fontSize,
					isBlod, isItalic, isStrike, isUnderLine, underLineStyle,
					underLineColor, isHightLight, hightLightValue, stRunEnum);
		}
		r1 = p.createRun();
		setRunTextStyleInfo(r1, null, fontFamily, colorVal, fontSize, isBlod,
				isItalic, isStrike, isUnderLine, underLineStyle,
				underLineColor, isHightLight, hightLightValue, stRunEnum);
		CTFtnEdnRef fr = r1.getCTR().addNewFootnoteReference();
		fr.setId(noteId);

		if (noteSuffix != null) {
			r1 = p.createRun();
			setRunTextStyleInfo(r1, noteSuffix, fontFamily, colorVal, fontSize,
					isBlod, isItalic, isStrike, isUnderLine, underLineStyle,
					underLineColor, isHightLight, hightLightValue, stRunEnum);
		}

		XWPFFootnotes footnotes = xdoc.createFootnotes();
		CTFtnEdn ctNote = null;
		CTP ctp = null;
		XWPFParagraph p2 = null;
		ctNote = CTFtnEdn.Factory.newInstance();
		ctNote.setId(noteId);
		ctNote.setType(STFtnEdn.NORMAL);

		ctp = ctNote.addNewP();
		p2 = new XWPFParagraph(ctp, xdoc);
		if (notePrefix != null) {
			r1 = p2.createRun();
			setRunTextStyleInfo(r1, notePrefix, fontFamily, colorVal, fontSize,
					isBlod, isItalic, isStrike, isUnderLine, underLineStyle,
					underLineColor, isHightLight, hightLightValue, stRunEnum);
		}

		r1 = p2.createRun();
		setRunTextStyleInfo(r1, null, fontFamily, colorVal, fontSize, isBlod,
				isItalic, isStrike, isUnderLine, underLineStyle,
				underLineColor, isHightLight, hightLightValue, stRunEnum);
		r1.getCTR().addNewFootnoteRef();

		if (noteSuffix != null) {
			r1 = p2.createRun();
			setRunTextStyleInfo(r1, noteSuffix, fontFamily, colorVal, fontSize,
					isBlod, isItalic, isStrike, isUnderLine, underLineStyle,
					underLineColor, isHightLight, hightLightValue, stRunEnum);
		}

		r1 = p2.createRun();
		CTText ctText = r1.getCTR().addNewT();
		ctText.setStringValue(" ");
		ctText.setSpace(SpaceAttribute.Space.PRESERVE);

		r1 = p2.createRun();
		setRunTextStyleInfo(r1, noteContent, fontFamily, colorVal, fontSize,
				isBlod, isItalic, isStrike, isUnderLine, underLineStyle,
				underLineColor, isHightLight, hightLightValue, stRunEnum);
		footnotes.addFootnote(ctNote);
	}

	public void setRunTextStyleInfo(XWPFRun pRun, String content,
			String fontFamily, String colorVal, String fontSize,
			boolean isBlod, boolean isItalic, boolean isStrike,
			boolean isUnderLine, int underLineStyle, String underLineColor,
			boolean isHightLight, int hightLightValue,
			STVerticalAlignRun.Enum stRunEnum) {
		if (content != null) {
			pRun.setText(content);
		}
		CTRPr pRpr = null;
		if (pRun.getCTR() != null) {
			pRpr = pRun.getCTR().getRPr();
			if (pRpr == null) {
				pRpr = pRun.getCTR().addNewRPr();
			}
		}

		if (fontFamily != null) {
			// 设置字体
			CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr
					.addNewRFonts();
			fonts.setAscii(fontFamily);
			fonts.setEastAsia(fontFamily);
			fonts.setHAnsi(fontFamily);
		}

		if (fontSize != null) {
			// 设置字体大小
			CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();
			sz.setVal(new BigInteger(fontSize));
			CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr
					.addNewSzCs();
			szCs.setVal(new BigInteger(fontSize));
		}

		if (colorVal != null) {
			pRun.setColor(colorVal);
		}

		// 设置字体样式
		if (isBlod) {
			pRun.setBold(isBlod);
		}
		if (isItalic) {
			pRun.setItalic(isItalic);
		}
		if (isStrike) {
			pRun.setStrike(isStrike);
		}
		if (colorVal != null) {
			pRun.setColor(colorVal);
		}

		// 设置下划线样式
		if (isUnderLine) {
			CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();
			u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19)));
			if (underLineColor != null) {
				u.setColor(underLineColor);
			}
		}
		// 设置字突出显示文本
		if (isHightLight) {
			if (hightLightValue > 0 && hightLightValue < 17) {
				CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr
						.getHighlight() : pRpr.addNewHighlight();
				hightLight
						.setVal(STHighlightColor.Enum.forInt(hightLightValue));
			}
		}

		if (stRunEnum != null) {
			CTVerticalAlignRun ctV = CTVerticalAlignRun.Factory.newInstance();
			ctV.setVal(stRunEnum);
			pRpr.setVertAlign(ctV);
		}
	}

	public void setParagraphTextStyleInfo(XWPFParagraph p, boolean isNew,
			String content, String fontFamily, String colorVal,
			String fontSize, boolean isBlod, boolean isItalic,
			boolean isStrike, boolean isUnderLine, int underLineStyle,
			String underLineColor, boolean isHightLight, int hightLightValue,
			STVerticalAlignRun.Enum stRunEnum) {
		XWPFRun pRun = null;
		if (isNew) {
			pRun = p.createRun();
		} else {
			if (p.getRuns() != null && p.getRuns().size() > 0) {
				pRun = p.getRuns().get(0);
			} else {
				pRun = p.createRun();
			}
		}
		if (content != null) {
			pRun.setText(content);
		}

		CTRPr pRpr = null;
		if (pRun.getCTR() != null) {
			pRpr = pRun.getCTR().getRPr();
			if (pRpr == null) {
				pRpr = pRun.getCTR().addNewRPr();
			}
		}

		// 设置字体
		CTFonts fonts = pRpr.isSetRFonts() ? pRpr.getRFonts() : pRpr
				.addNewRFonts();
		fonts.setAscii(fontFamily);
		fonts.setEastAsia(fontFamily);
		fonts.setHAnsi(fontFamily);

		// 设置字体大小
		CTHpsMeasure sz = pRpr.isSetSz() ? pRpr.getSz() : pRpr.addNewSz();
		sz.setVal(new BigInteger(fontSize));

		CTHpsMeasure szCs = pRpr.isSetSzCs() ? pRpr.getSzCs() : pRpr
				.addNewSzCs();
		szCs.setVal(new BigInteger(fontSize));

		if (colorVal != null) {
			pRun.setColor(colorVal);
		}

		// 设置字体样式
		if (isBlod) {
			pRun.setBold(isBlod);
		}
		if (isItalic) {
			pRun.setItalic(isItalic);
		}
		if (isStrike) {
			pRun.setStrike(isStrike);
		}
		if (colorVal != null) {
			pRun.setColor(colorVal);
		}

		// 设置下划线样式
		if (isUnderLine) {
			CTUnderline u = pRpr.isSetU() ? pRpr.getU() : pRpr.addNewU();
			u.setVal(STUnderline.Enum.forInt(Math.abs(underLineStyle % 19)));
			if (underLineColor != null) {
				u.setColor(underLineColor);
			}
		}
		// 设置突出显示文本
		if (isHightLight) {
			if (hightLightValue > 0 && hightLightValue < 17) {
				CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr
						.getHighlight() : pRpr.addNewHighlight();
				hightLight
						.setVal(STHighlightColor.Enum.forInt(hightLightValue));
			}
		}

		if (stRunEnum != null) {
			CTVerticalAlignRun ctV = CTVerticalAlignRun.Factory.newInstance();
			ctV.setVal(stRunEnum);
			pRpr.setVertAlign(ctV);
		}
	}

	public void saveDocument(XWPFDocument document, String savePath)
			throws Exception {
		FileOutputStream fos = new FileOutputStream(savePath);
		document.write(fos);
		fos.close();
	}
}

    结果为:

   

 

        转载请注明原处,原文链接:http://53873039oycg.iteye.com/blog/2158635  ,谢谢
--------------------------------------------顺便说一句--------------------------------------------
        使用上述方法创建批注(comment)或者尾注(endnote)是不可行的,到目前为止,尚未发现有效的方法使用poi新增批注或者尾注,如果有这方便的需求,请使用docx4j,至少docx4j在git上面的samples里包含了怎么创建comment,其他的如怎么使用poi创建书签(bookmark)这种简单的功能就不写了,修改书签网上也有现成的代码。
        全文完

 

  • 大小: 5.4 KB
  • 大小: 12.5 KB
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics