• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Convert Large & Small Text Files to PDF inside Android Applications

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This technical tip shows how developers can convert text files to a PDF file inside their Android application. We often get queries from customers who would like to convert their text files to PDF. 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. Sometimes you may need to convert large text files into PDF format. Using the above specified techniques are not appropriate to fulfill 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 should 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.

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

try{

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

int ch = -1;

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

reader.close();

//Instantiate Pdf pbject 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.IOException ioe){
System.out.println(ioe.getMessage());

The following code snippet shows you how convert large text files into PDF format.

try{
//Instantiate Pdf pbject 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
FileInputStream fstream = new FileInputStream("/mnt/sdcard/LargeText.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = 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.IOException ioe){
System.out.println(ioe.getMessage());
}catch(Exception e){
System.out.println(e.getMessage());
}

Overview: Aspose.Pdf for Android

Aspose.Pdf for Android is a PDF document creation & manipulation component that enables Android applications to read, write & manipulate PDF document without using any other third party application. It allows PDF compression options, table creation & manipulation, support for graph objects, extended security controls, custom font handling, add or remove bookmarks, create table of contents, add, update, delete attachments & annotations, import or export PDF form data, print PDF docs & much more.

More about Aspose.Pdf for Android

- Homepage of Aspose.Pdf for Android
- Download Aspose.Pdf for Android
- Read More Technical Tips by Aspose.Pdf for Android.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic