Home > Code > C# > How to Convert Large Text Files to PDF inside .NET Applications

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

by sher azam   on May 25, 2016   Category: C#   |  Views: 435    |  Points: 25   |  Gold 


This technical tip shows how to convert a text file to PDF inside .NET applications. 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

- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Download Aspose.Pdf for .NET: http://www.aspose.com/downloads/pdf-family/net



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts