Word控件Spire.Doc 【文本】教程(16) ;在 Word 中将文本大小写更改为全部大写

本文将给您介绍在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 filestring input = @"D:testptest.docx"; ;Document doc = new Document();doc.LoadFromFile(input);TextRange textRange;//Get the second paragraph and set its characters to AllCapsParagraph 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 IsSmallCapsParagraph 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 filestring output = "ChangeCase.docx";doc.SaveToFile(output, FileFormat.Docx2013);}}}

【VB.NET】

Imports SystemImports Spire.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsModule ProgramSub Main(args As String())' Create a new document and load from fileDim input As String = "D:testptest.docx"Dim doc As New Document()doc.LoadFromFile(input)Dim textRange As TextRange'Get the second paragraph and set its character to AllCapsDim para1 As Paragraph = doc.Sections(0).Paragraphs(2)For Each obj As DocumentObject In para1.ChildObjectsIf TypeOf obj Is TextRange ThentextRange = TryCast(obj, TextRange)textRange.CharacterFormat.AllCaps = TrueEnd IfNext obj'Get the third paragraph and set its character to IsSmallCapsDim para2 As Paragraph = doc.Sections(0).Paragraphs(3)For Each obj As DocumentObject In para2.ChildObjectsIf TypeOf obj Is TextRange ThentextRange = TryCast(obj, TextRange)textRange.CharacterFormat.IsSmallCaps = TrueEnd IfNext obj'Save the document to a new Word fileDim output As String = "ChangeCase.docx"doc.SaveToFile(output, FileFormat.Docx2013)End SubEnd Module

C#/VB.NET: Change Text Case to All Capitals in Word

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


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年7月19日
下一篇 2022年7月19日

相关推荐

发表回复

登录后才能评论