Word控件Spire.Doc 【段落处理】教程(十六):C#中如何设置段落前后的间距

发布于:2022-12-24 ⋅ 阅读:(397) ⋅ 点赞:(0)

使用 Spire.Doc,我们可以在 C# 中设置段落的格式。本文将重点演示如何在 C# 中设置段落前后的间距。

为新添加的段落设置段落前后的间距,该段落是通过paragraph.AppendHTML() 方法添加到一个新的空白word 文档中的。

Spire.Doc for.NET 最新下载(Q群767755948)icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

//create a new word document and add a section and paragraph to it.<font></font>
Document doc = new Document();<font></font>
Section sec = doc.AddSection();<font></font>
Paragraph para = sec.AddParagraph();<font></font>
<font></font>
//Add the text strings to the paragraph and set the style<font></font>
para.AppendHTML("<p>Add a new paragraph to the word and set the spacing</p>");<font></font>
para.ApplyStyle(BuiltinStyle.Heading1);<font></font>
<font></font>
//set the spacing before and after<font></font>
para.Format.BeforeAutoSpacing = false;<font></font>
para.Format.BeforeSpacing = 20;<font></font>
para.Format.AfterAutoSpacing = false;<font></font>
para.Format.AfterSpacing = 20;<font></font>
<font></font>
//save the document to file<font></font>
doc.SaveToFile("Result1.docx");<font></font>

using Spire.Doc;<font></font>
using Spire.Doc.Documents;<font></font>
using Spire.Doc.Fields;<font></font>
namespace SetSpacing<font></font>
{<font></font>
class Program<font></font>
{<font></font>
<font></font>
static void Main(string[] args)<font></font>
{<font></font>
//create a new word document and load the sample from file<font></font>
Document document = new Document();<font></font>
document.LoadFromFile("sample.docx", FileFormat.Docx);<font></font>
<font></font>
//Add the text strings to the paragraph and set the style<font></font>
Paragraph para = new Paragraph(document);<font></font>
TextRange textRange1 = para.AppendText("This is a inserted paragraph.");<font></font>
textRange1.CharacterFormat.TextColor = Color.Blue;<font></font>
textRange1.CharacterFormat.FontSize = 15;<font></font>
<font></font>
//set the spacing before and after<font></font>
para.Format.BeforeAutoSpacing = false;<font></font>
para.Format.BeforeSpacing = 10;<font></font>
para.Format.AfterAutoSpacing = false;<font></font>
para.Format.AfterSpacing = 10;<font></font>
<font></font>
//insert the added paragraph to the word document<font></font>
document.Sections[0].Paragraphs.Insert(1, para);<font></font>
<font></font>
//save the document to file<font></font>
document.SaveToFile("Result2.docx", FileFormat.Docx2010);<font></font>
}<font></font>
<font></font>
}<font></font>
}<font></font>


网站公告

今日签到

点亮在社区的每一天
去签到