การทำงานกับเขตข้อมูลแบบฟอร์ม
เอกสารที่มีการกรอกข้อมูลช่องว่าง (เขตข้อมูล) เรียกว่าแบบฟอร์ม ตัวอย่างเช่น คุณสามารถสร้างแบบฟอร์มการลงทะเบียนใน Microsoft Word ที่ใช้รายการแบบเลื่อนลงซึ่งผู้ใช้สามารถเลือกรายการได้ ฟิลด์ Form
คือตำแหน่งที่จัดเก็บข้อมูลประเภทใดประเภทหนึ่ง เช่น ชื่อหรือที่อยู่ ช่องแบบฟอร์มใน Microsoft Word ประกอบด้วยการป้อนข้อความ กล่องคำสั่งผสม และช่องทำเครื่องหมาย
คุณสามารถใช้ช่องแบบฟอร์มในโครงการของคุณเพื่อ “สื่อสาร” กับผู้ใช้ของคุณได้ ตัวอย่างเช่น คุณสร้างเอกสารที่มีการป้องกันเนื้อหา แต่สามารถแก้ไขได้เฉพาะช่องแบบฟอร์มเท่านั้น ผู้ใช้สามารถป้อนข้อมูลในช่องแบบฟอร์มและส่งเอกสารได้ แอปพลิเคชันของคุณที่ใช้ Aspose.Words สามารถดึงข้อมูลจากช่องแบบฟอร์มและประมวลผลได้
การวางฟิลด์แบบฟอร์มลงในเอกสารด้วยโค้ดเป็นเรื่องง่าย DocumentBuilder มีวิธีพิเศษในการแทรก โดย 1 วิธีสำหรับช่องแบบฟอร์มแต่ละประเภท แต่ละวิธียอมรับพารามิเตอร์สตริงที่แสดงชื่อของฟิลด์แบบฟอร์ม ชื่อสามารถเป็นสตริงว่างได้ อย่างไรก็ตาม หากคุณระบุชื่อสำหรับฟิลด์แบบฟอร์ม บุ๊กมาร์กจะถูกสร้างขึ้นโดยอัตโนมัติด้วยชื่อเดียวกัน
แทรกฟิลด์แบบฟอร์ม
ฟิลด์ฟอร์มเป็นกรณีเฉพาะของฟิลด์ Word ที่อนุญาตให้ “โต้ตอบ” กับผู้ใช้ ช่องแบบฟอร์มใน Microsoft Word ประกอบด้วยกล่องข้อความ กล่องคำสั่งผสม และช่องทำเครื่องหมาย
DocumentBuilder มีวิธีพิเศษในการแทรกช่องแบบฟอร์มแต่ละประเภทลงในเอกสาร: InsertTextInput, InsertCheckBox และ InsertComboBox โปรดทราบว่าหากคุณระบุชื่อสำหรับฟิลด์แบบฟอร์ม บุ๊กมาร์กจะถูกสร้างขึ้นโดยอัตโนมัติด้วยชื่อเดียวกัน
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกเขตข้อมูลแบบฟอร์ม Combobox ลงในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
string[] items = { "One", "Two", "Three" }; | |
builder.InsertComboBox("DropDown", items, 0); |
แทรกการป้อนข้อความ
ใช้วิธี InsertTextInput เพื่อแทรกกล่องข้อความลงในเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกฟิลด์แบบฟอร์มป้อนข้อความลงในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "Hello", 0); | |
dataDir = dataDir + "DocumentBuilderInsertTextInputFormField_out.doc"; | |
doc.Save(dataDir); |
ใส่กล่องกาเครื่องหมาย
โทร InsertCheckBox เพื่อแทรกช่องทำเครื่องหมายลงในเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกฟิลด์แบบฟอร์มช่องทำเครื่องหมายลงในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.InsertCheckBox("CheckBox", true, true, 0); | |
dataDir = dataDir + "DocumentBuilderInsertCheckBoxFormField_out.doc"; | |
doc.Save(dataDir); |
ใส่กล่องคำสั่งผสม
โทร InsertComboBox เพื่อแทรกคอมโบบ็อกซ์ลงในเอกสาร
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกเขตข้อมูลแบบฟอร์ม Combobox ลงในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
string[] items = { "One", "Two", "Three" }; | |
builder.InsertComboBox("DropDown", items, 0); | |
dataDir = dataDir + "DocumentBuilderInsertComboBoxFormField_out.doc"; | |
doc.Save(dataDir); |
รับฟิลด์แบบฟอร์ม
คอลเลกชันของฟิลด์แบบฟอร์มแสดงโดยคลาส FormFieldCollection ที่สามารถดึงข้อมูลได้โดยใช้คุณสมบัติ FormFields ซึ่งหมายความว่า คุณสามารถรับฟิลด์แบบฟอร์มที่อยู่ในโหนดเอกสารใดๆ รวมถึงตัวเอกสารด้วย
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการรับคอลเลกชันของฟิลด์แบบฟอร์ม:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormFieldCollection formFields = doc.Range.FormFields; |
คุณสามารถรับฟิลด์ฟอร์มเฉพาะตามดัชนีหรือชื่อ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการเข้าถึงฟิลด์แบบฟอร์ม:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormFieldCollection documentFormFields = doc.Range.FormFields; | |
FormField formField1 = documentFormFields[3]; | |
FormField formField2 = documentFormFields["Text2"]; |
คุณสมบัติ FormField ช่วยให้คุณสามารถทำงานกับชื่อฟิลด์ของฟอร์ม ประเภท และผลลัพธ์ได้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับชื่อเขตข้อมูลแบบฟอร์ม ประเภท และผลลัพธ์:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "FormFields.doc"); | |
FormField formField = doc.Range.FormFields[3]; | |
if (formField.Type.Equals(FieldType.FieldFormTextInput)) | |
formField.Result = "My name is " + formField.Name; |
จัดรูปแบบฟิลด์แบบฟอร์ม
คุณสมบัติ Font ของ FormField อนุญาตให้ใช้การจัดรูปแบบแบบอักษรกับ FormField โดยรวมรวมถึงค่าฟิลด์ด้วย
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการใช้การจัดรูปแบบแบบอักษรกับ FormField:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_WorkingWithFields(); | |
Document doc = new Document(dataDir + "Document.doc"); | |
doc.Range.FormFields[0].Font.Size = 20; | |
doc.Range.FormFields[0].Font.Color = Color.Red; | |
doc.Save(dataDir + "Document_out.doc"); |