IRONSOFTWAREHOME
添加段落
using IronWord;
using IronWord.Models;

// Load docx
WordDocument doc = new WordDocument("document.docx");

// Configure text
TextContent introText = new TextContent("This is an example paragraph with italic and bold styling.");

TextStyle italicStyle = new TextStyle()
{
    IsItalic = true
};
Run italicRun = new Run(new TextContent("Italic example sentence."));
italicRun.Style = italicStyle;

TextStyle boldStyle = new TextStyle()
{
    IsBold = true
};
Run boldRun = new Run(new TextContent("Bold example sentence."));
boldRun.Style = boldStyle;

Paragraph paragraph = new Paragraph();

// Add text
paragraph.AddText(introText);
paragraph.AddChild(italicRun);
paragraph.AddChild(boldRun);

// Add paragraph
doc.AddParagraph(paragraph);

// Export docx
doc.SaveAs("save_document.docx");
Imports IronWord
Imports IronWord.Models

' Load docx
Dim doc As New WordDocument("document.docx")

' Configure text
Dim introText As New TextContent("This is an example paragraph with italic and bold styling.")

Dim italicStyle As New TextStyle() With {
    .IsItalic = True
}
Dim italicRun As New Run(New TextContent("Italic example sentence."))
italicRun.Style = italicStyle

Dim boldStyle As New TextStyle() With {
    .IsBold = True
}
Dim boldRun As New Run(New TextContent("Bold example sentence."))
boldRun.Style = boldStyle

Dim paragraph As New Paragraph()

' Add text
paragraph.AddText(introText)
paragraph.AddChild(italicRun)
paragraph.AddChild(boldRun)

' Add paragraph
doc.AddParagraph(paragraph)

' Export docx
doc.SaveAs("save_document.docx")
NuGet Download
PM > Install-Package IronWord
添加段落

添加段落

IronWord 中的"添加段落"功能允许开发人员将整个段落插入到现有的 Word 文档中,从而提供了一种有效构建和组织内容的方法。 通过添加段落,开发人员可以将格式不同的多段文本合并成一个连贯的文本块。 此功能对于构建需要将文本分组并设置样式的动态文档(例如报告、文章或信件)尤其有用。

它在文本呈现方式上提供了灵活性,允许在单个段落中包含各种格式选项,如粗体、斜体和其他样式。 添加段落的功能有助于保持文档结构的清晰性,并确保内容组织得当,便于阅读。

Adding Paragraphs in C#

IronWord中的Paragraph类表示一个可以包含无样式文本和样式化内容的文本块。 在处理段落时,开发人员可以将普通的Run对象结合起来,以创建丰富的格式化文本块。

要直接将普通文本添加到段落中,请使用TextContent对象。 这非常适合不需要特定格式的纯文本。 对于样式化内容,创建一个包含RunTextStyle可指定格式化,如加粗、斜体、字体族、字体大小和颜色。

一旦AddChild方法将其添加到段落中。 此方法专为添加具有格式的Run对象而设计。 一个段落可以包含多个不同样式的文本行,从而在统一的文本块内实现灵活的文本格式设置。

在构造完所有所需内容的段落后,在AddParagraph将其插入到文档中。 此方法遵循文档层次结构:WordDocumentDocumentSectionParagraphRunTextContent,确保在保存文档时保持适当的结构和格式。

使用 IronWord 探索更多关于文档元素的信息
在 GitHub 上查看

准备开始了吗?

Nuget Downloads 56,401版本:2026.9刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户