by
8,582 18
0
5
2,723
8
Top 1% !
Famous
Easy-to-find
Specified
MultiPlatform
Popularity: 6570th place
Languagestata
LicenseMIT_X11

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.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1246240/How-to-Convert-Large-Text-Files-to-PDF-i?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
// Enter here the actual content of the snippet. //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")
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus