一、为 .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

以上便是在spire.doc中如何在 Word 中将文本大小写更改为全部大写,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询在线客服 ;
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!