在 IronWord 中使用 SaveAs 後標題消失

This article was translated from English: Does it need improvement?
Translated
View the article in English

請注意此行為僅在一個環境中被回報,且未獲得IronWord更新日誌的證實——其他IronWord指南均將SaveAs()作為標準的儲存呼叫,且未回報頁首遺失問題。 請將此視為可能與特定環境相關的異常,而非已確認的產品缺陷,並在套用下方的解決方法之前先在您自己的環境中驗證。

在某些環境中,當您使用IronWord填入DOCX範本並以SaveAs()寫入結果時,即使佔位符正確合併,範本的頁首仍可能在生成的檔案中消失。

Header (e.g. "City Attorney Cover Sheet") is visible in the original DOCX
but missing in the generated file after calling SaveAs().

有未取代佔位符和無頁首的Word範本

在觀察到此問題的環境中,以Save()而非SaveAs()寫入文件時,頁首得以保留。 這一點尚未在其他環境或IronWord的更新日誌中獲得證實——其他IronWord指南均將SaveAs()作為標準的儲存呼叫,且不存在頁首遺失問題,因此請將此視為特定於環境的情況,而非已確認的產品缺陷。

解決方案

1. 載入範本

使用WordDocument開啟來源DOCX。

using IronWord;
using System.Collections.Generic;

var templatePath = @"C:\Templates\CityAttorneyCoverSheetTemplate.docx";
var outputPath   = @"C:\Output\merged.docx";

var doc = new WordDocument(templatePath);
using IronWord;
using System.Collections.Generic;

var templatePath = @"C:\Templates\CityAttorneyCoverSheetTemplate.docx";
var outputPath   = @"C:\Output\merged.docx";

var doc = new WordDocument(templatePath);
Imports IronWord
Imports System.Collections.Generic

Dim templatePath As String = "C:\Templates\CityAttorneyCoverSheetTemplate.docx"
Dim outputPath As String = "C:\Output\merged.docx"

Dim doc As New WordDocument(templatePath)
$vbLabelText   $csharpLabel

2. 替換佔位符

通過doc.Texts合併您的值,這是範本取代的建議方法。

var replacements = new Dictionary<string, string>
{
    ["{CaseNumber}"] = "2026-CR-12345",
    ["{DefendantName}"] = "John Doe",
    ["{DefendantAge}"] = "34",
    ["{DefendantAttorney}"] = "Jane Smith",
};

foreach (var kv in replacements)
{
    doc.Texts.ForEach(t => t.Replace(kv.Key, kv.Value));
}
var replacements = new Dictionary<string, string>
{
    ["{CaseNumber}"] = "2026-CR-12345",
    ["{DefendantName}"] = "John Doe",
    ["{DefendantAge}"] = "34",
    ["{DefendantAttorney}"] = "Jane Smith",
};

foreach (var kv in replacements)
{
    doc.Texts.ForEach(t => t.Replace(kv.Key, kv.Value));
}
Imports System.Collections.Generic

Dim replacements As New Dictionary(Of String, String) From {
    {"{CaseNumber}", "2026-CR-12345"},
    {"{DefendantName}", "John Doe"},
    {"{DefendantAge}", "34"},
    {"{DefendantAttorney}", "Jane Smith"}
}

For Each kv In replacements
    doc.Texts.ForEach(Sub(t) t.Replace(kv.Key, kv.Value))
Next
$vbLabelText   $csharpLabel

doc.Texts上迭代每個條目,將文件主體中的每個佔位符標記交換為其合併值。

3. 使用 Save() 不要使用 SaveAs()

在觀察到此問題的環境中,以doc.Save(outputPath)寫出輸出可保持頁首完整,而doc.SaveAs(outputPath)則會遺失頁首。 請將此視為可嘗試的解決方法,而非兩種方法之間已確認的通用行為差異。

doc.Save(outputPath);
doc.Save(outputPath);
doc.Save(outputPath)
$vbLabelText   $csharpLabel

已合併的Word文件,頁首保留且佔位符已填充

筆記

  • 如果您的工作流程需要SaveAs()請在最新的IronWord版本中驗證頁首在您的環境中是否保留。
  • 如果問題再次出現: 鎖定到先前工作的版本作為短期的調解方案,同時正在調查行為。

請注意即使在遍歷過程中標頭丟失時,佔位符仍然會被找到並替換,因此成功的合併本身並不能確認標頭已保留。 打開輸出並檢查。

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檔。