Free Support Forum - aspose.com

Open .txt files

Hi, is there any way to use Aspose.word to open a .txt file? I get an error for opening a .txt file. I’m trying to use it to convert it to tiff files. Can you help?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please see the following link to learn how to load plain text (TXT) documents:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-load-plain-text-txt-files.html

Hope this helps.

Best regards,

This code worked for me… hope this helps

string name = uploadFile.FileName.ToString();
string[] file_details = name.Substring(name.LastIndexOf("\") + 1).Split(’.’);
;
string filename = file_details[file_details.Length - 2];
;
try
{

//save the file to the server
uploadFile.PostedFile.SaveAs(name);

if (file_details[file_details.Length - 1].Equals(“txt”))
{

try
{
const int BUFFER_SIZE = 255;
int nBytesRead = 0;
Byte[] Buffer = new Byte[BUFFER_SIZE];
StringBuilder strUploadedContent = new StringBuilder("");

Stream theStream = uploadFile.PostedFile.InputStream;
nBytesRead = theStream.Read(Buffer, 0, BUFFER_SIZE);

while (0 != nBytesRead)
{
strUploadedContent.Append(Encoding.ASCII.GetString(Buffer, 0, nBytesRead));
nBytesRead = theStream.Read(Buffer, 0, BUFFER_SIZE);
}
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Pdf pdf1 = new Aspose.Pdf.Pdf();

//Create a new section in the Pdf object
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Text t2 = new Aspose.Pdf.Text(strUploadedContent.ToString());

sec1.Paragraphs.Add(t2);

pdf1.Save(Server.MapPath(“a file location where you have write read access”) + filename + “.pdf”);


}
catch (Exception ex)
{
showPDF.Text = “
Exception has occured
” + ex.ToString();
}


Hi Abhilasha,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing your experience, but I do not think this code will be useful in this case. Question was about loading TXT file into Aspose.Words.Document object, but in your code you are loading text into Aspose.Pdf.Pdf object.

If it is necessary to load TXT file into Aspose.Words.Document object, you should use code suggested by Andrey:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-load-plain-text-txt-files.html

Best regards.

Thank you all for your response. I was wondering when I’m building a new doc using the stream reading, how to I retain the font, font size, and other properties from the text file? Because some of my documents are size 72 Bold but when I used the suggested code, those original properties were not carry over to word. Is there a way to get information about the font properties of a text file programmatically? Please help.

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. TXT document does not contain any information about text formatting. Text size and other text options are settings of Notepad (or other TXT viewer). If you would like to use text formatting, you can try using RTF or other Word formats.

Best regards,