翻譯|使用教程|編輯:胡濤|2022-05-16 11:23:05.127|閱讀 361 次
概述:本文解釋并描述了如何使用 Spire.Doc 在 C# 中的現有 word 文檔中插入新段落。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
借助 Spire.Doc,開發人員可以輕松地將多個 word 文檔合并為一個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);
有效截圖:
完整代碼:
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產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn