Free Support Forum - aspose.com

Convert a TXT document to PDF

When I try to convert a TXT document into a PDF using Aspose.Words, I get this error,"Unknown file format". Why is that? I thought ASpose.Words for .NET can be used for conversion of TXT documents to PDF.
This message was posted using Aspose.Live 2 Forum

Hello

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

Thanks for your interest in Aspose.Words. Please see the following link to learn how to load plain text into an Aspose.Words.Document object:

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,

Thank you very much for the reponse.

But the problem I'm facing now, is the PDF Document, gets opened, but there is no content in this. I make use of Aspose.Words for .Net and I read from a stream. This is my code.

Dim oFileStream As System.IO.MemoryStream

oFileStream = New System.IO.MemoryStream()

oFileStream.Write(File_blob, 0, File_blob.Length)

Dim builder As DocumentBuilder = New DocumentBuilder()

Using reader As System.IO.StreamReader = New System.IO.StreamReader(oFileStream, Encoding.UTF8)

' Read plain text "lines" and convert them into paragraphs in the document.

While reader.Peek <> -1

Dim line As String = reader.ReadLine()

If line IsNot Nothing Then

builder.Writeln(line)

End If

End While

End Using

Dim dstStream As System.IO.MemoryStream = New System.IO.MemoryStream()

builder.Document.Save(dstStream, SaveFormat.Pdf)

Here, when I step down the code, when it hits While reader.Peek , then it breaks comes out of the While loop. Not sure why.

Hi

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

Thanks for your request. You should just reset position of stream, please see the code bellow:

Dim oFileStream As System.IO.MemoryStream

oFileStream = New System.IO.MemoryStream()

oFileStream.Write(File_blob, 0, File_blob.Length)

oFileStream.Position = 0

Best regards,

Thank you so much. It works fine. :slight_smile: