TextConverter Class
Provides the functionalities to convert the Word document as text file.
Inheritance
Inherited Members
Namespace: Syncfusion.DocIO.DLS
Assembly: Syncfusion.DocIO.Base.dll
Syntax
public class TextConverter
Constructors
TextConverter()
Initializes a new instance of the TextConverter class.
Declaration
public TextConverter()
Methods
GetText(WordDocument)
Returns the text contents of the specified WordDocument as string.
Declaration
public string GetText(WordDocument document)
Parameters
Type | Name | Description |
---|---|---|
WordDocument | document | The WordDocument instance. |
Returns
Type | Description |
---|---|
System.String | The string that represents the text contents of the Word document. |
Examples
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//Open a new Word document
WordDocument document = new WordDocument("Template.docx");
//Initialize a new instance of TextConverter class
TextConverter converter = new TextConverter();
//Get the text contents from the template document
string text = converter.GetText(document);
//Create new Word document
WordDocument textDocument = new WordDocument();
textDocument.EnsureMinimal();
//Add the text to the newly created document
textDocument.LastParagraph.AppendText(text);
//Save and close the document.
textDocument.Save("Sample.txt", FormatType.Txt);
document.Close();
textDocument.Close();
}
Private Sub button_Click(sender As Object, e As EventArgs)
'Open a new Word document
Dim document As New WordDocument("Template.docx")
'Initialize a new instance of TextConverter class
Dim converter As New TextConverter()
'Get the text contents from the template document
Dim text As String = converter.GetText(document)
'Create new Word document
Dim textDocument As New WordDocument()
textDocument.EnsureMinimal()
'Add the text to the newly created document
textDocument.LastParagraph.AppendText(text)
'Save and close the document.
textDocument.Save("Sample.txt", FormatType.Txt)
document.Close()
textDocument.Close()
End Sub
Read(StreamReader, IWordDocument)
Reads the text file and convert it to the specified IWordDocument.
Declaration
public void Read(StreamReader reader, IWordDocument document)
Parameters
Type | Name | Description |
---|---|---|
System.IO.StreamReader | reader | The System.IO.StreamReader instance to read the text file. |
IWordDocument | document | The WordDocument instance to represent the text file. |
Examples
The following code example demonstrates how to write the text file content to Word document.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//Load an existing Word document into DocIO instance
WordDocument document = new WordDocument("Template.docx");
FileStream fs = new FileStream("Result.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
reader.BaseStream.Position = 0;
//Create the new TextConverter
TextConverter txtConverter = new TextConverter();
//Write the text file to document
txtConverter.Read(reader, document);
reader.Dispose();
fs.Dispose();
//Save and close the Word document instance
document.Save("Sample.docx", FormatType.Docx);
document.Close();
}
Private Sub button_Click(sender As Object, e As EventArgs)
'Load an existing Word document into DocIO instance
Dim document As New WordDocument("Template.docx")
Dim fs As New FileStream("Result.txt", FileMode.Open, FileAccess.Read)
Dim reader As New StreamReader(fs)
reader.BaseStream.Position = 0
'Create the new TextConverter
Dim txtConverter As New TextConverter()
'Write the text file to document
txtConverter.Read(reader, document)
reader.Dispose()
fs.Dispose()
'Save and close the Word document instance
document.Save("Sample.docx", FormatType.Docx)
document.Close()
End Sub
Write(StreamWriter, IWordDocument)
Writes the specified IWordDocument text contents as text file(.txt format).
Declaration
public void Write(StreamWriter writer, IWordDocument document)
Parameters
Type | Name | Description |
---|---|---|
System.IO.StreamWriter | writer | The System.IO.StreamWriter instance to write the text file. |
IWordDocument | document | The WordDocument to be converted as text file. |
WriteBody(ITextBody)
Writes the specified document ITextBody content to the text file.
Declaration
protected void WriteBody(ITextBody body)
Parameters
Type | Name | Description |
---|---|---|
ITextBody | body | The ITextBody instance content to write into text file. |
WriteHFBody(WordDocument)
Writes the specified WordDocument header and footer body contents.
Declaration
protected void WriteHFBody(WordDocument document)
Parameters
Type | Name | Description |
---|---|---|
WordDocument | document | The WordDocument instance. |
WriteList(IWParagraph)
Writes the list to the text file.
Declaration
protected void WriteList(IWParagraph paragraph)
Parameters
Type | Name | Description |
---|---|---|
IWParagraph | paragraph | The IWParagraph instance to get the list format. |
WriteNewLine()
Writes new line in the text file.
Declaration
protected void WriteNewLine()
WriteParagraph(ParagraphItemCollection, Boolean)
Writes the specified IWParagraph content to the text file.
Declaration
protected void WriteParagraph(ParagraphItemCollection paragraphItems, bool lastPara)
Parameters
Type | Name | Description |
---|---|---|
ParagraphItemCollection | paragraphItems | |
System.Boolean | lastPara | True if it is the last paragraph; otherwise, false. |
WriteSectionEnd(IWSection, Boolean)
Writes the end of the section.
Declaration
protected void WriteSectionEnd(IWSection section, bool lastSection)
Parameters
Type | Name | Description |
---|---|---|
IWSection | section | The IWSection instance. |
System.Boolean | lastSection | True if it is the last section; otherwise, false. |
WriteTable(IWTable)
Writes the specified IWTable text content to the text file.
Declaration
protected void WriteTable(IWTable table)
Parameters
Type | Name | Description |
---|---|---|
IWTable | table | The IWTable instance to write into the text file. |
WriteText(String)
Writes the text contents to the text file.
Declaration
protected void WriteText(string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The string that specifies the text to write into the text file. |
Examples
The following code example demonstrates how to write the document content to text file.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//Load an existing Word document into DocIO instance
WordDocument document = new WordDocument("Template.docx");
FileStream fs = new FileStream("Result.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter writer = new StreamWriter(fs);
//Create the new TextConverter
TextConverter txtConverter = new TextConverter();
//Write the document to text file
txtConverter.Write(writer, document);
writer.Flush();
writer.Dispose();
fs.Dispose();
document.Close();
}
Private Sub button_Click(sender As Object, e As EventArgs)
'Load an existing Word document into DocIO instance
Dim document As New WordDocument("Template.docx")
Dim fs As New FileStream("Result.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim writer As New StreamWriter(fs)
'Create the new TextConverter
Dim txtConverter As New TextConverter()
'Write the document to text file
txtConverter.Write(writer, document)
writer.Flush()
writer.Dispose()
fs.Dispose()
document.Close()
End Sub