How to create a pdf file programmatically in android? QnA

How to create a pdf file programmatically in android? QnA:


You need to download and Paste Jar DynamicPDF in libs Folder.
Create Activity and apply below Code :
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.cete.dynamicpdf.Document;
import com.cete.dynamicpdf.Font;
import com.cete.dynamicpdf.PageSize;
import com.cete.dynamicpdf.TextAlign;
import com.cete.dynamicpdf.pageelements.Label;
import com.vbagetech.taxmunshi.gstbillingapp.R;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;

public class PDFActivity extends AppCompatActivity {
    private static String FILE;
    String[] invoiceTitlelistArray, invoicepricelistArray, invoiceQNTlistArray, invoiceNOlistArray;
    String invoiceNO = "";
    TextView path_pdf;
    File storagePath;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_pdfhello_world);
        path_pdf = (TextView) findViewById(R.id.path_pdf_ID);
        Button button=(Button)findViewById(R.id.backbtn_pdf_ID);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               onBackPressed();
            }
        });
        storagePath = Environment.getExternalStorageDirectory();
        invoiceNO = "KJHS_q23";
        FILE = Environment.getExternalStorageDirectory() + "/" + invoiceNO+".pdf";
        fatchinvoiceList();
    }
void fatchinvoiceList() {
        // Create a document and set it's properties
        Document objDocument = new Document();
        objDocument.setCreator("Powered By : Google.com");
        objDocument.setAuthor("Google");
        objDocument.setTitle("Google Invoice");
// Create a page to add to the document
        Page objPage = new Page(PageSize.LETTER, PageOrientation.PORTRAIT, 54.0f);

        String strText = "Google";
        Label invoiceTitle = new Label(strText, 0, 0, 504, 100, Font.getHelvetica(), 25, TextAlign.CENTER);


        // Create a Label to add to the page

        Label objLabel = new Label("Company Name\n Mobile No\n Address\n", 0, 0, 504, 100, Font.getHelvetica(), 12, TextAlign.LEFT);

        Label objLabel2 = new Label("Owner Name\nMobile No\nAddress", 0, 0, 504, 100, Font.getHelvetica(), 12, TextAlign.RIGHT);
        // Add label to page

        objPage.getElements().add(invoiceTitle);
        objPage.getElements().add(objLabel);
        objPage.getElements().add(objLabel2);

        // Add page to document
        objDocument.getPages().add(objPage);

        try {
            // Outputs the document to file
            objDocument.draw(FILE);
            Toast.makeText(DynamicPDFHelloWorldActivity.this, "File has been written to :" + FILE,
                    Toast.LENGTH_LONG).show();
            path_pdf.setText(FILE);
            File file = new File(Environment.getExternalStorageDirectory(), "/" + invoiceNO);
            Uri path = Uri.fromFile(file);
            Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
            pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pdfOpenintent.setDataAndType(path, "application/pdf");
            try {
                startActivity(pdfOpenintent);
            } catch (ActivityNotFoundException e) {

            }
        } catch (Exception e) {
            Toast.makeText(DynamicPDFHelloWorldActivity.this,
                    "Error, unable to write to file\n" + e.getMessage(),
                    Toast.LENGTH_LONG).show();
        }
    }
@Override
    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(getBaseContext(), DashboardActivity.class));
        finish();
    }
}
Or You can download Create PDF Example by Click

Comments

Popular posts from this blog

VBage Tech, How website help a business to grow?

How to use Voice Commands into an android application? QnA