spire.doc是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。下面将给您介绍在spire.doc下如何在 Word 中将文本大小写更改为全部大写,希望能对您有所帮助!
Spire.Doc for.NET 最新下载
https://www.evget.com/product/3368/download
大写字母非常适合强调 Word 中的文本。用大写字母写的段落很容易注意到,大写字母暗示了段落的重要性。本文教您使用Spire.Doc for .NET通过编程将现有文本的大小写更改为全部大写的方法。
一、为 .NET 安装 Spire.Doc
首先,您需要将 Spire.Doc for.NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。
PM> Install-Package Spire.Doc
二、将指定段落的大小写更改为全部大写
详细步骤如下:
注:AllCaps 表示将所有字母大写并设置为相同大小,IsSmallCaps 表示将所有字母大写但将原始大写设置为大于小写。
【C#】
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace changecase
{
class Program
{
static void Main(string[] args)
{
//Create a new document and load from file
string input = @"D:\testp\test.docx"; ;
Document doc = new Document();
doc.LoadFromFile(input);
TextRange textRange;
//Get the second paragraph and set its characters to 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;
}
}
//Get the third paragraph and set its characters to 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;
}
}
//Save the document to a new Word file
string output = "ChangeCase.docx";
doc.SaveToFile(output, FileFormat.Docx2013);
}
}
}
【VB.NET】
Imports System Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Module Program Sub Main(args As String()) ' Create a new document and load from file Dim input As String = "D:\testp\test.docx" Dim doc As New Document() doc.LoadFromFile(input) Dim textRange As TextRange 'Get the second paragraph and set its character to 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 'Get the third paragraph and set its character to 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 'Save the document to a new Word file Dim output As String = "ChangeCase.docx" doc.SaveToFile(output, FileFormat.Docx2013) End Sub End Module

以上便是在spire.doc中如何在 Word 中将文本大小写更改为全部大写,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。