刪除地址塊中的段落間距

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. 對每個段落應用精確的行間距規則

對每一行,構建一個 Paragraph 並使用 Exact 規則將其 LineSpacing 設置為一個 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

帶有緊密行距的地址區塊

如果您更喜歡顯式值而不是 Exact 規則,para.SpacingBetweenLines = 210 也會產生緊湊的行距。

除錯提示

  • 勿使用 ParagraphSpacing: 該類別在 2026.4.1 中不可用,即使它出現在某些範例中。
  • 避免將 SpacingBeforeSpacingAfter 歸零:2026.4.1 中,將兩者設置為 0 會使段落文字不可見,而段落仍留在文件中。 此行為正在調查中; 請使用精確的行間距規則。

警告在IronWord 2026.4.1中將 SpacingBeforeSpacingAfter 設置為 0 會隱藏段落文字。 使用上面顯示的 Exact 行距規則來可靠地控制間距。)}

欲知更多文字定位,請參見新增文字操作指南

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 48,355 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明嗎? PM > Install-Package IronWord
運行範例觀看您的資料變成Word檔。