在Word中添加强调符号,即着重号,可在选中字符后,鼠标右键点击,选择“字体”,在窗口中选择“着重号”添加到文字,用以对重要文字内容起加强提醒的目的,如下图:
通过Java后端程序代码,我们可以查找到需要添加着重号的字符串,然后通过字符串格式的属性值来添加符号。下面,将对此做详细介绍。
【导入Jar包】
按照如下方法来引用Spire.Doc.jar 版本:5.2.0
方法1:将Free Spire.Doc for Java包下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。然后在IDEA中打开“Project Structure”界面,然后执行如图步骤来手动导入本地路径下的jar文件:
方法2:通过Maven仓库下载导入,如下配置pom.xml:
<repositories>
<repository>
<id>com.e-iceblueid>
<name>e-icebluename>
<url>https://repo.e-iceblue.cn/repository/maven-public/url>
repository>
repositories>
<dependencies>
<dependency>
<groupId>e-icebluegroupId>
<artifactId>spire.doc.freeartifactId>
<version>5.2.0version>
dependency>
dependencies>
【添加强调符号】
代码步骤如下,可参考该步骤来实现添加符号:
- 创建Document类的对象。
- 用Document.loadFromFile(String fileName)方法从本地加载Word文档。
- 用Document.findAllString(String matchString, boolean caseSensitive, boolean wholeWord)方法查找指定文本字符串。
- for循环遍历查找到的所有字符串,通过TextSelection.getAsOneRange().getCharacterFormat().setEmphasisMark(Emphasis value)方法给字符串设置强调符号(着重号)。另外,可通过TextSelection.getAsOneRange().getCharacterFormat().setTextColor(Color value)方法对强调符号及字符设置颜色。
- 用Document.saveToFile(String fileName, FileFormat fileFormat)方法保存文档为新的Word文档。
Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.shape.Emphasis;
import java.awt.*;
public class EmphasisMark {
public static void main(String[] args) {
//创建Document对象
Document document = new Document();
//加载Word文档
document.loadFromFile("sample.docx");
//查找指定字符串
TextSelection[] textSelections = document.findAllString("围城", false, true);
//遍历查找到的所有字符串
for (TextSelection selection : textSelections)
{
//添加强调符号(着重号)到字符串
selection.getAsOneRange().getCharacterFormat().setEmphasisMark(Emphasis.Dot);
//给添加了强调符号的字符设置颜色
selection.getAsOneRange().getCharacterFormat().setTextColor(new Color(205,133,63));
}
//保存文档
document.saveToFile("AddEmphasisMark.docx", FileFormat.Docx_2013);
document.dispose();
}
}
添加效果:
相关推荐:C# 给Word中的字符添加强调符号(着重号)
—END—