好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#向word文档插入新段落及隐藏段落的方法

编辑 word 文档时,我们有时会突然想增加一段新内容;而将word文档给他人浏览时,有些信息我们是不想让他人看到的。那么如何运用c#编程的方式巧妙地插入或隐藏 段落 呢?本文将与大家分享一种向word文档插入新段落及隐藏段落的好方法。

这里使用的是 free spire.doc for .net组件 ,该组件允许开发人员轻松并灵活地操作word文档。

向word文档插入一个新段落的操作步骤

步骤1: 新建一个文档并加载现有文档

?

document document = new document();

document.loadfromfile( @"c:\users\administrator\desktop\向日葵.docx" , fileformat.docx);

步骤2: 插入新段落并设置字体格式

?

paragraph parainserted = document.sections[0].addparagraph();

textrange textrange1 = parainserted.appendtext( "向日葵的花语是——太阳、光辉、高傲、忠诚、爱慕、沉默的爱。向日葵又叫望日莲,一个很美的名字" );

textrange1.characterformat.textcolor = color.blue;

textrange1.characterformat.fontsize = 15;

textrange1.characterformat.underlinestyle = underlinestyle.dash;

步骤3: 保存文档

?

document.savetofile( "result.docx" , fileformat.docx);

 以下是程序运行前后的对比图:

运行前

 

运行后

隐藏段落的操作步骤

当操作word文档时,我们可以通过microsoft word点击字体对话框来隐藏所选择的文本。请通过如下的屏幕截图来查看microsoft是如何隐藏文本的:

然而,free spire.doc for .net可以通过设置characterformat.hidden的属性来隐藏指定文本或整个段落,下面将为大家介绍详细步骤:

步骤1: 新建一个文档并加载现有文档

?

document doc = new document();

doc.loadfromfile( @"c:\users\administrator\desktop\雏菊.docx" , fileformat.docx);

步骤2: 获取word文档的第一个section和最后一段

?

section sec = doc.sections[0];

paragraph para = sec.paragraphs[sec.paragraphs.count - 1];

步骤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:\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);

 

  }

  }

}

这是我本次要分享的全部内容,感谢您的浏览。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

dy("nrwz");

查看更多关于C#向word文档插入新段落及隐藏段落的方法的详细内容...

  阅读:47次