POI设置word 2007文本下划线样式及文字底纹
2017-07-28 12:01
525 查看
直接上代码:
Java代码
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.Random;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBackground;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHighlight;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
public class POI_07_设置下划线样式_S3_Test {
public static void main(String[] args) throws Exception {
POI_07_设置下划线样式_S3_Test t = new POI_07_设置下划线样式_S3_Test();
XWPFDocument doc = new XWPFDocument();
// 需关闭护眼色才能看到效果
t.setDocumentbackground(doc, "FDE9D9");//设置页面背景色
t.testSetUnderLineStyle(doc);//设置下划线样式以及突出显示文本
t.addNewPage(doc, BreakType.PAGE);
t.testSetShdStyle(doc);//设置文字底纹
t.saveDocument(doc,
"f:/saveFile/temp/sys_" + System.currentTimeMillis() + ".docx");
}
public void testSetUnderLineStyle(XWPFDocument doc) {
String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0",
"7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1",
"B5E900", "FEF8B6" };
Random random = new Random();
// TODO 这里为了方便测试写了数字,推荐写英文样式
for (int i = 1; i <= 18; i++) {
XWPFParagraph p = doc.createParagraph();
setParagraphFontInfoAndUnderLineStyle(p, "测试下划线", "宋体", "1D8C56",
"22", false, false, false, true, i,
colors[Math.abs(random.nextInt(colors.length))], false, 0,
null);
setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0", true,
"240", STLineSpacingRule.Enum.forString("auto"));
setParagraphAlignInfo(p, ParagraphAlignment.LEFT,
TextAlignment.CENTER);
}
}
public void testSetShdStyle(XWPFDocument doc) {
String[] colors = new String[] { "CCA6EF", "DD999D", "4FCEF0",
"7A7A7A", "F3C917", "FFA932", "C7B571", "535354", "5FD2F1",
"B5E900", "FEF8B6" };
Random random = new Random();
// TODO 这里为了方便测试写了数字,推荐写英文样式
for (int i = 1; i <= 38; i++) {
XWPFParagraph p = doc.createParagraph();
setParagraphFontInfoAndUnderLineStyle(p, "测试底纹", "宋体", "1D8C56",
"22", false, false, false, false, i, null, true, i,
colors[Math.abs(random.nextInt(colors.length))]);
setParagraphSpacingInfo(p, true, "0", "50", false, "0", "0", true,
"240", STLineSpacingRule.Enum.forString("auto"));
setParagraphAlignInfo(p, ParagraphAlignment.LEFT,
TextAlignment.CENTER);
}
}
public void setParagraphAlignInfo(XWPFParagraph p,
ParagraphAlignment pAlign, TextAlignment valign) {
p.setAlignment(pAlign);
p.setVerticalAlignment(valign);
}
public void setParagraphSpacingInfo(XWPFParagraph p, boolean isSpace,
String before, String after, boolean isPLine, String beforeLines,
String afterLines, boolean isLine, String line,
STLineSpacingRule.Enum lineValue) {
CTPPr pPPr = null;
if (p.getCTP() != null) {
if (p.getCTP().getPPr() != null) {
pPPr = p.getCTP().getPPr();
} else {
pPPr = p.getCTP().addNewPPr();
}
}
CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing()
: pPPr.addNewSpacing();
if (isSpace) {
// 段前磅数
if (before != null) {
pSpacing.setBefore(new BigInteger(before));
}
// 段后磅数
if (after != null) {
pSpacing.setAfter(new BigInteger(after));
}
}
if (isPLine) {
// 段前行数
if (beforeLines != null) {
pSpacing.setBeforeLines(new BigInteger(beforeLines));
}
// 段后行数
if (afterLines != null) {
pSpacing.setAfterLines(new BigInteger(afterLines));
}
}
// 间距
if (isLine) {
if (line != null) {
pSpacing.setLine(new BigInteger(line));
}
if (lineValue != null) {
pSpacing.setLineRule(lineValue);
}
}
}
public void setParagraphFontInfoAndUnderLineStyle(XWPFParagraph p,
String content, String fontFamily, String colorVal,
String fontSize, boolean isBlod, boolean isItalic,
boolean isStrike, boolean isUnderLine, int underLineStyle,
String underLineColor, boolean isShd, int shdValue, String shdColor) {
XWPFRun pRun = null;
if (p.getRuns() != null && p.getRuns().size() > 0) {
pRun = p.getRuns().get(0);
} else {
pRun = p.createRun();
}
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 (isBlod) {
pRun.setBold(isBlod);
}
if (isItalic) {
pRun.setItalic(isItalic);
}
if (isStrike) {
pRun.setStrike(isStrike);
}
if (colorVal != null) {
pRun.setColor(colorVal);
}
// 设置字突出显示文本
if (underLineStyle > 0 && underLineStyle < 17) {
CTHighlight hightLight = pRpr.isSetHighlight() ? pRpr
.getHighlight() : pRpr.addNewHighlight();
hightLight.setVal(STHighlightColor.Enum.forInt(underLineStyle));
}
// 设置下划线样式
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 (isShd) {
// 设置底纹
CTShd shd = pRpr.isSetShd() ? pRpr.getShd() : pRpr.addNewShd();
if (shdValue > 0 && shdValue <= 38) {
shd.setVal(STShd.Enum.forInt(shdValue));
}
if (shdColor != null) {
shd.setColor(shdColor);
}
}
}
// 设置页面背景色
public void setDocumentbackground(XWPFDocument document, String bgColor) {
CTBackground bg = document.getDocument().isSetBackground() ? document
.getDocument().getBackground() : document.getDocument()
.addNewBackground();
bg.setColor(bgColor);
}
public void addNewPage(XWPFDocument document, BreakType breakType) {
XWPFParagraph xp = document.createParagraph();
xp.createRun().addBreak(breakType);
}
public void saveDocument(XWPFDocument document, String savePath)
throws Exception {
FileOutputStream fos = new FileOutputStream(savePath);
document.write(fos);
fos.close();
}
}
结果为:
转载请注明原处,原文链接:http://53873039oycg.iteye.com/blog/2157758
,谢谢。
全文完。
相关文章推荐
- C#word(2007)操作类--新建文档、添加页眉页脚、设置格式、添加文本和超链接、添加图片、表格处理、文档格式转化
- 通过 poi 设置 word 文档 表格内容样式和 页眉页脚样式
- word 2013 修订模式下修订的文字样式设置
- C++操作word:插入文字、图片、表格,设置样式字体
- Word 2007技巧-----为段落设置图案底纹
- (非常好的Word操作封装类)VC中进行office编程的-操作word,可以设置文字的样式,新建表格
- poi 操作excel 2007 设置 字体样式 插入图片
- word vbs脚本 设置所有题注样式为蓝色,下划线
- poi操作word文档,以07版本为参考,将word文档中图片替换为文本(替换文本也一样),读图片可选文字内容
- 利用POI对MS Excel进行读写(可以设置写入文件的样式),利用tm-extractor对Word进行读取操作并将信息用(log4j)日志信息显示出
- iOS 富文本 文字分别设置 文本属性 Attributes 文字样式 程序开发 效果图
- 设置文本缩略的样式为"..." word-wrap和word-break区别详解
- iOS- 详解文本属性Attributes(文本的样式设置/阴影/下划线/颜色)
- iOS开发:关于UILabel、UIButton、UITextField文字下划线的设置方法(涉及到富文本的知识)
- 文本质量巧设置,一招让Visio 2007字体从模糊到清晰
- [寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件
- SpannableStringBuilder设置TextView文字格式(颜色、背景色、下划线、删除线等)
- CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)
- 为TextView添加样式——下划线,颜色,设置链接样式及前背景色
- 【POI xlsx】使用POI对xlsx的单元格样式进行设置 / 使用POI对xlsx的字体进行设置