This Following technical tip shows how developers can convert text files to a PDF file inside their Android application. They often get queries from customers who would like to convert their text files to PDF. They 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, they 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.

The following code snippet shows you how convert text files to PDF.

Code:
try{

StringBuffersb = new StringBuffer(1024);
BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/test.txt"));

intch = -1;

while( (ch = reader.read()) > -1)
{
sb.append((char)ch);      
   }

reader.close();

	//Instantiate Pdfpbject by calling its empty constructor
	Pdf pdf1 = new Pdf();

	//Create a new section in the Pdf object
	Section sec1 = pdf1.getSections().add();

	//Create a new text paragraph and pass the text to its constructor as argument
	Text text1 = new Text(sec1,sb.toString());

	sec1.getParagraphs().add(text1);

	pdf1.save("/mnt/sdcard/Text_File_to_PDF.pdf");	

}

catch(java.io.IOExceptionioe)

{
System.out.println(ioe.getMessage());
The following code snippet shows you how convert large text files into PDF format.

Code:
try{
//Instantiate Pdfpbject by calling its empty constructor
Pdf pdf1 = new Pdf();        
//Create a new section in the Pdf object
Section sec1 = pdf1.getSections().add();

// Open the file that is the first 
// command line parameter
FileInputStreamfstream = new FileInputStream("/mnt/sdcard/LargeText.txt");
	// Get the object of DataInputStream
	DataInputStream in = new DataInputStream(fstream);
BufferedReaderbr = new BufferedReader(new InputStreamReader(in));
	String strLine;
	//Read File Line By Line
while ((strLine = br.readLine()) != null)   
{
	//Create a new text paragraph and pass the text to its constructor as argument
	Text text1 = new Text(sec1,strLine);
		sec1.getParagraphs().add(text1);
}

	//Close the input stream
	in.close();

        // Save the PDF file
pdf1.save("/mnt/sdcard/LargeText.pdf");

}
catch(java.io.IOExceptionioe)
{
System.out.println(ioe.getMessage());
}
catch(Exception e)
{
System.out.println(e.getMessage());
      }
If you want to read more technical tips for android then click here.