With C# we can add new paragraphs or hide paragraphs to Word documents. Requires Free Spire.Doc for .NET. The component used here provides the programmer with a variety of ways to manipulate Word documents. The following will describe in detail how to add and hide paragraphs. This article is reproduced from https://i.cnblogs.com/EditPosts.aspx?postid=6757884
1. Add paragraph effect comparison:
forward:
back:
2. Contrast before and after the hidden paragraph
forward:
back:
Here is all the code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; namespace insert_new_paragraph_and_hide { class Program { static void Main(string[] args) { //This part is the code to insert a new paragraph Document document = new Document(); document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx); Paragraph paraInserted = document.Sections[0].AddParagraph(); TextRange textRange1 = paraInserted.AppendText("The flower language of sunflower is - sun, brilliance, pride, loyalty, admiration, silent love. Sunflower is also called Wang Rilian, a beautiful name"); textRange1.CharacterFormat.TextColor = Color.Blue; textRange1.CharacterFormat.FontSize = 15; textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash; document.SaveToFile("result.docx", FileFormat.Docx); //This part is the code of the hidden paragraph Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\雏菊.docx", FileFormat.Docx); Section sec = doc.Sections[0]; Paragraph para = sec.Paragraphs[sec.Paragraphs.Count - 1]; for (int i = 0; i < para.ChildObjects.Count;i++) { (para.ChildObjects[i] as TextRange).CharacterFormat.Hidden = true; } doc.SaveToFile("result1.docx", FileFormat.Docx); } } }