/** * Creates a new instance. * <p> * <strong>Note:</strong> The provided file descriptor must be <strong>seekable</strong>, * i.e. its data being randomly accessed, e.g. pointing to a file. * </p> * <p> * <strong>Note:</strong> This class takes ownership of the passed in file descriptor * and is responsible for closing it when the renderer is closed. * </p> * <p> * If the file is from an untrusted source it is recommended to run the renderer in a separate, * isolated process with minimal permissions to limit the impact of security exploits. * </p> * * @param input Seekable file descriptor to read from. * * @throws java.io.IOException If an error occurs while reading the file. * @throws java.lang.SecurityException If the file requires a password or * the security scheme is not supported. */ publicPdfRenderer(@NonNull ParcelFileDescriptor input){ //略... }
/** * Create a new ParcelFileDescriptor accessing a given file. * * @param file The file to be opened. * @param mode The desired access mode, must be one of * {@link #MODE_READ_ONLY}, {@link #MODE_WRITE_ONLY}, or * {@link #MODE_READ_WRITE}; may also be any combination of * {@link #MODE_CREATE}, {@link #MODE_TRUNCATE}, * {@link #MODE_WORLD_READABLE}, and * {@link #MODE_WORLD_WRITEABLE}. * @return a new ParcelFileDescriptor pointing to the given file. * @throws FileNotFoundException if the given file does not exist or can not * be opened with the requested mode. * @see #parseMode(String) */ publicstatic ParcelFileDescriptor open(File file, int mode)throws FileNotFoundException { final FileDescriptor fd = openInternal(file, mode); if (fd == null) returnnull; returnnew ParcelFileDescriptor(fd); }
ParcelFileDescriptor mFileDescriptor; PdfRenderer mPdfRenderer; //根据文件对象创建 mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); // This is the PdfRenderer we use to render the PDF. if (mFileDescriptor != null) { mPdfRenderer = new PdfRenderer(mFileDescriptor); }
private PdfRenderer.Page mCurrentPage; privatevoidshowPage(int index){ if (mPdfRenderer.getPageCount() <= index) { return; } // Make sure to close the current page before opening another one. if (null != mCurrentPage) { mCurrentPage.close(); } // Use `openPage` to open a specific page in PDF. mCurrentPage = mPdfRenderer.openPage(index); // Important: the destination bitmap must be ARGB (not RGB). Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),Bitmap.Config.ARGB_8888); // Here, we render the page onto the Bitmap. // To render a portion of the page, use the second and third parameter. Pass nulls to get // the default result. // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter. mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); // We are ready to show the Bitmap to user. mImageView.setImageBitmap(bitmap); updateUi(); }
mPageIndex = 0; // If there is a savedInstanceState (screen orientations, etc.), we restore the page index. if (null != savedInstanceState) { mPageIndex = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0); } }
/** * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources. */ privatevoidopenRenderer(Context context)throws IOException { // In this sample, we read a PDF from the assets directory. File file = new File(context.getCacheDir(), FILENAME); if (!file.exists()) { // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into // the cache directory. InputStream asset = context.getAssets().open(FILENAME); FileOutputStream output = new FileOutputStream(file); finalbyte[] buffer = newbyte[1024]; int size; while ((size = asset.read(buffer)) != -1) { output.write(buffer, 0, size); } asset.close(); output.close(); } mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); // This is the PdfRenderer we use to render the PDF. if (mFileDescriptor != null) { mPdfRenderer = new PdfRenderer(mFileDescriptor); } }
/** * Closes the {@link android.graphics.pdf.PdfRenderer} and related resources. * * @throws java.io.IOException When the PDF file cannot be closed. */ privatevoidcloseRenderer()throws IOException { if (null != mCurrentPage) { mCurrentPage.close(); } mPdfRenderer.close(); mFileDescriptor.close(); }
/** * Shows the specified page of PDF to the screen. * * @param index The page index. */ privatevoidshowPage(int index){ if (mPdfRenderer.getPageCount() <= index) { return; } // Make sure to close the current page before opening another one. if (null != mCurrentPage) { mCurrentPage.close(); } // Use `openPage` to open a specific page in PDF. mCurrentPage = mPdfRenderer.openPage(index); // Important: the destination bitmap must be ARGB (not RGB). Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(), Bitmap.Config.ARGB_8888); // Here, we render the page onto the Bitmap. // To render a portion of the page, use the second and third parameter. Pass nulls to get // the default result. // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter. mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); // We are ready to show the Bitmap to user. mImageView.setImageBitmap(bitmap); updateUi(); }
/** * Updates the state of 2 control buttons in response to the current page index. */ privatevoidupdateUi(){ 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)); }
/** * Gets the number of pages in the PDF. This method is marked as public for testing. * * @return The number of pages. */ publicintgetPageCount(){ return mPdfRenderer.getPageCount(); }
@Override publicvoidonClick(View view){ switch (view.getId()) { case R.id.previous: { // Move to the previous page showPage(mCurrentPage.getIndex() - 1); break; } case R.id.next: { // Move to the next page showPage(mCurrentPage.getIndex() + 1); break; } } }