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


    spire.doc是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。下面将给您介绍在spire.doc下如何在 Word 中将文本大小写更改为全部大写,希望能对您有所帮助!

    Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://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

    二、将指定段落的大小写更改为全部大写

    详细步骤如下:

    • 创建 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 中将文本大小写更改为全部大写,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。


  • 相关阅读:
    Qt事件处理机制(二)重写事件处理函数:重写鼠标移动事件,实现用鼠标拖动按钮在widget中自由移动!
    4.存储NFS
    PWmat加入腾讯TEFS生态,共同探索大体系计算应用场景
    AdServices归因和iAd归因集成
    【软考】14.2 统一建模语言UML/事务关系图
    VK1056B/C计算器,跑步机,电子秤LCD驱动方案,段码液晶显示芯片IC技术资料
    Python基础:【习题系列】函数
    [附源码]计算机毕业设计校园服装租赁系统Springboot程序
    ve-plus:基于 vue3.x 桌面端UI组件库|vue3组件库
    excel怎么设置密码?加密文件这么做!
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/127789020