Back to Devexpress

ContentControlPlainText Interface

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-15dedf0f.md

latest7.5 KB
Original Source

ContentControlPlainText Interface

Plain text content control.

Namespace : DevExpress.XtraRichEdit.API.Native

Assembly : DevExpress.RichEdit.v25.2.Core.dll

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
public interface ContentControlPlainText :
    ContentControlBase
vb
Public Interface ContentControlPlainText
    Inherits ContentControlBase

The following members return ContentControlPlainText objects:

Remarks

Create a Plain Text Content Control

The code sample below creates a plain text content control and inserts text to is:

csharp
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit;

using (var wordProcessor = new RichEditDocumentServer()) {

    wordProcessor.LoadDocument("Content Controls.docx");
    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;

    // Insert a form to enter a name:
    var namePosition = document.CreatePosition(document.Paragraphs[0].Range.End.ToInt() - 1);    
    var nameControl = contentControls.InsertPlainTextControl(namePosition);

    // Insert a text to a content control:
    var nameTextPosition = document.CreatePosition(nameControl.Range.Start.ToInt() + 1);
    document.InsertText(nameTextPosition, "Click to enter a name");
}
vb
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit

Using wordProcessor = New RichEditDocumentServer()

  wordProcessor.LoadDocument("Content Controls.docx")
  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls

  ' Insert a form to enter a name:
  Dim namePosition = document.CreatePosition(document.Paragraphs(0).Range.End.ToInt() - 1)
  Dim nameControl = contentControls.InsertPlainTextControl(namePosition)

  ' Insert a text to a content control:
  Dim nameTextPosition = document.CreatePosition(nameControl.Range.Start.ToInt() + 1)
  document.InsertText(nameTextPosition, "Click to enter a name")
End Using

Access Plain Text Content Controls

The SubDocument.ContentControls property returns all content controls in a document. Use the ContentControlBase.ControlType property to determine the content control type.

The code sample below retrieves all plain text controls in a document:

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {

    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;

    var plainTextControls = document.ContentControls.Where(contentControl => contentControl.ControlType == ContentControlType.PlainText).Cast<ContentControlPlainText>();

    foreach (ContentControlPlainText plainTextControl in plainTextControls)
    {
        // your code here
    }
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()

  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls

  Dim plainTextControls = document.ContentControls.Where(Function(contentControl) contentControl.ControlType = ContentControlType.PlainText).Cast(Of ContentControlPlainText)()

  For Each plainTextControl In plainTextControls
    ' your code here
  Next plainTextControl

Modify Plain Text Content Controls

Use the ContentControlPlainText class properties to change the plain text control parameters. The code sample below retrieves the plain text content control from the first paragraph and changes its border color:

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {

    wordProcessor.LoadDocument("Content Controls.docx");
    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;
    var firstParagraph = document.Paragraphs[0];
    for (var i = 0; i < contentControls.Count; i++)
    {
        if (firstParagraph.Range.Contains(contentControls[i].Range.Start) && contentControls[i].ControlType == ContentControlType.PlainText)
        {
            ContentControlPlainText plainText = (ContentControlPlainText)contentControls[i];
            plainText.Color = Color.Red;
            break;
        }
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()

  wordProcessor.LoadDocument("Content Controls.docx")
  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls
  Dim firstParagraph = document.Paragraphs(0)
  For i = 0 To contentControls.Count - 1
    If firstParagraph.Range.Contains(contentControls(i).Range.Start) AndAlso contentControls(i).ControlType = ContentControlType.PlainText Then
      Dim plainText As ContentControlPlainText = CType(contentControls(i), ContentControlPlainText)
      plainText.Color = Color.Red
      Exit For
    End If
  Next i
End Using

Remove Plain Text Content Controls

The ContentControlCollection.Remove method allows you to remove specific content control. You can also specify whether to keep control’s contents when the controls is removed.

The code sample below removes all plain text controls from the document:

csharp
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;

using (var wordProcessor = new RichEditDocumentServer()) {
    Document document = wordProcessor.Document;
    var contentControls = document.ContentControls;

    for (var i = 0; i < contentControls.Count; i++)
    {
        if (contentControls[i].ControlType == ContentControlType.PlainText)
        {
            contentControls.Remove(contentControls[i], true);
        }
    }
}
vb
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native

Using wordProcessor = New RichEditDocumentServer()
  Dim document As Document = wordProcessor.Document
  Dim contentControls = document.ContentControls

  Dim i = 0
  Do While i < contentControls.Count
    If contentControls(i).ControlType = ContentControlType.PlainText Then
      contentControls.Remove(contentControls(i), True)
    End If
    i += 1
  Loop
End Using

See Also

ContentControlPlainText Members

DevExpress.XtraRichEdit.API.Native Namespace