在 Word 文档中,想要强调文档的英文段落,可以将其全部大写来达到强调的效果。大写的英文段落更能引人注意,同时大写字母本身就传递了文本的重要性。Spire.Doc for .NET 为大家提供了一种简单的方法,用编程的方式实现段落文本全部更改为大写字母,无需逐字操作。本文将给大家介绍用 Spire.Doc for .NET 更改英文文本为全部大写的详细操作步骤。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for .NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc更改英文文本为全部大写
详细操作步骤如下:
- 创建 Document 的对象,并用 Document.LoadFromFile() 方法从文件中载入 Word 文档。
 - 通过 Document.Sections[].Paragraph[] 属性获取第二段文本并通过 TextRange.CharacterFormat.AllCaps 属性将其设置为 AllCaps。
 - 通过 Document.Sections[].Paragraph[] 属性获取第三段文本并通过 TextRange.CharacterFormat.AllCaps 属性将其设置为 IsSmallCaps。
 - 用 Document.SaveToFile() 方法保存文档到新的 Word 文件。
 
注: AllCaps 是将文本全部大写,且所有字母大小相同,而 IsSmallCaps 是将文本全部大写,并且原大写文本比原小写文本大小更大。
- C#
 - VB.NET
 
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace changecase
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Document 的对象,并从文件中载入 Word 文档
            string input = @"D:\testp\示例.docx"; ;
            Document doc = new Document();
            doc.LoadFromFile(input);
            TextRange textRange;
            // 获取第二段文本并将其设置为 AllCaps
            Paragraph para1 = doc.Sections[0].Paragraphs[2];
            foreach (DocumentObject obj in para1.ChildObjects)
            {
                if (obj is TextRange)
                {
                    textRange = obj as TextRange;
                    textRange.CharacterFormat.AllCaps = true;
                }
            }
            // 获取第三段文本并将其设置为 IsSmallCaps
            Paragraph para2 = doc.Sections[0].Paragraphs[3];
            foreach (DocumentObject obj in para2.ChildObjects)
            {
                if (obj is TextRange)
                {
                    textRange = obj as TextRange;
                    textRange.CharacterFormat.IsSmallCaps = true;
                }
            }
            //保存文档到新的 Word 文件
            string output = "改为全部大写.docx";
            doc.SaveToFile(output, FileFormat.Docx2013);
        }
    }
}Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Module Program
    Sub Main(args As String())
        ' 创建 Document 的对象,并从文件中载入 Word 文档
        Dim input As String = "D:\testp\示例.docx"
        Dim doc As New Document()
        doc.LoadFromFile(input)
        Dim textRange As TextRange
        '获取第二段文本并将其设置为AllCaps
        Dim para1 As Paragraph = doc.Sections(0).Paragraphs(2)
        For Each obj As DocumentObject In para1.ChildObjects
            If TypeOf obj Is TextRange Then
                textRange = TryCast(obj, TextRange)
                textRange.CharacterFormat.AllCaps = True
            End If
        Next obj
        '获取第三段文本并将其设置为IsSmallCaps
        Dim para2 As Paragraph = doc.Sections(0).Paragraphs(3)
        For Each obj As DocumentObject In para2.ChildObjects
            If TypeOf obj Is TextRange Then
                textRange = TryCast(obj, TextRange)
                textRange.CharacterFormat.IsSmallCaps = True
            End If
        Next obj
        '保存文档到新的 Word 文件
        Dim output As String = "改为全部大写.docx"
        doc.SaveToFile(output, FileFormat.Docx2013)
    End Sub
End Module
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
    


					



