How to hide word paragraph in C#

When we operate the word document, we can hide some texts or the whole paragraph on the Microsoft Word after click the font box to hide the selected texts. Please check the screenshot of how Microsoft hide the text as below:

How to hide word paragraph in C#

Spire.Doc offers a CharacterFormat.Hidden property to enable developers to hide the specified text or the whole paragraph. This article will demonstrate how to hide the whole paragraph on the word document in C#.

Here comes to the code snippets:

Step 1: Create a new word document and load from file.

Document doc = new Document();
doc.LoadFromFile("sample.docx");

Step 2: Get the first section and the first paragraph from the word document.

Section sec = doc.Sections[0];
Paragraph para = sec.Paragraphs[0];

Step 3: Get the first TextRange and set CharacterFormat.Hidden property as true to hide the texts.

(para.ChildObjects[0] as TextRange).CharacterFormat.Hidden = true;

Step 4: Save the document to file.

doc.SaveToFile("result.docx", FileFormat.Docx);

Effective screenshot after the paragraph is hidden:

How to hide word paragraph in C#

After we set the options to display the hidden text, the hidden text will show like this:

How to hide word paragraph in C#

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace HideParagh
{
 class Program
    {
     
      static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("sample.docx");
            Section sec = doc.Sections[0];
            Paragraph para = sec.Paragraphs[0];

            (para.ChildObjects[0] as TextRange).CharacterFormat.Hidden = true;

            doc.SaveToFile("result.docx", FileFormat.Docx);
        }
    }
}