Class WTextBody
Represent a text body of the Word document.
Inheritance
Inherited Members
Namespace: Syncfusion.DocIO.DLS
Assembly: Syncfusion.DocIO.Base.dll
Syntax
public class WTextBody : WidgetContainer, IXDLSSerializable, ITextBody, ICompositeEntity, IEntity, IWidgetContainer, IWidget
Fields
m_bodyItems
The section paragraphs
Declaration
protected BodyItemCollection m_bodyItems
Field Value
Type | Description |
---|---|
BodyItemCollection |
Properties
ChildEntities
Gets the child elements of the WTextBody.
Declaration
public EntityCollection ChildEntities { get; }
Property Value
Type | Description |
---|---|
EntityCollection | The collection of the child elements. |
EntityType
Gets the type of the entity. Read-only.
Declaration
public override EntityType EntityType { get; }
Property Value
Type | Description |
---|---|
EntityType | The EntityType of the current item. |
Overrides
FormFields
Gets the collection of form fields in the WTextBody. Read-only.
Declaration
public FormFieldCollection FormFields { get; }
Property Value
Type | Description |
---|---|
FormFieldCollection | The collection that represents the form fields in the text body. |
See Also
LastParagraph
Gets the last paragraph in the section. Read-only.
Declaration
public 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
public IWParagraphCollection Paragraphs { get; }
Property Value
Type | Description |
---|---|
IWParagraphCollection | The collection that represents the paragraphs in the text body. |
See Also
Tables
Gets the collection of tables in the WTextBody. Read-only.
Declaration
public IWTableCollection Tables { get; }
Property Value
Type | Description |
---|---|
IWTableCollection | The collection that represents the tables in the text body. |
See Also
WidgetCollection
Gets the entity collection. Read-only.
Declaration
protected override IEntityCollectionBase WidgetCollection { get; }
Property Value
Type | Description |
---|---|
IEntityCollectionBase | The collection of entities. |
Overrides
Methods
AddBlockContentControl(ContentControlType)
Adds a new block content control at the end of section.
Declaration
public IBlockContentControl AddBlockContentControl(ContentControlType controlType)
Parameters
Type | Name | Description |
---|---|---|
ContentControlType | controlType |
Returns
Type | Description |
---|---|
IBlockContentControl | The reference to the newly added IBlockContentControl object. |
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()
Exceptions
Type | Condition |
---|---|
System.NotImplementedException | Thrown when |
AddParagraph()
Adds a new paragraph at the end of section.
Declaration
public 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
public 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
CloneImpl()
Creates a duplicate copy of the entity.
Declaration
protected override object CloneImpl()
Returns
Type | Description |
---|---|
System.Object | An System.Object that is newly created. |
Overrides
CreateLayoutInfo()
Creates layout information.
Declaration
protected override void CreateLayoutInfo()
Overrides
EnsureMinimum()
If the text body has no paragraphs, creates and add one paragraph to the text body.
Declaration
public 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
InitXDLSHolder()
Registers child objects in XDSL holder.
Declaration
protected override void InitXDLSHolder()
Overrides
InsertXHTML(String)
Declaration
public 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.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//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
public 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.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//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();
}
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, 2)
'Save and close the document
document.Save("Sample.docx")
document.Close()
End Sub
InsertXHTML(String, Int32, Int32)
Declaration
public 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
public 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
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//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();
}
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.
"
'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()
End Sub
IsValidXHTML(String, XHTMLValidationType, out String)
Validates the specified html string with the specified XHTML validation type and exception message.
Declaration
public 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
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//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();
}
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.
"
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()
End Sub