import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;
public class PdfRendererBasicFragment extends Fragment implements View.OnClickListener
{
private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index";
private ParcelFileDescriptor mFileDescriptor;
private PdfRenderer mPdfRenderer;
private PdfRenderer.Page mCurrentPage;
private ImageView mImageView;
private Button mButtonPrevious;
private Button mButtonNext;
public PdfRendererBasicFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_pdf_renderer_basic, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
mImageView = (ImageView) view.findViewById(R.id.image);
mButtonPrevious = (Button) view.findViewById(R.id.previous);
mButtonNext = (Button) view.findViewById(R.id.next);
mButtonPrevious.setOnClickListener(this);
mButtonNext.setOnClickListener(this);
int index = 0;
if (null != savedInstanceState)
{
index = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0);
}
showPage(index);
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
openRenderer(activity);
}
catch (IOException e)
{
e.printStackTrace();
activity.finish();
}
}
@Override
public void onDetach()
{
try
{
closeRenderer();
}
catch (IOException e)
{
e.printStackTrace();
}
super.onDetach();
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
if (null != mCurrentPage)
{
outState.putInt(STATE_CURRENT_PAGE_INDEX, mCurrentPage.getIndex());
}
}
private void openRenderer(Context context) throws IOException
{
mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor();
mPdfRenderer = new PdfRenderer(mFileDescriptor );
}
private void closeRenderer() throws IOException
{
if (null != mCurrentPage)
{
mCurrentPage.close();
}
mPdfRenderer.close();
mFileDescriptor.close();
}
private void showPage(int index)
{
if (mPdfRenderer.getPageCount() <= index)
{
return;
}
if (null != mCurrentPage)
{
mCurrentPage.close();
}
mCurrentPage = mPdfRenderer.openPage(index);
Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),Bitmap.Config.ARGB_8888);
mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
mImageView.setImageBitmap(bitmap);
updateUi();
}
private void updateUi()
{
int index = mCurrentPage.getIndex();
int pageCount = mPdfRenderer.getPageCount();
mButtonPrevious.setEnabled(0 != index);
mButtonNext.setEnabled(index + 1 < pageCount);
getActivity().setTitle(getString(R.string.app_name_with_index, index + 1, pageCount));
}
public int getPageCount()
{
return mPdfRenderer.getPageCount();
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.previous:
{
showPage(mCurrentPage.getIndex() - 1);
break;
}
case R.id.next:
{
showPage(mCurrentPage.getIndex() + 1);
break;
}
}
}
}
============================
// CALL From Activity
=============================
public static final String FRAGMENT_PDF_RENDERER_BASIC= "pdf_renderer_basic";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_real);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction().add(R.id.container, new PdfRendererBasicFragment(),FRAGMENT_PDF_RENDERER_BASIC).commit();
}
}
=============
XML FILES
==============
1) Fragment
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity$PlaceholderFragment">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white"
android:scaleType="fitCenter" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal">
<Button
android:id="@+id/previous"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/previous" />
<Button
android:id="@+id/next"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/next" />
</LinearLayout>
</LinearLayout>
2. Activity
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />