Question Detail

Hello All,
How can we generate PDFs on Android device or Write on PDF file?
Please share any example.


Thread Reply

Correct Answer

Hemant Sharma

- 1 years ago

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

Anonymous

- 1 years ago

Gradle dependency

If you prefer to use Gradle, you can download iTextG from the Maven Central Repository by adding this to your Gradle file:

dependencies {
    compile 'com.itextpdf:itextg:5.5.11'
}

Bili Greed

- 1 years ago

Viger - View Pager PDF OpenSource

Viger PDF How to Works is file pdf extract or convert to bitmap use Library Vudroid was write C Language, after that images include to adapter viewpager

Download

Gradle:

repositories {
        jcenter()
        maven { url 'https://jitpack.io' }
}
dependencies {
    compile 'com.github.aerdy:Android-Viger_View_Pager_PDF_OpenSource:-SNAPSHOT'
}

Support Android Version

        minSdkVersion 15

Library Support Open Source PDF and Connection

  • Vudroid Library PDF
  • Retrofit 2 Get Stream Connection

Support Vertical Swap and Horizontal Swap

    <com.necistudio.vigerpdf.utils.ViewPagerZoomHorizontal
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.necistudio.vigerpdf.utils.ViewPagerZoomVertical
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Support Get PDF From URL

private void fromNetwork(String endpoint) {
        new VigerPDF(this, endpoint).initFromFile(new OnResultListener() {
            @Override
            public void resultData(ArrayList<Bitmap> data) {
                VigerAdapter adapter = new VigerAdapter(getApplicationContext(),data);
                viewPager.setAdapter(adapter);
            }
        });
    }

Supprt Get PDF From File

 private void fromFile(String path) {
        File file = new File(path);
        new VigerPDF(this, file).initFromFile(new OnResultListener() {
            @Override
            public void resultData(ArrayList<Bitmap> data) {
                VigerAdapter adapter = new VigerAdapter(getApplicationContext(),data);
                viewPager.setAdapter(adapter);
            }
        });
    }