/* PDF text extractor based on PDFlib TET
 *
 * Version for Android, derived from standard extractor.java
 *
 * $Id: extractor_android.java,v 1.2 2011/11/22 15:28:58 rjs Exp $
 */

package com.pdflib;

import java.io.*;
import android.app.Activity;
import android.os.Bundle;
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 extractor_android extends Activity implements OnClickListener {
    TextView textResult;
    Button buttonGo;
    EditText editInput;
    private static final String TAG = "TET";

    @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);
    }

    /*
     * example how to switch on logging
    final String logging = "logging={filename=/data/data/com.pdflib/trace.txt}";
     */
    static String infile = "/data/data/com.pdflib/FontReporter.pdf";
    static String outfile = "/data/data/com.pdflib/FontReporter.txt";
    public void onClick(View view) {
        String out = "OK";

	/**
	 * Global option list
	 */
	final String globaloptlist = "searchpath={{data} {resource/cmap}}";

	/**
	 * Document-specific option list
	 */
	final String docoptlist = "";

	/**
	 * Page-specific option list
	 */
	final String pageoptlist = "granularity=page";

	/**
	 * Separator to emit after each chunk of text. This depends on the
	 * applications needs; for granularity=word a space character may be useful.
	 */
	final String separator = "\n";

	TET tet = null;

	try
	{
	    Writer outfp = new BufferedWriter(new OutputStreamWriter(
		    new FileOutputStream(outfile), "UTF-8"));

	    tet = new TET();

	    /* tet.set_option(logging); */
	    tet.set_option(globaloptlist);

	    int doc = tet.open_document(infile, docoptlist);

	    if (doc == -1)
	    {
		throw new Exception("Error " + tet.get_errnum() + " in "
			+ tet.get_apiname() + "(): " + tet.get_errmsg());
	    }

	    /* get number of pages in the document */
	    int n_pages = (int) tet.pcos_get_number(doc, "length:pages");

	    /* loop over pages in the document */
	    for (int pageno = 1; pageno <= n_pages; ++pageno)
	    {
		String text;
		int page;

		page = tet.open_page(doc, pageno, pageoptlist);

		if (page == -1)
		{
		    out += print_tet_error(tet, pageno);
		    continue; /* try next page */
		}

		/*
		 * Retrieve all text fragments; This is actually not required
		 * for granularity=page, but must be used for other
		 * granularities.
		 */
		while ((text = tet.get_text(page)) != null)
		{
		    /* print the retrieved text */
		    outfp.write(text);

		    /* print a separator between chunks of text */
		    outfp.write(separator);
		}


		if (tet.get_errnum() != 0)
		{
		    out += print_tet_error(tet, pageno);
		}

		tet.close_page(page);
	    }

	    tet.close_document(doc);
	    outfp.close();
	}
	catch (TETException e)
	{
	    out = "TET exception occurred in extractor sample:" +
	    "[" + e.get_errnum() + "] " + e.get_apiname() +
			    ": " + e.get_errmsg();
	}
	catch (Exception e)
	{
	    out = "Exception: " + e.getMessage();
	}
	finally
	{
	    if (tet != null) {
		tet.delete();
	    }
	    Log.d(TAG, out);
	}

	/**
	 * Report a TET error.
	 *
	 * @param tet The TET object
	 * @param pageno The page number on which the error occurred
	 */
        textResult.setText(out);
    }

    private static String print_tet_error(TET tet, int pageno)
    {
	return(" Error " + tet.get_errnum() + " in  "
		+ tet.get_apiname() + "() on page " + pageno + ": "
		+ tet.get_errmsg());
    }

}
