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

使用 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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值