下面的示例演示如何将段落插入到文档中。
第 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);
有效截图:

完整代码:
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进行处理,非常感谢!