Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Word .NET
>
Tutorial
> Select checkbox
The example shows you how to find checkbox and mark it as "checked".
C# example
using System; using System.Collections.Generic; using Independentsoft.Office; using Independentsoft.Office.Word; using Independentsoft.Office.Word.Fields; namespace Sample { class Program { static void Main(string[] args) { WordDocument doc = new WordDocument("c:\\test\\input.docx"); IList
allElements = doc.GetContentElements(); foreach (IContentElement element in allElements) { if (element is Paragraph) { Paragraph paragraph = (Paragraph)element; for (int i = 0; i < paragraph.Content.Count; i++) { if (paragraph.Content[i] is Run) { Run run = (Run)paragraph.Content[i]; for (int r = 0; r < run.Content.Count; r++) { if (run.Content[r] is ComplexField) { ComplexField complexField = (ComplexField)run.Content[r]; if (complexField.FormFieldProperties != null && complexField.FormFieldProperties.Name == "CheckBox1" && complexField.FormFieldProperties.CheckBoxFormFieldProperties != null) { complexField.FormFieldProperties.CheckBoxFormFieldProperties.Checked = ExtendedBoolean.True; } } } } } } } doc.Save("c:\\test\\output.docx", true); } } }
VB example
Imports System Imports Independentsoft.Office Imports Independentsoft.Office.Word Imports Independentsoft.Office.Word.Fields Module Module1 Sub Main(ByVal args As String()) Dim doc As WordDocument = New WordDocument("c:\test\input.docx") Dim allElements As IList(Of IContentElement) = doc.GetContentElements() For Each element As IContentElement In allElements If TypeOf element Is Paragraph Then Dim paragraph As Paragraph = DirectCast(element, Paragraph) For i As Integer = 0 To paragraph.Content.Count - 1 If TypeOf paragraph.Content(i) Is Run Then Dim run As Run = DirectCast(paragraph.Content(i), Run) For r As Integer = 0 To run.Content.Count - 1 If TypeOf run.Content(r) Is ComplexField Then Dim complexField As ComplexField = DirectCast(run.Content(r), ComplexField) If complexField.FormFieldProperties IsNot Nothing AndAlso complexField.FormFieldProperties.Name = "CheckBox1" AndAlso complexField.FormFieldProperties.CheckBoxFormFieldProperties IsNot Nothing Then complexField.FormFieldProperties.CheckBoxFormFieldProperties.Checked = ExtendedBoolean.[True] End If End If Next End If Next End If Next doc.Save("c:\test\output.docx", True) End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*