react-native-pdf icon indicating copy to clipboard operation
react-native-pdf copied to clipboard

setPage() method isn't working

Open yoh-space opened this issue 10 months ago • 8 comments

when I try to set page the app doesn't change the page

yoh-space avatar Mar 04 '25 12:03 yoh-space

+1

Chirag7096 avatar Mar 15 '25 09:03 Chirag7096

+1

lucianv avatar Mar 24 '25 10:03 lucianv

I made a patch file that fixes this issue also referenced in issue #817

patches/react-native-pdf+6.7.7.patch

--- a/node_modules/react-native-pdf/android/src/main/java/org/wonday/pdf/PdfView.java
+++ b/node_modules/react-native-pdf/android/src/main/java/org/wonday/pdf/PdfView.java
@@ -12,6 +12,8 @@ import java.io.File;
 
 import android.content.ContentResolver;
 import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
 import android.util.SizeF;
 import android.view.View;
 import android.view.ViewGroup;
@@ -157,7 +159,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
         TopChangeEvent tce = new TopChangeEvent(surfaceId, getId(), event);
 
         if (dispatcher != null) {
-            dispatcher.dispatchEvent(tce);
+            new Handler(Looper.getMainLooper()).postDelayed(() -> dispatcher.dispatchEvent(tce), 10);
         }
         //        ReactContext reactContext = (ReactContext)this.getContext();
 //        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
@@ -347,6 +349,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
     // page start from 1
     public void setPage(int page) {
         this.page = page>1?page:1;
+        this.jumpTo(this.page - 1);  // Subtract 1 because PDFView uses 0-based page numbers
     }
 
     public void setScale(float scale) {

You'll need to install patch-package and add "postinstall": "patch-package" to your scripts section in package.json. Then it works for me best of luck!

xksteven avatar Apr 02 '25 16:04 xksteven

+1

zulfio avatar Apr 21 '25 07:04 zulfio

+1

alqamabinsadiq avatar May 08 '25 17:05 alqamabinsadiq

public void setPage(int page) {
    this.jumpTo(page-1);
}

nazacity avatar Jul 23 '25 07:07 nazacity

"postinstall": "patch-package"

dude you crazy it works now thank you in advanced you saved my whole project

yoh-space avatar Jul 23 '25 17:07 yoh-space

I made a patch file that fixes this issue also referenced in issue #817

patches/react-native-pdf+6.7.7.patch

--- a/node_modules/react-native-pdf/android/src/main/java/org/wonday/pdf/PdfView.java
+++ b/node_modules/react-native-pdf/android/src/main/java/org/wonday/pdf/PdfView.java
@@ -12,6 +12,8 @@ import java.io.File;
 
 import android.content.ContentResolver;
 import android.content.Context;
+import android.os.Handler;
+import android.os.Looper;
 import android.util.SizeF;
 import android.view.View;
 import android.view.ViewGroup;
@@ -157,7 +159,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
         TopChangeEvent tce = new TopChangeEvent(surfaceId, getId(), event);
 
         if (dispatcher != null) {
-            dispatcher.dispatchEvent(tce);
+            new Handler(Looper.getMainLooper()).postDelayed(() -> dispatcher.dispatchEvent(tce), 10);
         }
         //        ReactContext reactContext = (ReactContext)this.getContext();
 //        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
@@ -347,6 +349,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
     // page start from 1
     public void setPage(int page) {
         this.page = page>1?page:1;
+        this.jumpTo(this.page - 1);  // Subtract 1 because PDFView uses 0-based page numbers
     }
 
     public void setScale(float scale) {

You'll need to install patch-package and add "postinstall": "patch-package" to your scripts section in package.json.

For those using pnpm, use:

pnpm patch react-native-pdf@<your-version>

then

pnpm patch-commit <path-generated-by-the-command-above>

I solved all my Android problems with this, thanks guys! Not the ideal solution, but 1 less issue today lol

marcelino-borges avatar Aug 15 '25 22:08 marcelino-borges