POI设置word 2007文本下划线样式及文字底纹
直接上代码:
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 ,谢谢。
全文完。
POI设置word 2007文本下划线样式及文字底纹相关推荐
- [简单]POI设置word 2007文本下划线样式及文字底纹
直接上代码: import java.io.FileOutputStream; import java.math.BigInteger; import java.util.Random;import ...
- [简单]poi 设置word 2007段落与表格底纹
直接上代码: import java.io.FileOutputStream; import java.math.BigInteger; import java.util.ArrayList; imp ...
- html设置文本的下划线效果,CSS3 自定义文本下划线样式
CSS 语言: CSSSCSS 确定 body { background: #165578; } .wrapper { width: 500px; margin: 0 auto; } h1 { col ...
- 计算机统考加重号,高会《职称计算机》Word 2007:设置字体效果、下划线、着重号...
通过高级会计师考试的考生们现在可以着手准备职称计算机考试了,东奥小编为您准备相关知识点,希望大家能够认真学习. 设置字体效果.下划线.着重号 文本格式化,除了可以设置字体.字形.字号.字体颜色,还可以 ...
- html怎么设置下划线形状,科技常识:CSS如何给文字添加下划线样式
今天小编跟大家讲解下有关CSS如何给文字添加下划线样式 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关CSS如何给文字添加下划线样式 的相关资料,希望小伙伴们看了有所帮助. 在css中可以 ...
- text-decoration设置文本下划线失效
前言 最近公司的一项业务中涉及到了文本下划线的问题,本来对于我们来说,下划线并不是难点,可曾想过,这里也能踩坑. 下划线? so easy! text-decoration: underline so ...
- CSS设置下划线对齐方式,如何巧妙利用CSS自定义网页下划线样式
如何巧妙利用CSS自定义网页下划线样式 CSS为网页设计者们提供了丰富而灵活的页面元素表现形式的控制手段.但是,或许你可能注意到了,对于下划线,CSS提供的可选操作却不是很多.一般情况下,人们看到的下 ...
- css中字体下划线样式,css下划线 浅谈css自定义下划线
使用css样式对一段文字或一段文字中其中几个文字设置虚线效果的下划线如何实现?我们知道css字体下划线使用text-decoration样式实现,而虚线下划线则不能使用此css样式属性.要实现通过下边 ...
- HTML文本下划线效果,聊聊CSS中文本下划线_CSS, SVG, masking, clip-path, 会员专栏, text-decoration 教程_W3cplus...
在Web中给文本添加下划线常常出现在链接的文本上,早期一般使用text-decoration属性给文本添加下划线.删除线等.除了text-decoration之外,CSS还有很多技术方案可以给文本添加 ...
最新文章
- vant组件实现上传图片裁剪_如何用 120 行代码,实现交互完整的拖拽上传组件?...
- fgets()用法笔记
- AutoHotkey热键脚本语言文件
- mysql+索引优化+查询优化+存储优化_mysql利用覆盖索引避免回表优化查询
- java中字符串的操作_java中字符串的操作
- printf 格式控制符的完整格式
- flask bootstrap ajax,使用Flask集成bootstrap的方法
- 第十二章_网络搭建及训练
- python获取月份 pos_python – 如何从POS标记单词列表中提取模式? NLTK
- Java项目:药品管理系统(java+SSM+html+jQuery+Tomcat+mysql)
- GD32E230按键软件消抖程序
- oracle术语英文,LOL各种英文术语,英雄联盟英文术语
- win11系统备份和还原
- 0805 0603 贴片电阻的区别
- 考研英语(二)——简单句
- 国内首部区块链行业纪录片开播
- DCloud是什么?
- SolidWorks装配体及约束快速导入Adams
- linux连接oracle的日志,linux shell脚本连接oracle查询数据插入文件和日志文件中
- 前台开发总结13——20180419
热门文章
- 模拟图灵机XN乘2的过程
- 【jprofiler】jprofiler安装使用教程
- 2017年第四八届C/C++ B组蓝桥杯省赛真题
- clang++ exe error unable to execute command Couldnt execute program文件名或扩展名太长
- 第一期码易猿游活动圆满结束 精彩仍将继续
- 用Nero刻录ISO镜像制作启动光盘
- 自己如何快速制作蓝色背景证件照
- python之__slots__
- VTK绘制螺钉螺纹线
- games101学习笔记_Geometry1(几何)