去除地址块中段落之间的空隙
This article was translated from English: Does it need improvement?
Translated
View the article in English
当您堆叠较短的行如地址块时,IronWord 的默认段落间距在每行之间留有可见的空隙。要消除该间隙,请在每个段落上设置精确的行间距规则,以便每行获得固定的高度,没有额外的填充。 在IronWord 2026.4.1上验证。
解决方案
1. 创建文档并列出行
开始一个新的WordDocument并收集您想要堆叠在一起的行。
WordDocument doc = new WordDocument();
string[] addressLines =
{
"Max Mustermann",
"Musterstraße 42",
"12345 Musterstadt",
"Deutschland"
};
WordDocument doc = new WordDocument();
string[] addressLines =
{
"Max Mustermann",
"Musterstraße 42",
"12345 Musterstadt",
"Deutschland"
};
Dim doc As New WordDocument()
Dim addressLines As String() = {
"Max Mustermann",
"Musterstraße 42",
"12345 Musterstadt",
"Deutschland"
}
$vbLabelText
$csharpLabel
2. 为每个段落应用精确的行间距规则
对于每一行,生成一个LineSpacing对象。 这为每一行提供了固定的高度,并消除了行间的间隙。
foreach (string line in addressLines)
{
Paragraph para = new Paragraph(new TextContent(line));
para.LineSpacing = new IronSoftware.Abstractions.Word.LineSpacing()
{
LineSpacingRule = IronSoftware.Abstractions.Word.LineSpacingRules.Exact,
};
doc.AddParagraph(para);
}
foreach (string line in addressLines)
{
Paragraph para = new Paragraph(new TextContent(line));
para.LineSpacing = new IronSoftware.Abstractions.Word.LineSpacing()
{
LineSpacingRule = IronSoftware.Abstractions.Word.LineSpacingRules.Exact,
};
doc.AddParagraph(para);
}
Dim para As Paragraph
For Each line As String In addressLines
para = New Paragraph(New TextContent(line))
para.LineSpacing = New IronSoftware.Abstractions.Word.LineSpacing() With {
.LineSpacingRule = IronSoftware.Abstractions.Word.LineSpacingRules.Exact
}
doc.AddParagraph(para)
Next
$vbLabelText
$csharpLabel
行间距类型存在于IronSoftware.Abstractions.Word命名空间中。 可用的类型和属性因版本而异,因此请根据您运行的版本进行确认。
3. 保存文档
doc.SaveAs("address3.docx");
doc.SaveAs("address3.docx");
doc.SaveAs("address3.docx")
$vbLabelText
$csharpLabel

如果您更喜欢明确的值而不是para.SpacingBetweenLines = 210也会产生紧凑的间距。
调试提示
- 不要使用
2026.4.1中不可用。 - 避免将
0会使段落文本不可见,但段落仍留在文档中。 这种行为正在调查中; 请使用精确的行间距规则。
警告在IronWord 2026.4.1中将
0会隐藏段落文本。 使用上面显示的Exact行间距规则来可靠地控制间距。)]
有关放置文本的更多信息,请参阅 添加文本教程。
准备开始了吗?
Nuget 下载 48,355 | 版本: 2026.7 刚刚发布

