C#给Word中的字符添加着重号的方法详解

2022-05-19 15:24:00
目录
前言引入dll方法1方法2添加强调符号C#vb.net

前言

在Word中添加着重号,即强调符号,可以在选中字符后,鼠标右键点击,选择“字体”,在窗口中可直接选择“着重号”添加到文字,用以对重要文字内容起加强提醒的目的,如下图:

通过C#,我们可以查找到需要添加着重号的字符串,然后通过字符串格式的属性值来添加符号。下面,将对此做详细介绍。

引入dll

方法1

手动引入

将 Free>

方法2

NuGet安装(2种方法)

(1)可以在Visual>

(2)将以下内容复制到PM控制台安装:

Install-Package FreeSpire.Doc -Version 10.2.0

添加强调符号

代码步骤如下,可参考该步骤来实现添加符号:

    创建 Document 类的对象。用 Document.LoadFromFile() 方法从本地加载Word文档。用Document.FindAllString()方法查找指定文本字符串。通过CharacterFormat.EmphasisMark属性添加强调符号到字符串。用Document.SaveToFile()方法保存文档为新的Word文档。

    C#

    using Spire.Doc;
    using Spire.Doc.Documents;
    using System.Drawing;
    
    namespace EmphasisMark
    {
        class Program
        {
            static void Main(string[] args)
            {
                //创建Document对象
                Document document = new Document();
    
                //加载Word文档
                document.LoadFromFile("sample.docx");
    
                //查找指定字符串
                TextSelection[] textSelections = document.FindAllString("分析报告", false, true);
    
                //添加强调符号到字符串
                foreach (TextSelection selection in textSelections)
                {
                    selection.GetAsOneRange().CharacterFormat.EmphasisMark = Emphasis.DotBelow;
                }
    
                //保存文档
                document.SaveToFile("Result.docx", FileFormat.Docx2013);
                System.Diagnostics.Process.Start("Result.docx");
            }
        }
    }

    vb.net

    Imports Spire.Doc
    Imports Spire.Doc.Documents
    Imports System.Drawing
    
    Namespace EmphasisMark
        Class Program
            Private Shared Sub Main(args As String())
                '创建Document对象
                Dim document As New Document()
    
                '加载Word文档
                document.LoadFromFile("sample.docx")
    
                '查找指定字符串
                Dim textSelections As TextSelection() = document.FindAllString("分析报告", False, True)
    
                '添加强调符号到字符串
                For Each selection As TextSelection In textSelections
                    selection.GetAsOneRange().CharacterFormat.EmphasisMark = Emphasis.DotBelow
                Next
    
                '保存文档
                document.SaveToFile("Result.docx", FileFormat.Docx2013)
                System.Diagnostics.Process.Start("Result.docx")
            End Sub
        End Class
    End Namespace

    添加效果:

    到此这篇关于C#给Word中的字符添加着重号的方法详解的文章就介绍到这了,更多相关C# Word添加着重号内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!