Hello friends,
Today we are learn about how to create PDF and read PDF file.
actvity_main.xml
ActvityMain.java
ShowPdfActvity.java
Thank you
Live Sample
Today we are learn about how to create PDF and read PDF file.
actvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actvities.MainActivity">
<include
android:id="@+id/tool0"
layout="@layout/toolbar" />
<LinearLayout
android:layout_below="@+id/tool0"
android:id="@+id/options_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp" >
<android.support.v7.widget.AppCompatButton
android:background="@drawable/rounded_shape1"
android:id="@+id/btn_create_"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Create New Pdf" />
</LinearLayout>
<LinearLayout
android:layout_below="@+id/options_layout"
android:id="@+id/pdf_selection_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:visibility="visible" >
<ListView
android:id="@+id/pdfList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actvities.MainActivity">
<include
android:id="@+id/tool0"
layout="@layout/toolbar" />
<LinearLayout
android:layout_below="@+id/tool0"
android:id="@+id/options_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp" >
<android.support.v7.widget.AppCompatButton
android:background="@drawable/rounded_shape1"
android:id="@+id/btn_create_"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Create New Pdf" />
</LinearLayout>
<LinearLayout
android:layout_below="@+id/options_layout"
android:id="@+id/pdf_selection_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:visibility="visible" >
<ListView
android:id="@+id/pdfList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</RelativeLayout>
ActvityMain.java
package com.samset.create_pdf_sample.actvities;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.samset.create_pdf_sample.R;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private AppCompatButton btn_write;
private Toolbar toolbar;
// File Descriptor for rendered Pdf file
private ParcelFileDescriptor mFileDescriptor;
private File[] filelist;
private ArrayAdapter<String> adapter;
ListView listView;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.tool0);
toolbar.setTitle(R.string.app_name);
btn_write=(AppCompatButton)findViewById(R.id.btn_create_);
listView =(ListView)findViewById(R.id.pdfList);
btn_write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,CreatePdfActivity.class));
}
});
new PdfListLoadTask().execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
fileName = adapter.getItem(position);
startActivity(new Intent(MainActivity.this,ShowPdfActivity.class).putExtra("data", fileName));
}
});
}
private class PdfListLoadTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File files = new File(path+"");
filelist = files.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return ((name.endsWith(".pdf")));
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
if (filelist != null && filelist.length >= 1) {
ArrayList<String> fileNameList = new ArrayList<>();
for (int i = 0; i < filelist.length; i++)
fileNameList.add(filelist[i].getPath());
adapter = new ArrayAdapter<>(getApplicationContext(),
R.layout.list_item, fileNameList);
listView.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(),
"No pdf file found, Please create new Pdf file",
Toast.LENGTH_LONG).show();
}
}
}
}
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.samset.create_pdf_sample.R;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private AppCompatButton btn_write;
private Toolbar toolbar;
// File Descriptor for rendered Pdf file
private ParcelFileDescriptor mFileDescriptor;
private File[] filelist;
private ArrayAdapter<String> adapter;
ListView listView;
String fileName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.tool0);
toolbar.setTitle(R.string.app_name);
btn_write=(AppCompatButton)findViewById(R.id.btn_create_);
listView =(ListView)findViewById(R.id.pdfList);
btn_write.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,CreatePdfActivity.class));
}
});
new PdfListLoadTask().execute();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
fileName = adapter.getItem(position);
startActivity(new Intent(MainActivity.this,ShowPdfActivity.class).putExtra("data", fileName));
}
});
}
private class PdfListLoadTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File files = new File(path+"");
filelist = files.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return ((name.endsWith(".pdf")));
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
if (filelist != null && filelist.length >= 1) {
ArrayList<String> fileNameList = new ArrayList<>();
for (int i = 0; i < filelist.length; i++)
fileNameList.add(filelist[i].getPath());
adapter = new ArrayAdapter<>(getApplicationContext(),
R.layout.list_item, fileNameList);
listView.setAdapter(adapter);
} else {
Toast.makeText(getApplicationContext(),
"No pdf file found, Please create new Pdf file",
Toast.LENGTH_LONG).show();
}
}
}
}
ShowPdfActvity.java
package com.samset.create_pdf_sample.actvities;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;
import com.samset.create_pdf_sample.R;
import com.samset.create_pdf_sample.listeners.CurrentView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ShowPdfActivity extends AppCompatActivity {
private Toolbar toolbar;
private ParcelFileDescriptor mFileDescriptor;
private PdfRenderer mPdfRenderer;
private Intent intent;
private String strinput;
private ImageView pdfView;
private static int currentView;
private PdfRenderer.Page mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_pdf);
pdfView=(ImageView)findViewById(R.id.iv_pdfview);
toolbar=(Toolbar)findViewById(R.id.tool);
toolbar.setTitle(R.string.app_name);
intent=getIntent();
strinput=intent.getStringExtra("data");
if (!strinput.isEmpty())
{
openRenderer(strinput);
showPage(1);
Log.v("Show"," data "+strinput);
}else{
Log.v("Show"," data null ");
}
}
@SuppressLint("NewApi")
private void openRenderer(String filePath) {
File file = new File(filePath);
try {
mFileDescriptor = ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
mPdfRenderer = new PdfRenderer(mFileDescriptor);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* API for cleanup of objects used in rendering
*/
@SuppressLint("NewApi")
private void closeRenderer() {
try {
if (mCurrentPage != null)
mCurrentPage.close();
if (mPdfRenderer != null)
mPdfRenderer.close();
if (mFileDescriptor != null)
mFileDescriptor.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressLint("NewApi")
private void showPage(int index) {
if (mPdfRenderer == null || mPdfRenderer.getPageCount() <= index
|| index < 0) {
return;
}
// For closing the current page before opening another one.
try {
if (mCurrentPage != null) {
mCurrentPage.close();
}
} catch (Exception e) {
e.printStackTrace();
}
// Open page with specified index
mCurrentPage = mPdfRenderer.openPage(index);
Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(),
mCurrentPage.getHeight(), Bitmap.Config.ARGB_8888);
// Pdf page is rendered on Bitmap
mCurrentPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
// Set rendered bitmap to ImageView
pdfView.setImageBitmap(bitmap);
}
}
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.ParcelFileDescriptor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ImageView;
import com.samset.create_pdf_sample.R;
import com.samset.create_pdf_sample.listeners.CurrentView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ShowPdfActivity extends AppCompatActivity {
private Toolbar toolbar;
private ParcelFileDescriptor mFileDescriptor;
private PdfRenderer mPdfRenderer;
private Intent intent;
private String strinput;
private ImageView pdfView;
private static int currentView;
private PdfRenderer.Page mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_pdf);
pdfView=(ImageView)findViewById(R.id.iv_pdfview);
toolbar=(Toolbar)findViewById(R.id.tool);
toolbar.setTitle(R.string.app_name);
intent=getIntent();
strinput=intent.getStringExtra("data");
if (!strinput.isEmpty())
{
openRenderer(strinput);
showPage(1);
Log.v("Show"," data "+strinput);
}else{
Log.v("Show"," data null ");
}
}
@SuppressLint("NewApi")
private void openRenderer(String filePath) {
File file = new File(filePath);
try {
mFileDescriptor = ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
mPdfRenderer = new PdfRenderer(mFileDescriptor);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* API for cleanup of objects used in rendering
*/
@SuppressLint("NewApi")
private void closeRenderer() {
try {
if (mCurrentPage != null)
mCurrentPage.close();
if (mPdfRenderer != null)
mPdfRenderer.close();
if (mFileDescriptor != null)
mFileDescriptor.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressLint("NewApi")
private void showPage(int index) {
if (mPdfRenderer == null || mPdfRenderer.getPageCount() <= index
|| index < 0) {
return;
}
// For closing the current page before opening another one.
try {
if (mCurrentPage != null) {
mCurrentPage.close();
}
} catch (Exception e) {
e.printStackTrace();
}
// Open page with specified index
mCurrentPage = mPdfRenderer.openPage(index);
Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(),
mCurrentPage.getHeight(), Bitmap.Config.ARGB_8888);
// Pdf page is rendered on Bitmap
mCurrentPage.render(bitmap, null, null,
PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
// Set rendered bitmap to ImageView
pdfView.setImageBitmap(bitmap);
}
}
activity_show_pdf.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actvities.ShowPdfActivity">
<include
android:id="@+id/tool"
layout="@layout/toolbar" />
<LinearLayout
android:id="@+id/read_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tool"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:id="@+id/iv_pdfview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"
android:scaleType="fitCenter" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".actvities.ShowPdfActivity">
<include
android:id="@+id/tool"
layout="@layout/toolbar" />
<LinearLayout
android:id="@+id/read_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tool"
android:orientation="vertical"
android:visibility="visible">
<ImageView
android:id="@+id/iv_pdfview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"
android:scaleType="fitCenter" />
</LinearLayout>
</RelativeLayout>
Thank you
FullSourceCodePDFCreaterSample
Live Sample
No comments:
Post a Comment