C#添加、隐藏Word段落

通过C#我们可以向Word文档添加新的段落或者隐藏段落。需要使用Free Spire.Doc for .NET。这里使用的这个组件给程序员提供多种操作Word文档的方法。下面将具体介绍如何来添加和隐藏段落。本文转载自https://i.cnblogs.com/EditPosts.aspx?postid=6757884

 

1.添加段落效果对比:

前:


C#添加、隐藏Word段落

后:


C#添加、隐藏Word段落
 
 2.隐藏段落前后对比

前:


C#添加、隐藏Word段落
 后:


C#添加、隐藏Word段落
 下面是全部代码:

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)
        {   //该部分为插入新段落的代码
            Document document = new Document();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\向日葵.docx", FileFormat.Docx);

            Paragraph paraInserted = document.Sections[0].AddParagraph();
            TextRange textRange1 = paraInserted.AppendText("向日葵的花语是——太阳、光辉、高傲、忠诚、爱慕、沉默的爱。向日葵又叫望日莲,一个很美的名字");
            textRange1.CharacterFormat.TextColor = Color.Blue;
            textRange1.CharacterFormat.FontSize = 15;
            textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;
            document.SaveToFile("result.docx", FileFormat.Docx);


            //该部分为隐藏段落的代码
            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);

        }
    }
}