在 Word 中將文本大小寫更改為全部大寫
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。下面將給您介紹在spire.doc下如何在 Word 中將文本大小寫更改為全部大寫,希望能對您有所幫助!
大寫字母非常適合強調 Word 中的文本。用大寫字母寫的段落很容易注意到,大寫字母暗示了段落的重要性。本文教您使用Spire.Doc for .NET通過編程將現有文本的大小寫更改為全部大寫的方法。
一、為 .NET 安裝 Spire.Doc
首先,您需要將 Spire.Doc for.NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。
PM> Install-Package Spire.Doc
二、將指定段落的大小寫更改為全部大寫
詳細步驟如下:
- 創建 Document 對象并使用Document.LoadFromFile()方法從文件中加載示例 Word 文檔。
- 
						使用Document.Sections[].Paragraph[]屬性獲取第二段,并通過TextRange.CharacterFormat.AllCaps屬性將其字符設置為 AllCaps。
 
- 
						使用Document.Sections[].Paragraph[]屬性獲取第三段,并通過TextRange.CharacterFormat.IsSmallCaps屬性將其字符設置為 IsSmallCaps 。
 
- 
						使用Document.SaveToFile()方法將文檔保存到新的 Word 文件中。
 
注: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 中將文本大小寫更改為全部大寫,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線