Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtl for android? #879

Open
Abdulrahman-Alanazi1 opened this issue Nov 2, 2024 · 3 comments
Open

rtl for android? #879

Abdulrahman-Alanazi1 opened this issue Nov 2, 2024 · 3 comments

Comments

@Abdulrahman-Alanazi1
Copy link

What react-native version are you using?
0.74.5

What react-native-pdf version are you using?
6.7.5

What platform does your issue occur on? (android/ios/both)
android

Describe your issue as precisely as possible :
I have a PDF file that was written in Arabic, and I would like to swipe the pages from left to right. I do not want to arrange the pages like p3-p2-p1 I just want to reverse the swiping of the pages in android, is it possible?

@irwant
Copy link

irwant commented Jan 5, 2025

That is possible, but it needs some native code in java to make it happen. I have tested, and it works.

Step 1 :
open file PdfManager.java in /node_modules/react-native-pdf/android/src/main/java/org/wonday/pdf/PdfManager.java

change :

@Override
    public void setEnableRTL(PdfView view, boolean value) {
        // NOOP on Android
    }

to :

@ReactProp(name = "enableRTL")
    public void setEnableRTL(PdfView view, boolean enableRTL) {
        pdfView.setEnableRTL(enableRTL);
    }

Step 2 :
open PdfView.java in same folder
add:

import java.io.IOException;
import android.graphics.pdf.PdfRenderer;
import android.os.ParcelFileDescriptor;

add declaration under class PdfView

 private boolean enableRTL = false;
 private int totalPages = 0;
 private int[] pagesArrays;

add method :

public void setEnableRTL(boolean enableRTL){
        this.enableRTL= enableRTL;
        
     }

add some code inside method drawPdf() under showLog :

File file = new File(this.path);

         if (file.exists()) {
             try {
                 ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                 PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
                 this.totalPages = pdfRenderer.getPageCount();
                 int[] pagesArrays = new int[this.totalPages];
                 if (this.enableRTL) {
                    for (int i = totalPages-1; i>=0; i--) {
                        pagesArrays[i] =totalPages-1- i;
                    }
                    this.pagesArrays = pagesArrays;
                    this.page=totalPages-1;
                }else{
                    for (int i = totalPages-1; i>=0; i--) {
                        pagesArrays[i] = i;
                    }
                    this.pagesArrays = pagesArrays;
                    this.page=totalPages-totalPages;
                }
             } catch (IOException e) {
                 Log.e("error", "error read PDF", e);
             }
         }

adding and change value .defaultPage(this.page-1) :
from :

configurator.defaultPage(this.page-1)
                .swipeHorizontal(this.horizontal)
                .onPageChange(this)
                .onLoad(this)

to :

configurator
                 .pages(this.pagesArrays) //set the pages
                 .defaultPage(this.page) // change value
                 .swipeHorizontal(this.horizontal)
                 .onPageChange(this)
                 .onLoad(this)

@irwant
Copy link

irwant commented Jan 5, 2025

and if you want to have a bookmark function

add some code to PdfView.java :
declare : private int bookmarks = 0;

adding on the setPage(int page) :

public void setPage(int page) {
         this.page = page>1?page:1;
         this.bookmarks = page; //add this line
     }

and change :

File file = new File(this.path);

         if (file.exists()) {
             try {
                 ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                 PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
                 this.totalPages = pdfRenderer.getPageCount();
                 int[] pagesArrays = new int[this.totalPages];
                 if (this.enableRTL) {
                    for (int i = totalPages-1; i>=0; i--) {
                        pagesArrays[i] =totalPages-1- i;
                    }
                    this.pagesArrays = pagesArrays;
                    this.page=totalPages-this.bookmarks; //change to this new code
                }else{
                    //delete old code to this new code
                    this.pagesArrays = null;
                    this.page=this.bookmarks-1;
                }
             } catch (IOException e) {
                 Log.e("error", "error read PDF", e);
             }
         }

for the save bookmarked, you can do on your React code

@irwant
Copy link

irwant commented Jan 9, 2025

and change :

File file = new File(this.path);

         if (file.exists()) {
             try {
                 ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                 PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
                 this.totalPages = pdfRenderer.getPageCount();
                 int[] pagesArrays = new int[this.totalPages];
                 if (this.enableRTL) {
                    for (int i = totalPages-1; i>=0; i--) {
                        pagesArrays[i] =totalPages-1- i;
                    }
                    this.pagesArrays = pagesArrays;
                    this.page=totalPages-this.bookmarks; //change to this new code
                }else{
                    //delete old code to this new code
                    this.pagesArrays = null;
                    this.page=this.bookmarks-1;
                }
             } catch (IOException e) {
                 Log.e("error", "error read PDF", e);
             }
         }

for the save bookmarked, you can do on your React code

EDIT :

if (this.enableRTL) {
                     for (int i = totalPages-1; i>=0; i--) {
                         pagesArrays[i] =totalPages-1- i;
                     }
                     if(this.page>1){
                        this.page= this.bookmarks-1;
                    }else{
                        this.page=totalPages; // make it jump to the first page
                    }
                     this.pagesArrays = pagesArrays;
                 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants