Interface ITextBody
Represents the text body
Inherited Members
Namespace: Syncfusion.DocIO.DLS
Assembly: Syncfusion.DocIO.NET.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
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
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
Methods
AddBlockContentControl(ContentControlType)
Declaration
IBlockContentControl AddBlockContentControl(ContentControlType controlType)
Parameters
Type | Name | Description |
---|---|---|
ContentControlType | controlType |
Returns
Type |
---|
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.
//Creates an empty Word document instance
WordDocument document = new WordDocument();
//This method adds a section and a paragraph in the document
document.EnsureMinimal();
//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);
//Creates an instance of memory stream
MemoryStream stream = new MemoryStream();
//Saves the document to stream
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
'Creates an empty Word document instance
Dim document As WordDocument = New WordDocument()
'This method adds a section and a paragraph in the document
document.EnsureMinimal()
'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)
'Creates an instance of memory stream
Dim stream As MemoryStream = New MemoryStream()
document.Save(stream, FormatType.Docx)
'Closes the document
document.Close()
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.
//Creates an empty Word document instance
WordDocument document = new WordDocument();
//This method adds a section and a paragraph in the document
document.EnsureMinimal();
//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, 1);
//Creates an instance of memory stream
MemoryStream stream = new MemoryStream();
//Saves the document to stream
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
'Creates an empty Word document instance
Dim document As WordDocument = New WordDocument()
'This method adds a section and a paragraph in the document
document.EnsureMinimal()
'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, 1)
'Creates an instance of memory stream
Dim stream As MemoryStream = New MemoryStream()
document.Save(stream, FormatType.Docx)
'Closes the document
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 |
Examples
The following example illustrates how to insert a html string at the paragraph specified by paragraphIndex, and at paragraph item index specified by paragraphItemIndex.
//Creates an empty Word document instance
WordDocument document = new WordDocument();
//This method adds a section and a paragraph in the document
document.EnsureMinimal();
//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, 1, 0);
//Creates an instance of memory stream
MemoryStream stream = new MemoryStream();
//Saves the document to stream
document.Save(stream, FormatType.Docx);
//Closes the document
document.Close();
'Creates an empty Word document instance
Dim document As WordDocument = New WordDocument()
'This method adds a section and a paragraph in the document
document.EnsureMinimal()
'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, 1, 0)
'Creates an instance of memory stream
Dim stream As MemoryStream = New MemoryStream()
document.Save(stream, FormatType.Docx)
'Closes the document
document.Close()
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()