ASP.NET Core

  • Code Examples
  • Upgrade Guide
  • User Guide
  • Demos
  • Support
  • Forums
  • Download
Interface ITextBody

    Show / Hide Table of Contents

    Interface ITextBody

    Represents the text body

    Inherited Members
    IEntity.Clone()
    IEntity.Document
    IEntity.Owner
    IEntity.EntityType
    IEntity.NextSibling
    IEntity.PreviousSibling
    IEntity.IsComposite
    Namespace: Syncfusion.DocIO.DLS
    Assembly: Syncfusion.DocIO.Base.dll
    Syntax
    public interface ITextBody : ICompositeEntity, IEntity

    Properties

    ChildEntities

    Gets the child elements of the WTextBody.

    Declaration
    EntityCollection ChildEntities { get; }
    Property Value
    Type Description
    EntityCollection

    The collection of the child elements.

    FormFields

    Gets the collection of form fields in the WTextBody. Read-only.

    Declaration
    FormFieldCollection FormFields { get; }
    Property Value
    Type Description
    FormFieldCollection

    The collection that represents the form fields in the text body.

    See Also
    FormFieldCollection
    WFormField

    LastParagraph

    Gets the last paragraph in the section. Read-only.

    Declaration
    IWParagraph LastParagraph { get; }
    Property Value
    Type Description
    IWParagraph

    The IWParagraph object.

    Examples
    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        //Load a template Word document
        WordDocument document = new WordDocument("Template.docx");
        //Get the textbody of first section
        WTextBody textbody = document.Sections[0].Body;
        //Get the last paragraph
        IWParagraph paragraph = textbody.LastParagraph;
        //Set horizontal alignment
        paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right;
        //Save and close the document	
        document.Save("Sample.docx");
        document.Close();
    }
    Private Sub button_Click(sender As Object, e As EventArgs)
        'Load a template Word document
        Dim document As New WordDocument("Template.docx")
        'Get the textbody of first section
        Dim textbody As WTextBody = document.Sections(0).Body
        'Get the last paragraph
        Dim paragraph As IWParagraph = textbody.LastParagraph
        'Set horizontal alignment
        paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Right
        'Save and close the document	
        document.Save("Sample.docx")
        document.Close()
    End Sub

    Paragraphs

    Gets the collection of paragraphs in the WTextBody. Read-only.

    Declaration
    IWParagraphCollection Paragraphs { get; }
    Property Value
    Type Description
    IWParagraphCollection

    The collection that represents the paragraphs in the text body.

    See Also
    IWParagraphCollection
    IWParagraph

    Tables

    Gets the collection of tables in the WTextBody. Read-only.

    Declaration
    IWTableCollection Tables { get; }
    Property Value
    Type Description
    IWTableCollection

    The collection that represents the tables in the text body.

    See Also
    IWTableCollection
    IWTable

    Methods

    AddBlockContentControl(ContentControlType)

    Declaration
    IBlockContentControl AddBlockContentControl(ContentControlType controlType)
    Parameters
    Type Name Description
    ContentControlType controlType
    Returns
    Type Description
    IBlockContentControl
    Examples

    The following example illustrates how to add a block content control to the text body.

    //Create new Word document
    WordDocument document = new WordDocument();
    //Add new section
    IWSection section = document.AddSection();
    WTextBody textbody = section.Body;
    //Add a block content control to the text body
    BlockContentControl blockControl = textbody.AddBlockContentControl(ContentControlType.RichText) as BlockContentControl;
    //Add text to the control added
    blockControl.TextBody.AddParagraph().AppendText("A new block control is added");
    //Save and close the document	
    document.Save("Sample.docx");
    document.Close();
    'Create new Word document
    Dim document As New WordDocument()
    'Add new section
    Dim section As IWSection = document.AddSection()
    Dim textbody As WTextBody = section.Body
    'Add a block content control to the text body
     Dim blockControl As BlockContentControl = TryCast(textbody.AddBlockContentControl(ContentControlType.RichText), BlockContentControl)
     'Add text to the control added
     blockControl.TextBody.AddParagraph().AppendText("A new block control is added")
     'Save and close the document	
     document.Save("Sample.docx")
     document.Close()

    AddParagraph()

    Adds a new paragraph at the end of section.

    Declaration
    IWParagraph AddParagraph()
    Returns
    Type Description
    IWParagraph

    The reference to the newly added IWParagraph object.

    Examples

    The following example illustrates how to add a paragraph to the text body.

    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        //Create new Word document
        WordDocument document = new WordDocument();
        //Add new section
        IWSection section = document.AddSection();
        WTextBody textbody = section.Body;
        //Add a paragraph to the text body
        WParagraph paragraph = textbody.AddParagraph() as WParagraph;
        //Add text
        paragraph.AppendText("A new paragraph is added");
        //Save and close the document	
        document.Save("Sample.docx");
        document.Close();
    }
    Private Sub button_Click(sender As Object, e As EventArgs)
        'Create new Word document
        Dim document As New WordDocument()
        'Add new section
        Dim section As IWSection = document.AddSection()
        Dim textbody As WTextBody = section.Body
        'Add a paragraph to the text body
        Dim paragraph As WParagraph = TryCast(textbody.AddParagraph(), WParagraph)
        'Add text
        paragraph.AppendText("A new paragraph is added")
        'Save and close the document	
        document.Save("Sample.docx")
        document.Close()
    End Sub

    AddTable()

    Adds a new table at the end of the section.

    Declaration
    IWTable AddTable()
    Returns
    Type Description
    IWTable

    The reference to the newly added IWTable object.

    Examples

    The following example illustrates how to add a table to the text body.

    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        //Create new Word document
        WordDocument document = new WordDocument();
        //Add new section
        IWSection section = document.AddSection();
        WTextBody textbody = section.Body;
        //Add a table to the text body
        IWTable table = textbody.AddTable();
        table.ResetCells(2, 2);
        //Save and close the document	
        document.Save("Sample.docx");
        document.Close();
    }
    Private Sub button_Click(sender As Object, e As EventArgs)
        'Create new Word document
        Dim document As New WordDocument()
        'Add new section
        Dim section As IWSection = document.AddSection()
        Dim textbody As WTextBody = section.Body
        'Add a table to the text body
        Dim table As IWTable = textbody.AddTable()
        table.ResetCells(2, 2)
        'Save and close the document	
        document.Save("Sample.docx")
        document.Close()
    End Sub

    EnsureMinimum()

    If the text body has no paragraphs, creates and add one paragraph to the text body.

    Declaration
    void EnsureMinimum()
    Examples
    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        //Create new Word document
        WordDocument document = new WordDocument();
        //Add new section
        IWSection section = document.AddSection();
        WTextBody textbody = section.Body;
        //Add one paragraph to the section body
        textbody.EnsureMinimum();
        textbody.LastParagraph.AppendText("Last paragraph");
        //Save and close the document	
        document.Save("Sample.docx");
        document.Close();
    }
    Private Sub button_Click(sender As Object, e As EventArgs)
        'Create new Word document
        Dim document As New WordDocument()
        'Add new section
        Dim section As IWSection = document.AddSection()
        Dim textbody As WTextBody = section.Body
        'Add one paragraph to the section body
        textbody.EnsureMinimum()
        textbody.LastParagraph.AppendText("Last paragraph")
        'Save and close the document	
        document.Save("Sample.docx")
        document.Close()
    End Sub

    InsertXHTML(String)

    Declaration
    void InsertXHTML(string html)
    Parameters
    Type Name Description
    System.String html
    Examples

    The following example illustrates how to insert a html string to the text body.

        //Load the template document
        WordDocument document = new WordDocument("Template.docx");
        //Html string to be inserted
        string htmlstring = "

    This text is inserted as HTML string.

    "; //Append Html string as first item of the second paragraph in the document document.Sections[0].Body.InsertXHTML(htmlstring); //Save and close the document document.Save("Sample.docx"); document.Close();
    Private Sub button_Click(sender As Object, e As EventArgs)
        'Load the template document
        Dim document As New WordDocument("Template.docx")
        'Html string to be inserted
        Dim htmlstring As String = "

    This text is inserted as HTML string.

    " 'Append Html string as first item of the second paragraph in the document document.Sections(0).Body.InsertXHTML(htmlstring) 'Save and close the document document.Save("Sample.docx") document.Close() End Sub

    InsertXHTML(String, Int32)

    Declaration
    void InsertXHTML(string html, int paragraphIndex)
    Parameters
    Type Name Description
    System.String html
    System.Int32 paragraphIndex
    Examples

    The following example illustrates how to insert a html string at the specified paragraph in the text body.

        //Load the template document
        WordDocument document = new WordDocument("Template.docx");
        //Html string to be inserted
        string htmlstring = "

    This text is inserted as HTML string.

    "; //Append Html string as first item of the second paragraph in the document document.Sections[0].Body.InsertXHTML(htmlstring, 2); //Save and close the document document.Save("Sample.docx"); document.Close();
        'Load the template document
        Dim document As New WordDocument("Template.docx")
        'Html string to be inserted
        Dim htmlstring As String = "

    This text is inserted as HTML string.

    " 'Append Html string as first item of the second paragraph in the document document.Sections(0).Body.InsertXHTML(htmlstring, 2) 'Save and close the document document.Save("Sample.docx") document.Close()

    InsertXHTML(String, Int32, Int32)

    Declaration
    void InsertXHTML(string html, int paragraphIndex, int paragraphItemIndex)
    Parameters
    Type Name Description
    System.String html
    System.Int32 paragraphIndex
    System.Int32 paragraphItemIndex

    IsValidXHTML(String, XHTMLValidationType)

    Validates the specified html string with the specified XHTML validation type.

    Declaration
    bool IsValidXHTML(string html, XHTMLValidationType type)
    Parameters
    Type Name Description
    System.String html

    The html string to validate.

    XHTMLValidationType type

    The XHTMLValidationType member that specifies the validation type.

    Returns
    Type Description
    System.Boolean

    True if it is valid XHTML; otherwise, false.

    Remarks

    This method is not supported in Silverlight, WinRT, Windows Phone, Universal, Universal Windows Platform, MVC6 and Xamarin platforms.

    Examples
        //Load the template document
        WordDocument document = new WordDocument("Template.docx");
        //Html string to be inserted
        string htmlstring = "

    This text is inserted as HTML string.

    "; //Validating the Html string bool isValidHtml = document.LastSection.Body.IsValidXHTML(htmlstring, XHTMLValidationType.Transitional); //If the Html string passes validation, it is inserted to document if (isValidHtml) { //Append Html string as first item of the second paragraph in the document document.Sections[0].Body.InsertXHTML(htmlstring, 2, 0); } //Save and close the document document.Save("Sample.docx"); document.Close();
        'Load the template document
        Dim document As New WordDocument("Template.docx")
        'Html string to be inserted
        Dim htmlstring As String = "

    This text is inserted as HTML string.

    " 'Validating the Html string Dim isValidHtmlAs Boolean = document.LastSection.Body.IsValidXHTML(htmlstring, XHTMLValidationType.Transitional) 'If the Html string passes validation, it is inserted to document If isValidHtmlThen 'Append Html string as first item of the second paragraph in the document document.Sections(0).Body.InsertXHTML(htmlstring, 2, 0) End If 'Save and close the document document.Save("Sample.docx") document.Close()

    IsValidXHTML(String, XHTMLValidationType, out String)

    Validates the specified html string with the specified XHTML validation type and exception message.

    Declaration
    bool IsValidXHTML(string html, XHTMLValidationType type, out string exceptionMessage)
    Parameters
    Type Name Description
    System.String html

    The html string to validate.

    XHTMLValidationType type

    The XHTMLValidationType member that specifies the validation type.

    System.String exceptionMessage

    The string that specifies the exception message.

    Returns
    Type Description
    System.Boolean

    True if it is valid XHTML, otherwise false.

    Remarks

    This method is not supported in Silverlight, WinRT, Windows Phone, Universal, Universal Windows Platform, MVC6 and Xamarin platforms.

    Examples
        //Load the template document
        WordDocument document = new WordDocument("Template.docx");
        //Html string to be inserted
        string htmlstring = "

    This text is inserted as HTML string.

    "; string exception = "The string is not valid"; //Validating the Html string bool isValidHtml = document.LastSection.Body.IsValidXHTML(htmlstring, XHTMLValidationType.Transitional, out exception); //If the Html string passes validation, it is inserted to document if (isValidHtml) { //Append Html string as first item of the second paragraph in the document document.Sections[0].Body.InsertXHTML(htmlstring, 2, 0); } //Save and close the document document.Save("Sample.docx"); document.Close();
        'Load the template document
        Dim document As New WordDocument("Template.docx")
        'Html string to be inserted
        Dim htmlstring As String = "

    This text is inserted as HTML string.

    " Dim exception As String = "The string is not valid" 'Validating the Html string Dim isValidHtmlAs Boolean = document.LastSection.Body.IsValidXHTML(htmlstring, XHTMLValidationType.Transitional, exception) 'If the Html string passes validation, it is inserted to document If isValidHtmlThen 'Append Html string as first item of the second paragraph in the document document.Sections(0).Body.InsertXHTML(htmlstring, 2, 0) End If 'Save and close the document document.Save("Sample.docx") document.Close()
    Back to top Generated by DocFX
    Copyright © 2001 - 2023 Syncfusion Inc. All Rights Reserved