Word控件Spire.Doc 【段落处理】教程(十):如何在 C# 中将新段落插入到 Word 文档中

下面的示例演示如何将段落插入到文档中。

第 1 步:创建一个新文档并从文件加载。

Document document = new Document();document.LoadFromFile("sample.docx", FileFormat.Docx);

第 2 步:附加文本并设置字体格式。

Paragraph paraInserted = new Paragraph(document);TextRange textRange1 = paraInserted.AppendText("This is a inserted paragraph, I want to insert this paragraph in the start of document.");textRange1.CharacterFormat.TextColor = Color.Blue;textRange1.CharacterFormat.FontSize = 15;textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;

第 3 步:插入新段落。

document.Sections[0].Paragraphs.Insert(0, paraInserted);

第 4 步:将文档保存到文件中。

document.SaveToFile("result.docx", FileFormat.Docx);

有效截图

How to insert a new paragraph to word document in C#

完整代码

using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.Drawing;namespace InsertParagh{class Program{static void Main(string[] args){Document document = new Document();document.LoadFromFile("sample.docx", FileFormat.Docx);Paragraph paraInserted = document.Sections[0].AddParagraph();TextRange textRange1 = paraInserted.AppendText("This is a inserted paragraph, I want to insert this paragraph in the start of document.");textRange1.CharacterFormat.TextColor = Color.Blue;textRange1.CharacterFormat.FontSize = 15;textRange1.CharacterFormat.UnderlineStyle = UnderlineStyle.Dash;document.Sections[0].Paragraphs.Insert(0, document.Sections[0].Paragraphs[document.Sections[0].Paragraphs.Count - 1]);document.SaveToFile("result.docx", FileFormat.Docx);}}}


欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年4月12日
下一篇 2022年4月12日

相关推荐

发表回复

登录后才能评论