android-PageFlip icon indicating copy to clipboard operation
android-PageFlip copied to clipboard

Can we use it on PDF?

Open ashvinstech opened this issue 4 years ago • 8 comments

I want to use it on PDF file can i do that?

ashvinstech avatar Apr 06 '21 04:04 ashvinstech

@ashvinstech Yes

malikbilal1997 avatar Apr 18 '21 14:04 malikbilal1997

Is there a sample project with pdf?

khayalsharifli avatar Mar 26 '22 12:03 khayalsharifli

Sorry, I don't have sample for PDF, if someone has that, please share, thanks a lot!

eschao avatar Apr 22 '22 06:04 eschao

I had implemented it with Android PdfRenderer But it was having glitch issues. So I used https://github.com/harism/android-pagecurl it worked real smooth with pdf.

malikbilal1997 avatar Apr 22 '22 06:04 malikbilal1997

You can modify this method to use it with pdf. private void drawPage(final int number) It's in SinglePageRender

private void drawPage(final int number) {

        int width = canvas.getWidth();
        int height = canvas.getHeight();

        Paint p = new Paint();

        p.setFilterBitmap(true);

        Bitmap background = PdfRendererUtility.pdfToBitmap(context, pdfLocation, number - 1);

        Rect rect = new Rect(0, 0, width, height);

        canvas.drawBitmap(background, null, rect, p);

        background.recycle();
}

The Utility Class is used to convert the pdf pages to bitmap.

public class PdfRendererUtility {

    /**
     * Calculate Total PDF File Pages Number
     *
     * @param pdfFile File object to pdf file
     * @return 0 if failed to open the pdf file
     */
    public static int pdfTotalPages(String pdfFile) {

        int pages = 0;

        try {

            PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File(pdfFile), ParcelFileDescriptor.MODE_READ_ONLY));

            pages = renderer.getPageCount();

            renderer.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pages;
    }

    /**
     * Generate Bitmap of PDF Specified Page.
     *
     * @param context context to read the file
     * @param pdfFile File object to pdf file
     * @return null if couldn't Read pdf file.
     */
    public static Bitmap pdfToBitmap(Context context, String pdfFile, int pageNumber) {

        Bitmap bitmap = null;

        try {

            PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File(pdfFile), ParcelFileDescriptor.MODE_READ_ONLY));

            Page page = renderer.openPage(pageNumber);

            DisplayMetrics displayMetrics = new DisplayMetrics();

            ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            bitmap = Bitmap.createBitmap(displayMetrics.widthPixels, displayMetrics.heightPixels, Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(bitmap);

            canvas.drawColor(Color.WHITE);

            canvas.drawBitmap(bitmap, 0, 0, null);

            page.render(bitmap, null, null, RENDER_MODE_FOR_DISPLAY);

            page.close();

            renderer.close();

        } catch (Exception ex) {

            ex.printStackTrace();
        }
        return bitmap;
    }
}

malikbilal1997 avatar Apr 22 '22 06:04 malikbilal1997

check out my implementation here, https://github.com/shittu33/Dynamic-Page-Flip. it's built on eschao pageFlip. with this you can use an xml layout or use pdfium library.

shittu33 avatar Jun 15 '23 11:06 shittu33

I want to use double page rendering to read txt files. Is there a similar example?

CoolChimpanzee avatar Jun 19 '23 03:06 CoolChimpanzee

Oops, I have not cater for that at the moment.

On Mon, 19 Jun 2023, 04:15 CoolChimpanzee, @.***> wrote:

I want to use double page rendering to read txt files. Is there a similar example?

— Reply to this email directly, view it on GitHub https://github.com/eschao/android-PageFlip/issues/64#issuecomment-1596431592, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGGK7RJZX4HGXH6UVHUKSLDXL673RANCNFSM42N6XZRQ . You are receiving this because you commented.Message ID: @.***>

shittu33 avatar Jun 19 '23 06:06 shittu33