在制作Word文档时,颜色是避不开的运用元素,不同的内容,可能需要填充特定的颜色,以突显重要内容、丰富文档。
但是,在Word中无论是文字、表格、项目符号、下划线等等元素,默认均为黑色。如果想改变这些元素的颜色,大家都知道怎么操作吗?……
今天,我们就将为大家一一介绍其操作方法,一起来学学吧!
1、为文字填充颜色
文字填色非常简单啦!选择需要填充颜色的文字,点击【开始】-【字体】组中的“字体颜色”按钮,在弹出的列表中选择需要的颜色即可。
或是,选择文字后,点击鼠标右键,在弹出的浮动工具栏中单击“字体颜色”按钮,然后选择需要的颜色。
2、为表格填充颜色
首先选中整个表格,点击【开始】-【段落】-【边框】-【边框和底纹】。在“边框和底纹”对话框中,将边框颜色设置为喜爱的颜色,然后点击“确定”按钮。此时,即可看到表格边框的颜色都变了。
3、为项目符号填充颜色
在项目符号处单击鼠标,选择所有项目符号,然后点击【开始】-【字体】组中的“字体颜色”按钮,在弹出的列表中选择需要的颜色即可。
4、为下划线填充颜色
如果想要突出显示下划线,我们可以自定义下划线的颜色。点击【开始】-【字体】【下划线】按钮,在弹出的列表中选择“下划线颜色”,再选择颜色即可。
OK,非常简单且实用的几个小技巧,希望能帮助到大家!
****部落窝教育-word颜色填充技巧****
原创:部落窝教育(未经同意,请勿转载)
更多教程:部落窝教育(www.itblw.com)
微信公众号:worditblw
我们在做项目的时候,可能会遇到这样的一个需求,根据业务数据,生成一份pdf文件,然后提供给用户下载查看。
生成pdf文件,可能会有很多种方式实现:
1.根据html模板,把数据填充到html模板,生成一份pdf文件2.根据pdf模板,把数据填充到pdf模板,生成一份pdf文件3.根据word模板,把数据填充到word模板,生成一份pdf文件对比了这3种实现方式:
第1种实现方式,html的代码,会导致有些样式丢失,效果不好。(不太建议)
第2种实现方式,操作起来,很麻烦,需要编写大量的代码,最终的效果也不是很友好。(难度比较大,不太建议)
第3种实现方式,这种实现,是个人目前觉得比较好的一种方式,样式,实现也可以接受啦!!!
以上,仅代表个人观点,可能有不对的地方,别喷,或轻点喷!!!
那今天咋们着重的讨论一下第3种实现方式!!!^_^
word转pdf说明目前,word转pdf,我们可以借助openoffice帮我们实现这个功能。
但凡,有个但是啦,毕竟你要借助openoffice,这毕竟是一个工具,那咋们就不得不部署运行这个程序,那这样得话,不得加重咋们后期服务器的部署和运维了嘛?
所以这里,咋们还是得想想看,有无其他简单的实现方式?
哈哈,那肯定是有的啦,我们可以通过纯代码的方式,实现word转pdf的功能!!!
下面看哥们操作喽!!!^_^
word转pdf实现word模板数据填充工具类public class WordTemplateUtils { private static Logger logger = LoggerFactory.getLogger(WordTemplateUtils.class); /* * 根据Word模板生成word文档 * @param uploadPath 文件夹路径 * @param writePath 生成word文件路径 * @param templatePath word模板路径 * @param contentMap 填充数据 */ public static boolean writeWordByTemplate(String uploadPath, String writePath, String templatePath, Map<String, Object> contentMap) throws Exception { //如果文件夹不存在,新建文件夹 File file = new File(uploadPath); //如果路径不存在,新建 if (!file.exists() && !file.isDirectory()) { file.mkdirs(); } boolean result = false; logger.info("---根据模板生成word文档"); XWPFDocument document = new XWPFDocument(new FileInputStream(templatePath)); if (document == null) { logger.error("---模板文件不存在,tempPath:{}", templatePath); return result; } InputStream is = null; FileOutputStream fos = null; try { fos = new FileOutputStream(writePath); WordTemplateUtils.changeText(document, contentMap); //生成新的word文档 document.write(fos); result = true; } catch (IOException e) { logger.error("---输出word文档失败,原因:{}", e.getMessage()); result = false; } finally { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } return result; } /** * 替换段落文本 * * @param document docx解析对象 * @param textMap 需要替换的信息集合 */ private static void changeText(XWPFDocument document, Map<String, Object> textMap) { //获取段落集合 List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { //判断此段落时候需要进行替换 String text = paragraph.getText(); System.out.println(text); if (!text.equals("")) { List<XWPFRun> runs = paragraph.getRuns(); for (XWPFRun run : runs) { String textR = run.getText(0);//读取的模板段落 if (StringUtils.isEmpty(textR)) { continue; } String newTxetR = ""; //判断文本是否需要进行替换 // System.out.print(textR + ","); for (Map.Entry<String, Object> entry : textMap.entrySet()) { //匹配模板与替换值 格式${key} String key = entry.getKey(); Object value = entry.getValue(); if (textR.contains(key)) {// if (value instanceof String) { //文字替换 String inputText = value.toString(); if (inputText.contains("\n")) {//包含换行符 String[] lines = inputText.split("\n"); //newTxetR = textR.replace(key, lines[0]); run.setText(lines[0].trim(), 0); // set first line into XWPFRun for (int i = 1; i < lines.length; i++) { // add break and insert new text run.addBreak();//换行 run.addTab();//缩进 run.setText(lines[i].trim()); } } else { newTxetR = textR.replace(key, (String) value); //替换模板原来位置 if (!newTxetR.equals("")) { run.setText(newTxetR, 0); } } } else if (value instanceof Map) { //图片替换 newTxetR = textR.replace(key, ""); //替换模板原来位置 if (!newTxetR.equals("")) { run.setText(newTxetR, 0); } Map picMap = (Map) value; int width = Integer.parseInt(picMap.get("width").toString()); int height = Integer.parseInt(picMap.get("height").toString()); int picType = getPictureType(picMap.get("type").toString()); FileInputStream fis = (FileInputStream) picMap.get("content"); try { String blipId = document.addPictureData(fis, picType); int id = document.getNextPicNameNumber(picType); createPicture(id, blipId, width, height, run); } catch (Exception e) { e.printStackTrace(); } } } } } } } } /** * @param id * @param blipId * @param width 宽 * @param height 高 * //* @param paragraph 段落 */ private static void createPicture(int id, String blipId, int width, int height, XWPFRun xwpfRun) { final int EMU = 9525; width *= EMU; height *= EMU; CTInline inline = xwpfRun.getCTR().addNewDrawing().addNewInline(); //CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline(); //在遍历run列表的时候,创建新的run有可能会导致报错 String picXml = "" + "<a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">" + " <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">" + " <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">" + " <pic:nvPicPr>" + " <pic:cNvPr id="" + id + "" name="Generated"/>" + " <pic:cNvPicPr/>" + " </pic:nvPicPr>" + " <pic:blipFill>" + " <a:blip r:embed="" + blipId + "" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"/>" + " <a:stretch>" + " <a:fillRect/>" + " </a:stretch>" + " </pic:blipFill>" + " <pic:spPr>" + " <a:xfrm>" + " <a:off x="0" y="0"/>" + " <a:ext cx="" + width + "" cy="" + height + ""/>" + " </a:xfrm>" + " <a:prstGeom prst="rect">" + " <a:avLst/>" + " </a:prstGeom>" + " </pic:spPr>" + " </pic:pic>" + " </a:graphicData>" + "</a:graphic>"; inline.addNewGraphic().addNewGraphicData(); XmlToken xmlToken = null; try { xmlToken = XmlToken.Factory.parse(picXml); } catch (XmlException xe) { xe.printStackTrace(); } inline.set(xmlToken); inline.setDistT(0); inline.setDistB(0); inline.setDistL(0); inline.setDistR(0); CTPositiveSize2D extent = inline.addNewExtent(); extent.setCx(width); extent.setCy(height); CTNonVisualDrawingProps docPr = inline.addNewDocPr(); docPr.setId(id); docPr.setName("docx_img_ " + id); docPr.setDescr("docx Picture"); } /** * 根据图片类型,取得对应的图片类型代码 * * @param picType * @return int */ private static int getPictureType(String picType) { int res = XWPFDocument.PICTURE_TYPE_PICT; if (picType != null) { if (picType.equalsIgnoreCase("png")) { res = XWPFDocument.PICTURE_TYPE_PNG; } else if (picType.equalsIgnoreCase("dib")) { res = XWPFDocument.PICTURE_TYPE_DIB; } else if (picType.equalsIgnoreCase("emf")) { res = XWPFDocument.PICTURE_TYPE_EMF; } else if (picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")) { res = XWPFDocument.PICTURE_TYPE_JPEG; } else if (picType.equalsIgnoreCase("wmf")) { res = XWPFDocument.PICTURE_TYPE_WMF; } } return res; } public static void main(String[] args) throws Exception { Map<String, Object> contentMap = new HashMap<String, Object>(); //组装文本参数 contentMap.put("demo1", "示例1"); contentMap.put("demo2", "示例2"); String fileName = "demo.docx"; WordTemplateUtils.writeWordByTemplate("D:\word\file", ""D:\word\file\" + fileName, "D:\word\template\template.docx", contentMap); } } 复制代码word模板,可以是这样:
最终生成的效果如下:
这里提供一下:License.xml文件
<License> <Data> <Products> <Product>Aspose.Total for Java</Product> <Product>Aspose.Words for Java</Product> </Products> <EditionType>Enterprise</EditionType> <SubscriptionExpiry>20991231</SubscriptionExpiry> <LicenseExpiry>20991231</LicenseExpiry> <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber> </Data> <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature> </License>好了,这个就是word转pdf的示例,也是比较简单啦!!!
好了,word转pdf,大概的实现,就是这样了!!!^_^
那我们就可以愉快的编写代码了!!!^_^
作者:llsydn链接:https://juejin.cn/post/7087036463035973640
在制作Word文档的时候,如果在文档中加底纹会让文档看起来更精美一点。Word文档底纹怎么设置呢?
第一步:我们开启WPS文档,接下来需要找到并打开需要设置底纹的文件:
第二步:在打开的文档中,我们需要选择菜单栏里的“开始”按钮,然后找到下面工具栏中的底纹设置按钮:
第三步:选择我们需要设置底纹的文字内容,然后点击底纹按钮:
第四步:点击底纹按钮后我们在这里需要选择底纹的颜色,选好后就可以看到效果了:
通过上面的介绍,相信朋友们大多会了解Word文档底纹的设置方法。