Introduction to how to insert and hide paragraphs in C # into a Word document

Source: Internet
Author: User
This article mainly for you in detail how C # to insert a new paragraph and hidden paragraphs in Word document, with a certain reference value, interested in small partners can refer to

When you edit a Word document, we sometimes suddenly want to add a new piece of content, and when we view the Word document to others, we don't want to be seen by others. So how do you use C # programming to subtly insert or hide a paragraph? This article will share a good way to insert new paragraphs and hide paragraphs into Word documents.

The use of free spire.doc is here. NET component that allows developers to manipulate word documents with ease and flexibility.

To insert a new paragraph into a Word document

Step 1: Create a new document and load an existing document


Document document = new document ();d ocument. LoadFromFile (@ "C:\Users\Administrator\Desktop\ Sunflower. docx", fileformat.docx);

Step 2: insert a new paragraph and set the font format


Paragraph parainserted = document. Sections[0]. Addparagraph (); TextRange textRange1 = Parainserted.appendtext ("The flower of the sunflower is--the sun, the glory, the pride, the loyalty, the love, The Silence." Sunflower is also called Wang Zhi, a very beautiful name "); textRange1.CharacterFormat.TextColor = Color.blue;textrange1.characterformat.fontsize = 15; TextRange1.CharacterFormat.UnderlineStyle = Underlinestyle.dash;

Step 3: Save the document


Document. SaveToFile ("Result.docx", fileformat.docx);

Here is the comparison chart before and after the program runs:

Before running

After running

Steps to hide a paragraph

When you manipulate a Word document, we can use Microsoft Word to click the Font dialog box to hide the selected text. Use the following screen to see how Microsoft hides text:

However, free spire.doc for. NET can hide the specified text or the entire paragraph by setting the properties of the Characterformat.hidden, here are the detailed steps for you:

Step 1: Create a new document and load an existing document


Document doc = new document ();d OC. LoadFromFile (@ "C:\Users\Administrator\Desktop\ Daisy. docx", fileformat.docx);

Step 2: get the first section and last paragraph of a Word document


Section sec = Doc. Sections[0]; Paragraph para = sec. Paragraphs[sec. PARAGRAPHS.COUNT-1];

Step 3: call the FOR Loop statement to get all the textrange of the last paragraph and set the Characterformat.hidden property to True


for (int i = 0; i < para. childobjects.count;i++)  {  (para. Childobjects[i] as TextRange). Characterformat.hidden = true;  }

Step 4: Save the document


Doc. SaveToFile ("Result1.docx", fileformat.docx);

Here is the comparison chart before and after the program runs:

Before running

After running

C # full 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 that inserts a new paragraph document document = new document (); Document.  LoadFromFile (@ "C:\Users\Administrator\Desktop\ Sunflower. docx", fileformat.docx); Paragraph parainserted = document. Sections[0].  Addparagraph (); TextRange textRange1 = Parainserted.appendtext ("The flower of the sunflower is--the sun, the glory, the pride, the loyalty, the love, The Silence."  Sunflower is also called Wang Zhi, a very beautiful name ");  TextRange1.CharacterFormat.TextColor = Color.Blue;  TextRange1.CharacterFormat.FontSize = 15;  TextRange1.CharacterFormat.UnderlineStyle = Underlinestyle.dash; Document.  SaveToFile ("Result.docx", fileformat.docx);  This section is the code document for the hidden paragraph doc = new document (); Doc.  LoadFromFile (@ "C:\Users\Administrator\Desktop\ Daisy. docx", fileformat.docx); Section sec = Doc.  Sections[0]; Paragraph para = sec. Paragraphs[sec.  PARAGRAPHS.COUNT-1]; for (inti = 0; I < para. childobjects.count;i++) {(para. Childobjects[i] as TextRange).  Characterformat.hidden = true; } doc. SaveToFile ("Result1.docx", fileformat.docx); } }}

"Recommended"

1. Special recommendation : "PHP Programmer Toolkit" V0.1 version download

2. asp free Video Tutorial

3. Eon the ASP Basic video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.