article

How to Convert Large Text Files to PDF inside .NET Applications

Email
Submitted on: 5/25/2016 8:12:48 AM
By: Sherazam  
Level: Intermediate
User Rating: Unrated
Compatibility: C#, VB.NET
Views: 2244
 
     This technical tip shows how to convert a text file to PDF inside .NET applications. We often get queries from customers who would like to convert their text files to PDF. We are often asked if we can quickly provide some code which can accomplish this task and save them the effort of going through the documentation. So for the benefit of everyone, we present here a simple example which can be used as it is to easily and efficiently convert a text file to PDF using Aspose.Pdf. We may need to convert large text files into PDF format. Using the above specified techniques are not appropri ate to fullfil the requirement as it can generate OutOfMemoryException or can make the system slow down, because in that technique we are reading the complete file using ReadTOEnd method.

 
				This technical tip shows how to convert a text file to PDF inside .NET applications. We often get queries from customers who would like to convert their text files to PDF. We are often asked if we can quickly provide some code which can accomplish this task and save them the effort of going through the documentation. So for the benefit of everyone, we present here a simple example which can be used as it is to easily and efficiently convert a text file to PDF using Aspose.Pdf. We may need to convert large text files into PDF format. Using the above specified techniques are not appropri ate to fullfil the requirement as it can generate OutOfMemoryException or can make the system slow down, because in that technique we are reading the complete file using ReadTOEnd method. Instead of ReadToEnd method we sould be using ReadLine method. As its name suggests, reads text one line at a time. In order to do this, though, you need to use a loop. You can then loop round each line and create a text object for each text line and add it to paragraphs collection of section. In order to identify the end of file, we need to use Peek method which takes a peek at the incoming text characters. It's looking ahead one character at a time. If it doesn't see any more characters, it will return a value of minus 1. This will signify the end of the text file.
Please take a look over the following code snippet for converting a text file to PDF
//[C# Code Sample]
 
//read the source text file
System.IO.TextReader tr = new StreamReader("test.txt");
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a new section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Create a new text paragraph and pass the text to its constructor as argument
Aspose.Pdf.Generator.Text t2 = new Aspose.Pdf.Generator.Text(tr.ReadToEnd());
sec1.Paragraphs.Add(t2);
pdf1.Save("test.Pdf");
//[VB.NET Code Sample]
 
Dim tr As System.IO.TextReader = New StreamReader("test.txt")
'Instantiate Pdf pbject by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Create a new section in the Pdf object
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
'Create a new text paragraph and pass the text to its constructor as argument
Dim t2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(tr.ReadToEnd())
sec1.Paragraphs.Add(t2)
pdf1.Save("test.Pdf")
Converting Large Text files to PDF
//[C# Code Sample]
 
//Instantiate Pdf pbject by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Create a new section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Specify the location of input text file
String FILE_NAME = "d:/pdftest/LargeText.txt";
if (File.Exists(FILE_NAME))
{
System.IO.TextReader objReader = new System.IO.StreamReader(FILE_NAME);
// Read the file till the end of the file has come
do 
{
 //Create a new text paragraph & pass text to its constructor as argument
 Aspose.Pdf.Generator.Text t2 = new Aspose.Pdf.Generator.Text(objReader.ReadLine());
 // add the text object to paragraphs collection of section
 sec1.Paragraphs.Add(t2);
 // Read till the end of file
 }while(objReader.Peek() != -1);
// Close the StreamReader object
objReader.Close();
}
else
MessageBox.Show("File Does Not Exist");
// Save the PDF file
pdf1.Save("d:/pdftest/large_textfile.pdf");
//[VB.NET Code Sample]
 
'Instantiate Pdf pbject by calling its empty constructor
Dim pdf1 As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
'Create a new section in the Pdf object
Dim sec1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
' Specify the location of input text file
Dim FILE_NAME As String = "d:/pdftest/LargeText.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
' Read the file till the end of the file has come
Do While objReader.Peek() <> -1
'Create a new text paragraph and pass text to its constructor as argument
Dim t2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(objReader.ReadLine())
' add the text object to paragraphs collection of section
sec1.Paragraphs.Add(t2)
Loop
' Close the StreamReader object
objReader.Close()
Else
MsgBox("File Does Not Exist")
End If
' Save the PDF file
pdf1.Save("d:/pdftest/large_textfile.pdf")

More about Aspose.Pdf for .NET


Other 44 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.