是的,这是可能的,试试这个:
DocumentModel document = DocumentModel.Load("FormFilled.docx");
// Get a snapshot of all form fields in the document.
FormFieldDataCollection formData = document.Content.FormFieldsData;
// Edit "FullName" text box field.
FormTextData fullNameData = (FormTextData)formData["FullName"];
fullNameData.Field.ResultInlines.Content.LoadText("Jane Doe");
// Edit "BirthDate" text box field.
FormTextData birthDateData = (FormTextData)formData["BirthDate"];
birthDateData.Field.ResultInlines.Content.LoadText(
new DateTime(2000, 1, 1).ToString(birthDateData.ValueFormat));
// Edit "Salary" text box field.
FormTextData salaryData = (FormTextData)formData["Salary"];
salaryData.Field.ResultInlines.Content.LoadText(
5432.1.ToString(salaryData.ValueFormat));
// Uncheck "Married" check box field.
FormCheckBoxData marriedData = (FormCheckBoxData)formData["Married"];
marriedData.Value = false;
// Select "Female" from drop down field, note that "genderData.Items
// contains a list of drop down field's items that you can select.
FormDropDownData genderData = (FormDropDownData)formData["Gender"];
genderData.SelectedItemIndex = 2;
document.Save("FormFilledOutput.docx");
我希望这有帮助。
更新:
在较新的 GemBox.Document 的错误修复版本之一中,FormTextData.Value属性同时具有 getter 和 setter,因此可以通过以下方式简化上述内容:
// Edit "FullName" text box field.
FormTextData fullNameData = (FormTextData)formData["FullName"];
fullNameData.Value = "Jane Doe";
// Edit "BirthDate" text box field.
FormTextData birthDateData = (FormTextData)formData["BirthDate"];
birthDateData.Value = new DateTime(2000, 1, 1);
// Edit "Salary" text box field.
FormTextData salaryData = (FormTextData)formData["Salary"];
salaryData.Value = 5432.1;
你也可以参考这个例子。