At the time of this writing, Aspose.Words for .NET 5.3 and Aspose.Words for Java 2.5 support saving documents as plain text files, but do not directly support loading them. However, it is easy to write your own code to load plain text files into Aspose.Words.
Example LoadTxt
Loads a plain text file into an Aspose.Words.Document object.
[C#]
using System.IO;
using System.Reflection;
using System.Text;
using Aspose.Words;
namespace LoadTxt
{
class Program
{
static void Main(string[] args)
{
// Sample infrastructure.
string exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string docsDir = Path.GetFullPath(Path.Combine(exeDir, @"..\..\..\Common\"));
// This object will help us generate the document.
DocumentBuilder builder = new DocumentBuilder();
// You might need to specify a different encoding depending on your plain text files.
using (StreamReader reader = new StreamReader(docsDir + "LoadTxt.txt", Encoding.UTF8))
{
// Read plain text "lines" and convert them into paragraphs in the document.
while (true)
{
string line = reader.ReadLine();
if (line != null)
builder.Writeln(line);
else
break;
}
}
// Save in any Aspose.Words supported format.
builder.Document.Save(docsDir + "LoadTxt Out.docx");
builder.Document.Save(docsDir + "LoadTxt Out.html");
}
}
}
[Visual Basic]
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Reflection
Imports System.Text
Imports Aspose.Words
Namespace LoadTxt
Friend Class Program
Shared Sub Main(ByVal args As String())
' Sample infrastructure.
Dim exeDir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Dim docsDir As String = Path.GetFullPath(Path.Combine(exeDir, "..\..\..\Common\"))
' This object will help us generate the document.
Dim builder As DocumentBuilder = New DocumentBuilder()
' You might need to specify a different encoding depending on your plain text files.
Using reader As StreamReader = New StreamReader(docsDir & "LoadTxt.txt", Encoding.UTF8)
' Read plain text "lines" and convert them into paragraphs in the document.
Do While True
Dim line As String = reader.ReadLine()
If Not line Is Nothing Then
builder.Writeln(line)
Else
Exit Do
End If
Loop
End Using
' Save in any Aspose.Words supported format.
builder.Document.Save(docsDir & "LoadTxt Out.docx")
builder.Document.Save(docsDir & "LoadTxt Out.html")
End Sub
End Class
End Namespace
[Java]
import com.aspose.words.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
public class Program
{
public static void main(String[] args) throws Exception
{
// Sample infrastructure.
String projectDir = System.getProperty("user.dir");
String docsDir = projectDir + "/../Common/";
// This object will help us generate the document.
DocumentBuilder builder = new DocumentBuilder();
// You might need to specify a different encoding depending on your plain text files.
InputStreamReader input = new InputStreamReader(new FileInputStream(docsDir + "LoadTxt.txt"), "UTF-8");
BufferedReader reader = new BufferedReader(input);
try
{
while (true)
{
String line = reader.readLine();
if (line != null)
builder.writeln(line);
else
break;
}
}
finally
{
reader.close();
}
// Save in any Aspose.Words supported format.
builder.getDocument().save(docsDir + "LoadTxt Out.docx");
builder.getDocument().save(docsDir + "LoadTxt Out.html");
}
}