如何使用 IronWord 在 C# 中建立可填寫的表格範本
使用IronWord庫,透過建立基於表格的佈局和占位符文字字段,在 C# 中建立可填寫的 Word 表單範本。 然後,您可以透過程式設計方式向其中填入實際數據,並可選擇將其轉換為 PDF 格式。
透過結構化表格收集資訊對於各行各業的資料收集至關重要——從人力資源部門處理求職申請到醫療保健提供者收集患者資訊。 在 .NET 應用程式中,以程式設計方式建立可填寫表單範本可以節省時間並確保 Word 文件之間的一致性。 本教學示範如何使用 C# 和 IronWord在 Word 文件中建立可填寫的表單範本,IronWord 是一個 .NET Word 函式庫,可在不依附 Microsoft Office 的情況下產生和編輯 DOCX 檔案。 最後,您將獲得一個完整的求職申請表模板,可以填寫數據,甚至可以將 Word 文件轉換為 PDF 格式進行分發。
什麼是 Word 文件中的可填表格範本?
可填寫表格範本是結構化的 Word 文件,設計有指定區域供使用者輸入文字和其他資料。 這些範本使用表格和占位符文字欄位來建立有序的佈局,您可以透過程式設計方式或透過互動式表單手動填入實際資料。 在使用 .NET 應用程式時,您可以將IronWord等程式庫與IronPDF 等其他 Iron Software 產品結合使用,以產生 PDF 文件,從而建立完整的文件自動化解決方案。
Microsoft Word 支援各種互動欄位的內容控制,包括純文字內容控制、富文字內容控制、檢查方塊內容控制、下拉清單內容控制、組合框內容控制、日期選擇器內容控制和圖片內容控制。 雖然原生表單欄位可以建立互動式表單,但使用佔位符文字的基於範本的方法可以為 Web 應用程式和伺服器環境中的文件產生提供更大的靈活性。 這種靈活性在建立企業工作流程時尤其有用,該工作流程需要處理PDF 數位簽章或其他文件類型以及 Word 表單。
常見的應用程式包括
- 具備可填欄位的工作申請表與員工入職表格
- 收集資料的客戶註冊和回饋調查
- 帶有文字方塊和複選框控制項的醫療資訊收集和知情同意書
- 具有可變文字欄位的合約範本
- 可匯出為 PDF 文件的訂單表單與發票
這些表單的結構化特性使其非常適合自動化處理。 基於範本的表單生成功能,讓您的應用程式能從單一主範本產生數十甚至數百份格式一致的文件,從而減少錯誤並消除重複的手動作業。 此方法同樣適用於從簡單的單頁表單,到包含條件邏輯、驗證規則及分支結構的多頁文件。
如何透過 NuGet 安裝 IronWord?
若要開始使用 IronWord,請建立一個新的 .NET 控制台應用程式並安裝此套件。 您可以透過 .NET CLI 從 NuGet 安裝 IronWord:
dotnet new console -n WordFormTemplate
cd WordFormTemplate
dotnet add package IronWord
dotnet new console -n WordFormTemplate
cd WordFormTemplate
dotnet add package IronWord
您亦可透過 Visual Studio 中的 NuGet 套件管理員,搜尋"IronWord"進行安裝。此 .NET Word程式庫無需在系統中安裝 Microsoft Office 或 Word Interop 即可運作,因此非常適合在無法使用 Office 的伺服器端及雲端部署環境中使用。
Visual Studio 中的 NuGet 套件管理器視窗顯示了 IronWord 套件的搜尋結果和安裝介面
安裝完成後,請在進行任何 API 呼叫前先輸入授權金鑰。 您可從 IronWord 授權頁面取得免費試用授權金鑰,或直接在程式碼中設定金鑰:
using IronWord;
License.LicenseKey = "YOUR-LICENSE-KEY";
using IronWord;
License.LicenseKey = "YOUR-LICENSE-KEY";
IRON VB CONVERTER ERROR developers@ironsoftware.com
安裝並啟用授權後,您即可開始透過程式設計建立表單範本。
如何在 C# 中建立可填寫的表單範本?
如何利用表格和佔位符來建構表單?
表格為井然有序的表單佈局奠定基礎,並確保表格儲存格對齊正確。 文件物件用於將表格和表單欄位新增至 WORD 文件中。 以下程式碼範例示範如何使用 IronWord 的文件 API 建立包含標籤與輸入佔位符的基本表單結構:
using IronWord;
using IronWord.Models;
// Apply your license key
License.LicenseKey = "YOUR-LICENSE-KEY";
// Create a new document instance
WordDocument doc = new WordDocument();
// Create the form header
Paragraph header = new Paragraph();
var headerText = new IronWord.Models.TextContent("Job Application Form")
{
Style = new TextStyle
{
TextFont = new Font() { FontFamily = "Arial", FontSize = 24 },
IsBold = true,
Color = new Color("#1a1a1a")
}
};
header.AddText(headerText);
doc.AddParagraph(header);
// Add spacing paragraph
doc.AddParagraph(new Paragraph());
// Create a table for personal information section
Table personalInfoTable = new Table(4, 2);
// Set column labels and placeholder text fields
personalInfoTable.Rows[0].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Full Name:")));
personalInfoTable.Rows[0].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{FullName}")));
personalInfoTable.Rows[1].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Email Address:")));
personalInfoTable.Rows[1].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{Email}")));
personalInfoTable.Rows[2].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Phone Number:")));
personalInfoTable.Rows[2].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{Phone}")));
personalInfoTable.Rows[3].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Date of Application:")));
personalInfoTable.Rows[3].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{ApplicationDate}")));
doc.AddTable(personalInfoTable);
// Save the template to a file with descriptive name
doc.SaveAs("JobApplicationTemplate.docx");
Console.WriteLine("Form template created successfully!");
using IronWord;
using IronWord.Models;
// Apply your license key
License.LicenseKey = "YOUR-LICENSE-KEY";
// Create a new document instance
WordDocument doc = new WordDocument();
// Create the form header
Paragraph header = new Paragraph();
var headerText = new IronWord.Models.TextContent("Job Application Form")
{
Style = new TextStyle
{
TextFont = new Font() { FontFamily = "Arial", FontSize = 24 },
IsBold = true,
Color = new Color("#1a1a1a")
}
};
header.AddText(headerText);
doc.AddParagraph(header);
// Add spacing paragraph
doc.AddParagraph(new Paragraph());
// Create a table for personal information section
Table personalInfoTable = new Table(4, 2);
// Set column labels and placeholder text fields
personalInfoTable.Rows[0].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Full Name:")));
personalInfoTable.Rows[0].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{FullName}")));
personalInfoTable.Rows[1].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Email Address:")));
personalInfoTable.Rows[1].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{Email}")));
personalInfoTable.Rows[2].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Phone Number:")));
personalInfoTable.Rows[2].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{Phone}")));
personalInfoTable.Rows[3].Cells[0].AddParagraph(new Paragraph(new IronWord.Models.TextContent("Date of Application:")));
personalInfoTable.Rows[3].Cells[1].AddParagraph(new Paragraph(new IronWord.Models.TextContent("{ApplicationDate}")));
doc.AddTable(personalInfoTable);
// Save the template to a file with descriptive name
doc.SaveAs("JobApplicationTemplate.docx");
Console.WriteLine("Form template created successfully!");
Imports IronWord
Imports IronWord.Models
' Apply your license key
License.LicenseKey = "YOUR-LICENSE-KEY"
' Create a new document instance
Dim doc As New WordDocument()
' Create the form header
Dim header As New Paragraph()
Dim headerText = New IronWord.Models.TextContent("Job Application Form") With {
.Style = New TextStyle With {
.TextFont = New Font() With {.FontFamily = "Arial", .FontSize = 24},
.IsBold = True,
.Color = New Color("#1a1a1a")
}
}
header.AddText(headerText)
doc.AddParagraph(header)
' Add spacing paragraph
doc.AddParagraph(New Paragraph())
' Create a table for personal information section
Dim personalInfoTable As New Table(4, 2)
' Set column labels and placeholder text fields
personalInfoTable.Rows(0).Cells(0).AddParagraph(New Paragraph(New IronWord.Models.TextContent("Full Name:")))
personalInfoTable.Rows(0).Cells(1).AddParagraph(New Paragraph(New IronWord.Models.TextContent("{FullName}")))
personalInfoTable.Rows(1).Cells(0).AddParagraph(New Paragraph(New IronWord.Models.TextContent("Email Address:")))
personalInfoTable.Rows(1).Cells(1).AddParagraph(New Paragraph(New IronWord.Models.TextContent("{Email}")))
personalInfoTable.Rows(2).Cells(0).AddParagraph(New Paragraph(New IronWord.Models.TextContent("Phone Number:")))
personalInfoTable.Rows(2).Cells(1).AddParagraph(New Paragraph(New IronWord.Models.TextContent("{Phone}")))
personalInfoTable.Rows(3).Cells(0).AddParagraph(New Paragraph(New IronWord.Models.TextContent("Date of Application:")))
personalInfoTable.Rows(3).Cells(1).AddParagraph(New Paragraph(New IronWord.Models.TextContent("{ApplicationDate}")))
doc.AddTable(personalInfoTable)
' Save the template to a file with descriptive name
doc.SaveAs("JobApplicationTemplate.docx")
Console.WriteLine("Form template created successfully!")
此程式碼使用 WordDocument 類別建立新的文件實例,並使用 Table 類別建立結構化表單。 每一行的第一個儲存格包含一個標籤,第二個儲存格包含一個占位符 (以大括弧包覆)。 TextContent 類別處理純文字內容,而 TextStyle 應用程式格式。 佔位符語法 {FieldName} 標記稍後將以實際資料取代文字的區域。 有關邊框、底色及欄位寬度等進階格式設定選項,請參閱 IronWord 文件。

多欄位表單的最佳實務有哪些?
以下程式碼範例展示如何建立包含多個區段的完整求職申請表。 輔助方法可減少重複,並使範本易於擴充,例如新增工作經歷或推薦人等區塊:
using IronWord;
using IronWord.Models;
License.LicenseKey = "YOUR-LICENSE-KEY";
// Create an empty document to start fresh
WordDocument doc = new WordDocument();
// Document title with rich text styling
Paragraph title = new Paragraph();
TextContent titleText = new TextContent("Employment Application Form");
titleText.Style = new TextStyle()
{
TextFont = new Font() { FontFamily = "Arial", FontSize = 28 },
IsBold = true
};
// Center the paragraph
title.Alignment = IronWord.Models.Enums.TextAlignment.Center;
title.AddText(titleText);
doc.AddParagraph(title);
doc.AddParagraph(new Paragraph());
// Section 1: Personal Information with text box style fields
AddSectionHeader(doc, "Personal Information");
Table personalTable = new Table(5, 2);
SetFormRow(personalTable, 0, "Full Name:", "{FullName}");
SetFormRow(personalTable, 1, "Email:", "{Email}");
SetFormRow(personalTable, 2, "Phone:", "{Phone}");
SetFormRow(personalTable, 3, "Address:", "{Address}");
SetFormRow(personalTable, 4, "Date of Birth:", "{DOB}");
doc.AddTable(personalTable);
doc.AddParagraph(new Paragraph());
// Section 2: Position Details
AddSectionHeader(doc, "Position Details");
Table positionTable = new Table(3, 2);
SetFormRow(positionTable, 0, "Position Applied For:", "{Position}");
SetFormRow(positionTable, 1, "Available Start Date:", "{StartDate}");
SetFormRow(positionTable, 2, "Desired Salary:", "{Salary}");
doc.AddTable(positionTable);
doc.AddParagraph(new Paragraph());
// Section 3: Education Background
AddSectionHeader(doc, "Education Background");
Table educationTable = new Table(3, 2);
SetFormRow(educationTable, 0, "高的est Degree:", "{Degree}");
SetFormRow(educationTable, 1, "Institution:", "{Institution}");
SetFormRow(educationTable, 2, "Graduation Year:", "{GradYear}");
doc.AddTable(educationTable);
doc.AddParagraph(new Paragraph());
// Section 4: Declaration - certification statement
Paragraph declaration = new Paragraph();
declaration.AddText(new TextContent("Applicant certifies that the information provided is accurate and complete."));
doc.AddParagraph(declaration);
doc.AddParagraph(new Paragraph());
Table signatureTable = new Table(1, 2);
SetFormRow(signatureTable, 0, "Signature:", "{Signature}");
doc.AddTable(signatureTable);
// Save template file
doc.SaveAs("CompleteJobApplication.docx");
Console.WriteLine("Complete job application form created!");
// Helper method to add styled section headers
void AddSectionHeader(WordDocument document, string headerText)
{
Paragraph sectionHeader = new Paragraph();
TextContent sectionText = new TextContent(headerText);
sectionText.Style = new TextStyle()
{
TextFont = new Font() { FontFamily = "Arial", FontSize = 14 },
IsBold = true,
Color = new Color("#333333")
};
sectionHeader.AddText(sectionText);
document.AddParagraph(sectionHeader);
}
// Helper method to populate table cells with label and placeholder
void SetFormRow(Table table, int rowIndex, string label, string placeholder)
{
table.Rows[rowIndex].Cells[0].AddParagraph(new Paragraph(new TextContent(label)));
table.Rows[rowIndex].Cells[1].AddParagraph(new Paragraph(new TextContent(placeholder)));
}
using IronWord;
using IronWord.Models;
License.LicenseKey = "YOUR-LICENSE-KEY";
// Create an empty document to start fresh
WordDocument doc = new WordDocument();
// Document title with rich text styling
Paragraph title = new Paragraph();
TextContent titleText = new TextContent("Employment Application Form");
titleText.Style = new TextStyle()
{
TextFont = new Font() { FontFamily = "Arial", FontSize = 28 },
IsBold = true
};
// Center the paragraph
title.Alignment = IronWord.Models.Enums.TextAlignment.Center;
title.AddText(titleText);
doc.AddParagraph(title);
doc.AddParagraph(new Paragraph());
// Section 1: Personal Information with text box style fields
AddSectionHeader(doc, "Personal Information");
Table personalTable = new Table(5, 2);
SetFormRow(personalTable, 0, "Full Name:", "{FullName}");
SetFormRow(personalTable, 1, "Email:", "{Email}");
SetFormRow(personalTable, 2, "Phone:", "{Phone}");
SetFormRow(personalTable, 3, "Address:", "{Address}");
SetFormRow(personalTable, 4, "Date of Birth:", "{DOB}");
doc.AddTable(personalTable);
doc.AddParagraph(new Paragraph());
// Section 2: Position Details
AddSectionHeader(doc, "Position Details");
Table positionTable = new Table(3, 2);
SetFormRow(positionTable, 0, "Position Applied For:", "{Position}");
SetFormRow(positionTable, 1, "Available Start Date:", "{StartDate}");
SetFormRow(positionTable, 2, "Desired Salary:", "{Salary}");
doc.AddTable(positionTable);
doc.AddParagraph(new Paragraph());
// Section 3: Education Background
AddSectionHeader(doc, "Education Background");
Table educationTable = new Table(3, 2);
SetFormRow(educationTable, 0, "高的est Degree:", "{Degree}");
SetFormRow(educationTable, 1, "Institution:", "{Institution}");
SetFormRow(educationTable, 2, "Graduation Year:", "{GradYear}");
doc.AddTable(educationTable);
doc.AddParagraph(new Paragraph());
// Section 4: Declaration - certification statement
Paragraph declaration = new Paragraph();
declaration.AddText(new TextContent("Applicant certifies that the information provided is accurate and complete."));
doc.AddParagraph(declaration);
doc.AddParagraph(new Paragraph());
Table signatureTable = new Table(1, 2);
SetFormRow(signatureTable, 0, "Signature:", "{Signature}");
doc.AddTable(signatureTable);
// Save template file
doc.SaveAs("CompleteJobApplication.docx");
Console.WriteLine("Complete job application form created!");
// Helper method to add styled section headers
void AddSectionHeader(WordDocument document, string headerText)
{
Paragraph sectionHeader = new Paragraph();
TextContent sectionText = new TextContent(headerText);
sectionText.Style = new TextStyle()
{
TextFont = new Font() { FontFamily = "Arial", FontSize = 14 },
IsBold = true,
Color = new Color("#333333")
};
sectionHeader.AddText(sectionText);
document.AddParagraph(sectionHeader);
}
// Helper method to populate table cells with label and placeholder
void SetFormRow(Table table, int rowIndex, string label, string placeholder)
{
table.Rows[rowIndex].Cells[0].AddParagraph(new Paragraph(new TextContent(label)));
table.Rows[rowIndex].Cells[1].AddParagraph(new Paragraph(new TextContent(placeholder)));
}
Imports IronWord
Imports IronWord.Models
License.LicenseKey = "YOUR-LICENSE-KEY"
' Create an empty document to start fresh
Dim doc As New WordDocument()
' Document title with rich text styling
Dim title As New Paragraph()
Dim titleText As New TextContent("Employment Application Form")
titleText.Style = New TextStyle() With {
.TextFont = New Font() With {.FontFamily = "Arial", .FontSize = 28},
.IsBold = True
}
' Center the paragraph
title.Alignment = IronWord.Models.Enums.TextAlignment.Center
title.AddText(titleText)
doc.AddParagraph(title)
doc.AddParagraph(New Paragraph())
' Section 1: Personal Information with text box style fields
AddSectionHeader(doc, "Personal Information")
Dim personalTable As New Table(5, 2)
SetFormRow(personalTable, 0, "Full Name:", "{FullName}")
SetFormRow(personalTable, 1, "Email:", "{Email}")
SetFormRow(personalTable, 2, "Phone:", "{Phone}")
SetFormRow(personalTable, 3, "Address:", "{Address}")
SetFormRow(personalTable, 4, "Date of Birth:", "{DOB}")
doc.AddTable(personalTable)
doc.AddParagraph(New Paragraph())
' Section 2: Position Details
AddSectionHeader(doc, "Position Details")
Dim positionTable As New Table(3, 2)
SetFormRow(positionTable, 0, "Position Applied For:", "{Position}")
SetFormRow(positionTable, 1, "Available Start Date:", "{StartDate}")
SetFormRow(positionTable, 2, "Desired Salary:", "{Salary}")
doc.AddTable(positionTable)
doc.AddParagraph(New Paragraph())
' Section 3: Education Background
AddSectionHeader(doc, "Education Background")
Dim educationTable As New Table(3, 2)
SetFormRow(educationTable, 0, "Highest Degree:", "{Degree}")
SetFormRow(educationTable, 1, "Institution:", "{Institution}")
SetFormRow(educationTable, 2, "Graduation Year:", "{GradYear}")
doc.AddTable(educationTable)
doc.AddParagraph(New Paragraph())
' Section 4: Declaration - certification statement
Dim declaration As New Paragraph()
declaration.AddText(New TextContent("Applicant certifies that the information provided is accurate and complete."))
doc.AddParagraph(declaration)
doc.AddParagraph(New Paragraph())
Dim signatureTable As New Table(1, 2)
SetFormRow(signatureTable, 0, "Signature:", "{Signature}")
doc.AddTable(signatureTable)
' Save template file
doc.SaveAs("CompleteJobApplication.docx")
Console.WriteLine("Complete job application form created!")
' Helper method to add styled section headers
Sub AddSectionHeader(document As WordDocument, headerText As String)
Dim sectionHeader As New Paragraph()
Dim sectionText As New TextContent(headerText)
sectionText.Style = New TextStyle() With {
.TextFont = New Font() With {.FontFamily = "Arial", .FontSize = 14},
.IsBold = True,
.Color = New Color("#333333")
}
sectionHeader.AddText(sectionText)
document.AddParagraph(sectionHeader)
End Sub
' Helper method to populate table cells with label and placeholder
Sub SetFormRow(table As Table, rowIndex As Integer, label As String, placeholder As String)
table.Rows(rowIndex).Cells(0).AddParagraph(New Paragraph(New TextContent(label)))
table.Rows(rowIndex).Cells(1).AddParagraph(New Paragraph(New TextContent(placeholder)))
End Sub
此代碼會建立一個多節的表單範本,組織成合邏輯的部分。 輔助方法 AddSectionHeader 和 SetFormRow 減少了程式碼重複。 Table 建構函數接受行和列參數,而 Rows 和 Cells 集合提供單一表格單元格的存取。 每一部分都包含一個有風格的標題,接著是一個有可填欄位的表格。 這種模組化設計讓您能輕鬆根據需求變更,新增日期選擇器欄位、下拉式選單選項或核取方塊區塊。 您亦可使用圖片控件嵌入圖片,並使用日期控件新增日期選擇器欄位。 有關在 IronWord 中處理段落的更多資訊,請參閱操作指南。
! Microsoft Word 文檔,顯示一個求職申請表模板,其中包含姓名、電子郵件地址、電話號碼和申請日期等字段,以表格形式呈現。
如何將資料填入表單範本?
何謂文字替換法?
一旦建立好範本,即可透過文字替換功能輕鬆填入實際資料。 以下程式碼片段示範如何透過載入範本檔案並遍歷所有文字元素,將範例申請人資訊填入表單:
using IronWord;
License.LicenseKey = "YOUR-LICENSE-KEY";
// Load the template document
WordDocument doc = new WordDocument("CompleteJobApplication.docx");
// Define replacement data - example using John Doe as applicant
var applicantData = new Dictionary<string, string>
{
{ "{FullName}", "John Doe" },
{ "{Email}", "john.doe@email.com" },
{ "{Phone}", "(555) 123-4567" },
{ "{Address}", "123 Main Street, Chicago, IL 60601" },
{ "{DOB}", "March 15, 1992" },
{ "{Position}", "Senior Software Developer" },
{ "{StartDate}", "January 15, 2025" },
{ "{Salary}", "$95,000" },
{ "{Degree}", "Bachelor of Science in Computer Science" },
{ "{Institution}", "University of Illinois" },
{ "{GradYear}", "2014" },
{ "{Signature}", "John Doe" }
};
// Replace all placeholders with actual values
foreach (var field in applicantData)
{
doc.Texts.ForEach(text => text.Replace(field.Key, field.Value));
}
// Save the filled form to a new file
doc.SaveAs("JohnDoe_Application.docx");
Console.WriteLine("Application form filled successfully!");
using IronWord;
License.LicenseKey = "YOUR-LICENSE-KEY";
// Load the template document
WordDocument doc = new WordDocument("CompleteJobApplication.docx");
// Define replacement data - example using John Doe as applicant
var applicantData = new Dictionary<string, string>
{
{ "{FullName}", "John Doe" },
{ "{Email}", "john.doe@email.com" },
{ "{Phone}", "(555) 123-4567" },
{ "{Address}", "123 Main Street, Chicago, IL 60601" },
{ "{DOB}", "March 15, 1992" },
{ "{Position}", "Senior Software Developer" },
{ "{StartDate}", "January 15, 2025" },
{ "{Salary}", "$95,000" },
{ "{Degree}", "Bachelor of Science in Computer Science" },
{ "{Institution}", "University of Illinois" },
{ "{GradYear}", "2014" },
{ "{Signature}", "John Doe" }
};
// Replace all placeholders with actual values
foreach (var field in applicantData)
{
doc.Texts.ForEach(text => text.Replace(field.Key, field.Value));
}
// Save the filled form to a new file
doc.SaveAs("JohnDoe_Application.docx");
Console.WriteLine("Application form filled successfully!");
Imports IronWord
License.LicenseKey = "YOUR-LICENSE-KEY"
' Load the template document
Dim doc As New WordDocument("CompleteJobApplication.docx")
' Define replacement data - example using John Doe as applicant
Dim applicantData As New Dictionary(Of String, String) From {
{"{FullName}", "John Doe"},
{"{Email}", "john.doe@email.com"},
{"{Phone}", "(555) 123-4567"},
{"{Address}", "123 Main Street, Chicago, IL 60601"},
{"{DOB}", "March 15, 1992"},
{"{Position}", "Senior Software Developer"},
{"{StartDate}", "January 15, 2025"},
{"{Salary}", "$95,000"},
{"{Degree}", "Bachelor of Science in Computer Science"},
{"{Institution}", "University of Illinois"},
{"{GradYear}", "2014"},
{"{Signature}", "John Doe"}
}
' Replace all placeholders with actual values
For Each field In applicantData
doc.Texts.ForEach(Sub(text) text.Replace(field.Key, field.Value))
Next
' Save the filled form to a new file
doc.SaveAs("JohnDoe_Application.docx")
Console.WriteLine("Application form filled successfully!")
文字元素的 Replace 方法會將佔位標記與實際值交換。 使用字典可讓您的資料井然有序,並使您在網頁應用程式中能輕鬆地從資料庫、API 或使用者輸入中填入表單。 Texts 屬性提供對文件中所有文字內容的訪問,而 ForEach 遍歷每個文字元素以執行替換。 此模式非常適合從單一範本生成多份個人化文件——特別適用於批次處理情境,例如一次為多位候選人製作錄用通知書。
如何確保已填寫表單的安全性?
填寫表單後,您可以透過對 WORD 文件套用保護功能來提升文件安全性。 這包括設定只讀限制與密碼要求等保護措施,確保僅授權使用者能修改內容。 在處理個人識別號碼、財務資料或醫療紀錄等敏感資料時,安全性考量至關重要。 請考慮實施額外的安全層,例如針對靜態資料與傳輸中資料的加密、表單存取的稽核記錄,以及針對不同使用者類型的基於角色的權限設定。
對於需要可驗證稽核軌跡的文件,建議將完成的 WORD 表單轉換為 PDF,並使用 IronPDF 套用 PDF 數位簽章。 這種組合——使用 WORD 進行撰寫、PDF 進行分發——在金融和醫療保健等受監管行業中是常見的模式。
如何將可填寫的 WORD 表單轉換為 PDF?
將可填寫的 Word 表單轉換為 PDF,是讓您的表單普及且易於分享的必要步驟。 透過 IronWord 等 .NET Word 程式庫,您可以高效地將包含表單欄位的 Word 文件轉換為 PDF 文件。 此流程包含載入您的 WORD 文件、存取其中的表單欄位,並使用該函式庫的轉換方法來產生一個完整保留所有內容的 PDF 檔案。
生成的 PDF 文件完整保留了表單內容,使用者可透過任何標準 PDF 檢視器開啟瀏覽,無需 Microsoft WORD 或專用軟體。 這對需要廣泛散佈表單的組織特別有用,可確保不同平台和裝置之間的相容性。 透過運用 .NET WORD 函式庫的轉換功能,您可以在 WORD 中建立 Professional 表單,並將其轉換為 PDF 格式以供最終發佈,藉此簡化工作流程並提升可存取性。
在選擇 PDF 轉換方案時,請考量下表所列的各項因素:
| 方法 | 辦公室要求 | 伺服器端安全 | 忠實度 |
|---|---|---|---|
| Microsoft Office Interop | 是 | 無 | 高的 |
| IronWord + IronPDF | 無 | 是 | 高的 |
| LibreOffice 無頭模式 | 無 | 是 (Linux) | 中等的 |
| Aspose.Words | 無 | 是 | 高的 |
若 Enterprise 部署需求涵蓋多種文件類型,請參閱 IronWord 的授權方案,並考慮選用能涵蓋您整個文件處理流程的 Suite 授權。
如何分發可填寫的 PDF 檔案?
一旦您建立了可填寫的 PDF,將其分發給使用者是非常直接且高度靈活的。 您可以透過電子郵件分享可填寫的 PDF 檔案、將其嵌入網頁應用程式,或上傳至 Dropbox 或 Google Drive 等雲端儲存服務。這讓使用者能輕鬆下載 PDF 檔案,使用 Adobe Acrobat Reader 等 PDF 檢視器填寫表單,並以電子方式回傳已填妥的文件。
這種數位分發流程不僅能加速資料收集,還能省去實體紙本工作,非常適合遠端團隊和線上工作流程。 無論您是收集求職申請、客戶回饋,還是註冊資訊,分發可填寫的 PDF 都能為您的組織和受訪者提供流暢、高效且無紙化的體驗。
建議導入自動化工作流程,在表單開放填寫時通知收件者、追蹤完成狀態,並針對待提交的表單發送提醒。 與電子郵件行銷平台的整合,可簡化大量發送流程,同時透過合併欄位維持個人化效果。 關於設計有效數位表單的背景知識,W3C 的《Web Forms 指南》與微軟的 DOCX Open XML 規格,均提供了有關標準遵循性的實用參考。
如何實作進階表單功能?
若要進一步提升可填寫表單的功能,建議考慮加入邏輯判斷與資料驗證等進階功能。 Logic 讓您能夠建立互動式表單,這些表單能動態回應使用者的輸入。 例如,您可以根據先前回答的內容顯示或隱藏區段,或僅在滿足特定條件時啟用某些欄位。 驗證可確保使用者輸入的資料符合您的要求,例如強制執行正確的日期格式、必填欄位或有效的電子郵件地址。
許多 .NET Word 函式庫支援透過程式碼建立這些進階功能,讓您可以建立複雜的表單,引導使用者並減少錯誤。 透過在 Word 文件範本中整合邏輯和驗證,您可以建立互動式表單,不僅能收集資料,還能改善所接收資訊的品質和一致性。 進階實作可能包含:
- 自動計算總和或套用公式的計算欄位
- 透過條件格式化標示必填欄位或錯誤
- 支援多國語言,並具備動態欄位標籤與操作說明
- 與外部資料來源整合以進行即時驗證
- 使用正規表達式或業務邏輯的自訂驗證規則
- 顯示表單填寫完成百分比的進度指示器
針對複雜的表單情境,建議實作表單Builder介面,讓非技術背景的使用者無需編寫程式碼即可建立與修改範本。 此方法可讓大型組織靈活管理表單,使業務團隊能維護自己的範本,同時讓開發人員專注於資料管道。IronWord 的範例頁面展示了更多關於文字樣式、表格邊框及文件屬性的操作技巧,這些技巧在建構進階範本時非常實用。
在建構驗證邏輯時,請遵循 .NET 中既定的輸入驗證模式,以確保程式碼具備可維護性與可測試性。 微軟關於 OOXML 文件結構的文件,也是理解 IronWord 所產生底層格式的寶貴參考資料。
下一步計劃是什麼?
使用 IronWord 在 C# 中建立可填寫的表單範本,可簡化您的 .NET 應用程式的文件生成工作流程。 以表格為基礎的排版方式可製作出專業、結構化的表單,並將表格單元適當地對齊,而模板替換模式則可從任何來源進行有效的資料填充。 隨著您的文件自動化需求日益增長,請參閱 IronWord 的操作指南,內容涵蓋郵件合併、頁首與頁尾自訂,以及多語言文件生成等主題。
立即開始免費試用,探索 IronWord 的完整功能,或購買授權以進行生產環境部署。 如有實作相關疑問,請透過 IronWord 支援頁面聯繫工程團隊。 請參閱 IronWord API 參考文件,其中包含詳細的類別文件以及展示複雜表單情境、多文件處理及 Enterprise 級實作的高階範例。
常見問題解答
什麼是 IronWord?
IronWord for .NET 是一個 .NET Word 函式庫,可讓開發人員在不需要 Microsoft Office 相依性的情況下產生和編輯 DOCX 檔案。
如何使用IronWord在C#中創建可填表單?
您可以通過程式化建構具有基於表格佈局和占位符文本字段的表單範本,然後在運行時用實際數據替換占位符來使用IronWord在C#中創建可填表單。
為何製作可填寫表單範本有好處?
建立可填寫的表單範本是有益的,因為它可以簡化資料收集流程、確保文件的一致性,並在各種應用程式和產業中節省時間。
哪些行業可以從使用可填寫表格範本中獲益?
人力資源、醫療保健等產業,以及任何需要結構化資料收集的領域,都可以從使用可填寫表單範本來有效率地處理申請和收集重要資訊中獲益。
使用IronWord需要安裝Microsoft Office嗎?
不,使用 IronWord 不需要安裝 Microsoft Office。它允許生成和編輯 DOCX 檔案,而無需任何 Microsoft Office 依賴。
IronWord 可以處理大型文件處理嗎?
是的,IronWord 旨在有效處理大規模的文件處理,因此適用於企業級應用程式。
IronWord 使用何種程式語言?
IronWord可與C#一起使用,這使它成為在.NET架構內工作的開發人員的理想選擇。
是否有支援可以將IronWord整合到專案中?
是的,Iron Software提供支援和文檔幫助您將IronWord整合到您的專案中。
IronWord 可以同時用於產生和編輯 Word 文件嗎?
是的,IronWord 既可以用於生成新的 Word 文檔,也可以用於編輯現有的 Word 文檔。

