步骤3:调用for循环语句来获取最后一段的所有TextRange并将CharacterFormat.Hidden的属性设置为true
for (int i = 0; i < para.ChildObjects.Count;i++)
{
(para.ChildObjects[i] as TextRange).CharacterFormat.Hidden = true;
}
步骤4:保存文档
doc.SaveToFile("result1.docx", FileFormat.Docx);
以下是程序运行前后的对比图:
运行前

运行后

C#完整代码
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:UsersAdministratorDesktop向日葵.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:UsersAdministratorDesktop雏菊.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);
}
}
}
这是我本次要分享的全部内容,感谢您的浏览。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。
注:相关教程知识阅读请移步到c#教程频道。










