The following technical tip show how developers can convert text files to a PDF file inside their Android application with Aspose.PDF for Android Library.
The following code snippet shows you how convert text files to PDF.
The following code snippet shows you how convert large text files into PDF format.
If you want to read more technical tips for android then click here.
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());
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());
}