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

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.
//your code here...//[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")

Url: http://www.aspose.com/.net/pdf-component.aspx

Language: C# | User: Sheraz Khan | Created: May 25, 2016 | Tags: Convert a text to PDF convert text file contents to PDF Convert Large Text Files to PDF .NET PDF component .NET PDF API export text files to PDF Aspose.Pdf for .NET