Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Convert Large & Small Text Files to PDF inside Android Applications

sheraz786sheraz786 Posts: 19Member

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.

Comments

Sign In or Register to comment.