private void showPage(int indexx) { if (mPdfRenderer.getPageCount() <= indexx) { index=mPdfRenderer.getPageCount()-1; 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. imgPdf.setImageBitmap(bitmap); mAttacher.update(); updateUi(); }
@Override @SuppressWarnings("NewApi") public Object instantiateItem(ViewGroup container, int position) { View v = inflater.inflate(R.layout.view_pdf_page, container, false); ImageView iv = (ImageView) v.findViewById(R.id.imageView); if (renderer == null || getCount() < position) { return v; } PdfRenderer.Page page = getPDFPage(renderer, position); Bitmap bitmap = bitmapContainer.get(position); page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); iv.setImageBitmap(bitmap); container.addView(v, 0); return v; }
/** * Creates a {@link Bitmap} in the correct size and renders the region defined by rect of the * {@link PdfRenderer.Page} into it. * * @param rect the rect of the {@link PdfRenderer.Page} to be rendered to the bitmap * @param sampleSize the sample size * @return a bitmap containing the rendered rect of the page */ @Override public Bitmap decodeRegion(Rect rect, int sampleSize) { int bitmapWidth = rect.width() / sampleSize; int bitmapHeight = rect.height() / sampleSize; Bitmap bitmap = Bitmap.createBitmap(bitmapWidth , bitmapHeight, Bitmap.Config.ARGB_8888); Matrix matrix = new Matrix(); matrix.setScale(scale/sampleSize,scale/sampleSize); matrix.postTranslate(-rect.left/sampleSize, -rect.top/sampleSize); page.render(bitmap, null, matrix, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); return bitmap; }
/** * Creates a {@link Bitmap}in the correct size and renders the {@link PdfRenderer.Page} into it. * * @param context not used * @param uri not used * @return a bitmap, containing the pdf page * @throws Exception, if rendering fails */ @Override public Bitmap decode(Context context, Uri uri) throws Exception { ParcelFileDescriptor descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); PdfRenderer renderer = new PdfRenderer(descriptor); final PdfRenderer.Page page = renderer.openPage(position); Bitmap bitmap = Bitmap.createBitmap((int) (page.getWidth() * scale + 0.5), (int) (page.getHeight() * scale + 0.5f), Bitmap.Config.ARGB_8888); page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); renderer.close(); descriptor.close(); return bitmap; }
@Override @SuppressWarnings("NewApi") public Object instantiateItem(ViewGroup container, int position) { View v = inflater.inflate(R.layout.view_pdf_page, container, false); ImageView iv = (ImageView) v.findViewById(R.id.imageView); if (renderer == null || getCount() < position) { return v; } PdfRenderer.Page page = getPDFPage(renderer, position); Bitmap bitmap = bitmapContainer.get(position); page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); iv.setImageBitmap(bitmap); ((ViewPager) container).addView(v, 0); return v; }
private void openRenderer( ) { // In this sample, we read a PDF from the assets directory. File f = new File(uri.getPath()); try { mFileDescriptor = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY); mPdfRenderer = new PdfRenderer(mFileDescriptor); cnt=mPdfRenderer.getPageCount(); } catch (Exception e) { log.e(e); } }
@Override @SuppressWarnings("NewApi") public Object instantiateItem(ViewGroup container, int position) { View v = inflater.inflate(R.layout.view_pdf_page, container, false); ImageView iv = (ImageView) v.findViewById(R.id.imageView); if (renderer == null || getCount() < position) { return v; } PdfRenderer.Page page = getPDFPage(renderer, position); Bitmap bitmap = bitmapContainer.get(position); page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); PhotoViewAttacher attacher = new PhotoViewAttacher(iv); attacher.setScale(scale.getScale(), scale.getCenterX(), scale.getCenterY(), true); attacher.setOnMatrixChangeListener(this); attachers.put(position, new WeakReference<PhotoViewAttacher>(attacher)); iv.setImageBitmap(bitmap); attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { @Override public void onPhotoTap(View view, float x, float y) { if (pageClickListener != null) { pageClickListener.onPageTap(view, x, y); } } }); attacher.update(); container.addView(v, 0); return v; }
@SuppressWarnings("NewApi") protected void init() { try { renderer = new PdfRenderer(getSeekableFileDescriptor(pdfPath)); inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); PdfRendererParams params = extractPdfParamsFromFirstPage(renderer, renderQuality); bitmapContainer = new SimpleBitmapPool(params); } catch (IOException e) { e.printStackTrace(); } }
@SuppressWarnings("NewApi") private PdfRendererParams extractPdfParamsFromFirstPage(PdfRenderer renderer, float renderQuality) { PdfRenderer.Page samplePage = getPDFPage(renderer, FIRST_PAGE); PdfRendererParams params = new PdfRendererParams(); params.setRenderQuality(renderQuality); params.setOffScreenSize(offScreenSize); params.setWidth((int) (samplePage.getWidth() * renderQuality)); params.setHeight((int) (samplePage.getHeight() * renderQuality)); samplePage.close(); return params; }
/** * Open pdf render */ private void openRenderer(Context context) throws IOException { // In this sample, we read a PDF from the assets directory. mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor(); // This is the PdfRenderer we use to render the PDF. if (mFileDescriptor != null) { mPdfRenderer = new PdfRenderer(mFileDescriptor); System.out.println("successfully open pdf render"); } else System.out.println("cant create pdf file description"); }
/** * Show pages by index */ private void showPage(int index) { if (mPdfRenderer == null || mPdfRenderer.getPageCount() <= index) { System.out.println("pdf render is null"); 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). if (pdfBitmap != null) { mImageView.setImageBitmap(null); pdfBitmap.recycle(); } pdfBitmap = 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(pdfBitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); // We are ready to show the Bitmap to user. mImageView.setImageBitmap(pdfBitmap); updateUi(); }
/** * @param file the pdf file */ public PDFPagerAdapter(Context context, File file) { super(); this.context = context; this.file = file; this.scale = 8; try { mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); renderer = new PdfRenderer(mFileDescriptor); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Initializes the region decoder. This method initializes * @param context not used here * @param uri not used here (file is already loaded) * @return the rescaled point * @throws Exception */ @Override public Point init(Context context, Uri uri) throws Exception { this.descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); this.renderer = new PdfRenderer(descriptor); page = renderer.openPage(position); return new Point((int) ((page.getWidth() * scale + 0.5f)), (int) ((page.getHeight() * scale + 0.5f))); }
@Override @SuppressWarnings("NewApi") public Object instantiateItem(ViewGroup container, int position) { View v = inflater.inflate(R.layout.view_pdf_page, container, false); ImageView iv = (ImageView) v.findViewById(R.id.imageView); if (renderer == null || getCount() < position) { return v; } PdfRenderer.Page page = getPDFPage(renderer, position); Bitmap bitmap = bitmapContainer.get(position); page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); page.close(); PhotoViewAttacher attacher = new PhotoViewAttacher(iv); attacher.setScale(scale.getScale(), scale.getCenterX(), scale.getCenterY(), true); attacher.setOnMatrixChangeListener(this); attachers.put(position, new WeakReference<PhotoViewAttacher>(attacher)); iv.setImageBitmap(bitmap); attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { @Override public void onPhotoTap(View view, float x, float y) { pageClickListener.onClick(view); } }); attacher.update(); ((ViewPager) container).addView(v, 0); return v; }
/** * Loads the PDF file at the given path into the pdf.js component within WebView. */ public void loadPdfFile(final String fileId, final int initialPage) throws IOException { File pdfFile = new File(getContext().getCacheDir(), fileId); try (InputStream in = DB.Singleton.get(getContext()).getInputStreamForFile(fileId); FileOutputStream out = new FileOutputStream(pdfFile)) { ByteStreams.copy(in, out); } mRenderer = new PdfRenderer( ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY)); setPage(initialPage); }
@SuppressWarnings("NewApi") protected PdfRenderer.Page getPDFPage(PdfRenderer renderer, int position) { return renderer.openPage(position); }