Saturday, 13 October 2012

How to generate PDF file in android

By Sukhpal Saini

Hi friends....

I am trying to generate a pdf file in android OS with android coding but its not possible with iText packages(used in java for generate a pdf file in windows) because Google android does not accepts to include iText in the application. Because android has separate virtual machine (Dalvik) and it does not support all java classes.

So i found a new solution for this problem now i am using pdflib for generate the pdf documents in android OS.

use this code for generate the pdf document in android

hello_android.java
package com.pdflib;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;

public class hello_android extends Activity implements OnClickListener {
    TextView textResult;
    Button buttonGo;
    EditText editInput;
    private static final String TAG = "PDFlib";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Find UI views
        textResult = (TextView) findViewById(R.id.textResult);
        buttonGo = (Button) findViewById(R.id.buttonGo);
        buttonGo.setOnClickListener(this);
    }

    public void onClick(View view) {
        String out = "OK";
        /* This is where the data files are. Adjust as necessary. */
        final String searchpath = "/data/data/com.pdflib/data";
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File(sdCard.getAbsolutePath()
    + "/stericheck.pdf");
        final String outfile = ""+dir;
        /*
         * example how to switch on logging 
        final String logging = "filename=/data/data/com.pdflib/trace.txt";
         */
        pdflib p = null;
        int font;


        try {
            p = new pdflib();

            /*
             *p.set_parameter("logging", logging);
             */
            p.set_parameter("SearchPath", searchpath);


            /* This means we must check return values of load_font() etc. */
            p.set_parameter("errorpolicy", "return");

            if (p.begin_document(outfile, "") == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.set_info("Creator", "hello.java");
            p.set_info("Author", "Thomas Merz");
            p.set_info("Title", "Hello world (Java)!");

            p.begin_page_ext(595, 842, "");

            font = p.load_font("Helvetica-Bold", "unicode", "");

            if (font == -1)
                throw new Exception("Error: " + p.get_errmsg());

            p.setfont(font, 24);

            p.set_text_pos(50, 700);
            p.show("Hello world!");
            p.continue_text("(says Java)");
            p.end_page_ext("");

            p.end_document("");

        }
        catch (PDFlibException e) {
            out = "PDFlib exception" + "[" + e.get_errnum() + "] "
                + e.get_apiname() + ": " + e.get_errmsg() + "\n";
            Log.d(TAG, out);
        }
        catch (Exception e) {
            out = "Exception: " + e.getMessage();
            Log.d(TAG, out);
        }
        finally {
            if (p != null) {
                p.delete();
            }
        }

        textResult.setText(out);
    }
}

also add two another java files put in Package

 Link for download the sample project of PDF creator project is given below


Sample PDF creator Project


And For another help in this creator contact to me.....


My English is so weak sorry for spelling mistakes and sentence mistakes  


Please send a feedback after view this post.....