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

2022-05-11 08:41:00 Written by  support iceblue
Rate this item
(0 votes)

Capitals are quite suitable for stressing texts in Word. A paragraph written with capitalized letters is easy to notice, and capitalized letters imply the importance of the paragraph. This article teaches you the method of changing the case of existing text to all capitals with Spire.Doc for .NET by programming.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Change the Case of a Specified Paragraph to All Capitals

The detailed steps are as follows:

  • Create an object of Document and load a sample Word document from file using Document.LoadFromFile() method.
  • Get the second paragraph using Document.Sections[].Paragraph[] property and set its characters to AllCaps through TextRange.CharacterFormat.AllCaps property.
  • Get the third paragraph using Document.Sections[].Paragraph[] property and set its characters to IsSmallCaps by TextRange.CharacterFormat.IsSmallCaps property.
  • Save the document to a new Word file using Document.SaveToFile() method.

Note: AllCaps means to capitalize all the letters and set them to the same size, and IsSmallCaps means to capitalize all the letters but set the original majuscules bigger than the minuscules.

  • C#
  • VB.NET
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);
        }
    }
}

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

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 01 June 2023 01:17