Convert TEXT to PDF via Android via Java

TEXT to PDF Android via Java conversion. Programmers can use this example code to export TEXT to PDF

Convert TEXT to PDF in Android via Java

How to convert TEXT to PDF? You can easily convert programmatically a document from TEXT to PDF format using Android via Java. Use just a few lines of Android via Java code to convert files with high quality. The Aspose.PDF library will allow any developer to efficiently solve the tasks of converting TEXT to PDF using Android via Java.

For a more detailed description of the code snippet and other possible conversion formats, see the Documentation pages. Also, you can check the other conversions of formats, which are supported by our library.

In order to convert TEXT to PDF, we’ll use Aspose.PDF for Android via Java API which is a feature-rich, powerful, and easy-to-use conversion API for Android via Java platform. Check the details of Installing the Library on the Documentation pages. To verify the benefits of the library, try using the conversion TEXT to PDF code snippet. To install and use Aspose.PDF for Android via Java extract files from the ZIP archive.

How to Convert TEXT to PDF


Android via Java developers can easily load & convert TEXT files to PDF in just a few lines of code.

  1. Select a Text file for converting
  2. Use StringBuilder to read the Text file
  3. Instantiate Document object and add a new page in the Pages collection
  4. Create a new object of TextFragment and pass StringBuilder object to its constructor
  5. Save the output PDF file using the Save method

Here is an example that demonstrates how to convert TEXT to PDF in Android via Java. You can follow these easy steps to convert your TEXT file to PDF format. First, upload your TEXT file and then simply save it as a PDF file. You can use fully qualified filenames for both TEXT reading and PDF writing. The output PDF content and formatting will be identical to the original TEXT document.

Example: Convert TEXT to PDF via Android via Java

This sample code shows TEXT to PDF Android via Java Conversion

Input file:

File not added

Output format:

PDF

Output file:

    public void convertTXTtoPDF_Simple () {
        // Initialize document object

        File pdfDocumentFileName=new File(fileStorage, "demo_txt.pdf");
        File txtDocumentFileName=new File(fileStorage, "Conversion/rfc822.txt");

        // Instantiate a Document object by calling its empty constructor
        document=new Document();

        // Add a new page in Pages collection of Document
        Page page=document.getPages().add();

        String string;
        StringBuilder stringBuilder=new StringBuilder();
        InputStream is;
        try {
            is=new FileInputStream(txtDocumentFileName);
        } catch (FileNotFoundException e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        BufferedReader reader=new BufferedReader(new InputStreamReader(is));
        while (true) {
            try {
                if ((string=reader.readLine()) == null) break;
            } catch (IOException e) {
                resultMessage.setText(e.getMessage());
                return;
            }
            stringBuilder.append(string).append("\n");
        }
        try {
            is.close();
        } catch (IOException e) {
            resultMessage.setText(e.getMessage());
            return;
        }


        // Create an instance of TextFragment and pass the text from reader object to its
        // constructor as argument
        TextFragment text=new TextFragment(stringBuilder.toString());

        // Add a new text paragraph in paragraphs collection and pass the TextFragment
        // object
        page.getParagraphs().add(text);

        // Save resultant PDF file
        try {
            document.save(pdfDocumentFileName.toString());
        } catch (Exception e) {
            resultMessage.setText(e.getMessage());
            return;
        }
        resultMessage.setText(R.string.success_message);
    }

Convert TEXT to PDF using Android via Java library

Aspose.PDF for Android via Java is a PDF processing API that allows Android apps to read, write, manipulate, and render PDF documents. It has a simple and easy-to-use API for developers. With Aspose.PDF for Android via Java, you can easily convert text and images, and convert PDF to various document formats with high performance and quality. You can also use the Aspose.PDF converter app to check the quality of conversions and view the results online.

You can find more information about Aspose.PDF for Android via Java API on documentation and examples on how to use API