Skip to content

Commit

Permalink
Release GroupDocs.Editor for .NET 24.5
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorStupak committed May 16, 2024
1 parent 947b565 commit 92cbace
Show file tree
Hide file tree
Showing 37 changed files with 591 additions and 3 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: working-with-editabledocument
url: editor/net/working-with-editabledocument
title: Working with EditableDocument
id: editabledocument
url: editor/net/editabledocument
title: EditableDocument
weight: 100
description: "This documentation section explains features of EditableDocument class when editing document with GroupDocs.Editor for .NET API."
keywords: Edit document, Editable document
Expand Down
82 changes: 82 additions & 0 deletions net/developer-guide/form-field-management/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
id: form-field-management
url: editor/net/form-field-management
title: Field Management in GroupDocs.Editor for .NET
weight: 100
description: "This documentation section explains how to the management and manipulation of form fields within documents."
keywords: fields, form fields
productName: GroupDocs.Editor for .NET
hideChildren: False
toc: True
structuredData:
showOrganization: True
---

The [`GroupDocs.Editor.Words.FieldManagement`](https://reference.groupdocs.com/editor/net/groupdocs.editor/formfieldmanager/) namespace provides classes and interfaces for the management and manipulation of form fields within Word documents. This feature allows users to interact with various types of form fields programmatically, including text fields, checkboxes, dropdowns, and more. This article covers the main features and usage of field management in GroupDocs.Editor for .NET.

## Key Classes and Interfaces

### IFormField

The [`IFormField`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/iformfield/) interface represents a general form field and defines common properties shared among all form fields.

#### Properties

- **Stylesheet**: Gets the stylesheet associated with the form field.
- **Readonly**: Gets or sets a value indicating whether the form field is read-only.
- **Name**: Gets the name of the form field.
- **Type**: Gets the type of the form field.
- **LocaleId**: Gets or sets the locale identifier for the form field.
- **StatusText**: Gets or sets the status text associated with the form field.
- **HelpText**: Gets or sets the help text associated with the form field.

### Specific Form Field Types

The namespace includes several classes that implement `IFormField` for specific types of form fields:

- **[`CheckBoxForm`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/checkboxform/)**: Represents a checkbox form field.
- **[`DropDownFormField`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/dropdownformfield/)**: Represents a dropdown form field with a list of string values.
- **[`NumberFormField`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/numberformfield/)**: Represents a number input form field.
- **[`TextFormField`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/textformfield/)**: Represents a text input form field.
- **[`DateFormField`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/dateformfield/)** Represents a date input form field.

### FormFieldCollection

The [`FormFieldCollection`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/formfieldcollection/) class manages a collection of form fields within a document. It allows you to enumerate, insert, and retrieve form fields by their name.

#### Methods

- **Insert(IFormField field)**: Adds a new form field to the collection.
- **GetEnumerator()**: Returns an enumerator that iterates through the collection.
- **this[string name]**: Gets the form field with the specified name.
- **GetFormField<T>(string name)**: Retrieves a form field of type `T` by its name.

### InvalidFormField

The [`InvalidFormField`](https://reference.groupdocs.com/editor/net/groupdocs.editor.words.fieldmanagement/invalidformfield/) class represents an invalid form field that needs renaming. It is used in the context of fixing invalid form field names.

#### Properties

- **Name**: Gets the original name of the form field.
- **FixedName**: Gets or sets the new name for the form field after repair. The default value is `string.Format("{0}_fixed", name)`.


### FieldManager

The [`FormFieldManager`](https://reference.groupdocs.com/editor/net/groupdocs.editor/formfieldmanager/) class provides a high-level interface for managing form fields in a document.

#### Properties

- **FormFieldCollection**: Gets the collection of form fields in the document.

#### Methods

- **UpdateFormFiled(FormFieldCollection formFieldCollection)**: Updates form fields in the document.
- **FixInvalidFormFieldNames(IEnumerable<UpdateInvalidFormFieldNames> updateInvalidFormFieldNames)**: Fixes invalid form field names in the document.
- **HasInvalidFormFields()**: Checks if the document has invalid form fields.
- **RemoveFormFiled(IFormField formField)**: Removes a specific form field.
- **RemoveFormFileds(IEnumerable<IFormField> formFields)**: Removes multiple form fields.

## Conclusion

The `GroupDocs.Editor.Words.FieldManagement` namespace provides a powerful set of tools for managing form fields in Word documents. With these tools, developers can programmatically interact with, modify, and validate form fields, ensuring that documents are correctly formatted and free of duplicate or invalid form fields. For more details, refer to the official [GroupDocs.Editor for .NET documentation](https://docs.groupdocs.com/editor/net/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---
id: edit-and-update-form-fields
url: editor/net/edit-and-update-form-fields
title: Edit and Update Form Fields
weight: 4
description: "Editing Form Fields with GroupDocs.Editor for .NET"
keywords: Edit Form Fields, Update Form Fields, Update Form Fields with Word Document Protection, FormFieldManager
productName: GroupDocs.Editor for .NET
hideChildren: False
toc: True
---

This article demonstrates how to edit form fields in a Word document using GroupDocs.Editor for .NET. The example provided guides you through loading a document, modifying form fields, and saving the updated document.

#### Step-by-Step Guide

1. **Create a Stream from the Path**

Create a `FileStream` from the input file path.

```csharp
using (FileStream fs = File.OpenRead(inputFilePath))
{
// Further code will be placed here
}
```

2. **Create Load Options**

Initialize the `WordProcessingLoadOptions` for loading the document. If your document is password-protected, specify the password. In this example, the document is unprotected, so the password will be ignored.

```csharp
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
loadOptions.Password = "some_password_to_open_a_document";
```

3. **Load the Document into the Editor Instance**

Use the `Editor` class to load the document with the specified load options.

```csharp
using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
// Further code will be placed here
}
```

4. **Retrieve and Update Form Fields**

Obtain the `FormFieldManager` instance from the `Editor` and read the `FormFieldCollection`. Update a specific text form field by modifying its properties and then call the `UpdateFormFiled` method to apply the changes.

```csharp
FormFieldManager fieldManager = editor.FormFieldManager;
FormFieldCollection collection = fieldManager.FormFieldCollection;

TextFormField textField = collection.GetFormField<TextFormField>("Text1");
textField.LocaleId = 1029;
textField.Value = "new Value";
fieldManager.UpdateFormFiled(collection);
```

5. **Create and Set Save Options**

Initialize the `WordProcessingSaveOptions` with the desired format. Optimize memory usage if necessary, and protect the document from writing with a password.

```csharp
WordProcessingFormats docFormat = WordProcessingFormats.Docx;
WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(docFormat);
saveOptions.OptimizeMemoryUsage = true;
saveOptions.Protection = new WordProcessingProtection(WordProcessingProtectionType.AllowOnlyFormFields, "write_password");
```

6. **Save the Document**

Prepare a stream for saving and use the `Save` method of the `Editor` to save the updated document.

```csharp
using (MemoryStream outputStream = new MemoryStream())
{
editor.Save(outputStream, saveOptions);
}
```

7. **Completion Message**

Print a confirmation message to indicate the successful completion of the operation.

```csharp
System.Console.WriteLine("EditFormFieldCollection routine has successfully finished");
```

### Complete Example Code

Below is the complete example code demonstrating the entire process:

```csharp
/// <summary>
/// This example demonstrates how to edit form fields in a Word document using GroupDocs.Editor for .NET.
/// </summary>
internal static class EditFormFieldCollection
{
/// <summary>
/// Runs the example to demonstrate loading, editing, and saving form fields in a document.
/// </summary>
internal static void Run()
{
// 1. Get a path to the input file (or stream with file content).
// In this case, it is a sample DOCX with form fields.
string inputFilePath = Constants.SampleLegacyFormFields_docx;

// 2. Create a stream from this path
using (FileStream fs = File.OpenRead(inputFilePath))
{
// 3. Create load options for this document
WordProcessingLoadOptions loadOptions = new WordProcessingLoadOptions();
// 3.1. If the input document is password-protected, specify the password for its opening...
loadOptions.Password = "some_password_to_open_a_document";
// 3.2. ...but, because the document is unprotected, this password will be ignored
// 4. Load the document with options into the Editor instance
using (Editor editor = new Editor(delegate { return fs; }, delegate { return loadOptions; }))
{
// 4.1. Read the FormFieldManager instance
FormFieldManager fieldManager = editor.FormFieldManager;
// 4.2. Read the FormFieldCollection in the document
FormFieldCollection collection = fieldManager.FormFieldCollection;

// 4.3. Update a specific text form field
TextFormField textField = collection.GetFormField<TextFormField>("Text1");
textField.LocaleId = 1029;
textField.Value = "new Value";
fieldManager.UpdateFormFiled(collection);

// 5. Create document save options
WordProcessingFormats docFormat = WordProcessingFormats.Docx;
WordProcessingSaveOptions saveOptions = new WordProcessingSaveOptions(docFormat);

// 5.1. If the document is large and causes OutOfMemoryException, set the memory optimization option
saveOptions.OptimizeMemoryUsage = true;

// 5.2. Protect the document from writing (allow only form fields) with a password
saveOptions.Protection = new WordProcessingProtection(WordProcessingProtectionType.AllowOnlyFormFields, "write_password");

// 6. Save the document
// 6.1. Prepare a stream for saving
using (MemoryStream outputStream = new MemoryStream())
{
// 6.2. Save the document
editor.Save(outputStream, saveOptions);
}
}
}
System.Console.WriteLine("EditFormFieldCollection routine has successfully finished");
}
}
```
Loading

0 comments on commit 92cbace

Please sign in to comment.