From c220b06ce0e2ff24022092d5c71f02fd1333938e Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 10 Oct 2025 12:34:36 +0200 Subject: [PATCH 1/2] fix: `Loaded page is null` error when `bookmarks` is 0 --- android/src/main/java/org/wonday/pdf/PdfView.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/org/wonday/pdf/PdfView.java b/android/src/main/java/org/wonday/pdf/PdfView.java index ae6836ab..305ce77d 100644 --- a/android/src/main/java/org/wonday/pdf/PdfView.java +++ b/android/src/main/java/org/wonday/pdf/PdfView.java @@ -91,7 +91,7 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl private int totalPages = 0; private int[] pagesArrays; - private int bookmarks = 0; + private int bookmarks = 0; public PdfView(Context context, AttributeSet set){ super(context, set); @@ -251,7 +251,7 @@ public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int d if (originalWidth == 0) { originalWidth = pageWidth; } - + if (lastPageWidth>0 && lastPageHeight>0 && (pageWidth!=lastPageWidth || pageHeight!=lastPageHeight)) { // maybe change by other instance, restore zoom setting Constants.Pinch.MINIMUM_ZOOM = this.minScale; @@ -299,7 +299,7 @@ public void drawPdf() { int[] pagesArrays = new int[this.totalPages]; if (this.enableRTL) { if(this.page>0){ - this.page= this.bookmarks-1; + this.page = Math.max(this.bookmarks-1, 0); }else{ this.page=this.totalPages; } @@ -307,16 +307,16 @@ public void drawPdf() { pagesArrays[i] =totalPages-1- i; } this.pagesArrays = pagesArrays; - + }else{ this.pagesArrays = null; - this.page=this.bookmarks-1; + this.page = Math.max(this.bookmarks-1, 0); } } catch (IOException e) { Log.e("error", "error read PDF", e); } } - + if (this.path != null){ // set scale @@ -390,7 +390,7 @@ public void setPage(int page) { public void setEnableRTL(boolean enableRTL){ this.enableRTL= enableRTL; - + } public void setScale(float scale) { From 9c50cbaa17afba7f31d81684a742b1999d432ed5 Mon Sep 17 00:00:00 2001 From: war-in Date: Fri, 10 Oct 2025 16:28:20 +0200 Subject: [PATCH 2/2] fix: enableRTL implementation on Android which caused many issues --- .../src/main/java/org/wonday/pdf/PdfView.java | 74 +++++++++---------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/android/src/main/java/org/wonday/pdf/PdfView.java b/android/src/main/java/org/wonday/pdf/PdfView.java index 305ce77d..efde3b04 100644 --- a/android/src/main/java/org/wonday/pdf/PdfView.java +++ b/android/src/main/java/org/wonday/pdf/PdfView.java @@ -89,10 +89,6 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl private int oldW = 0; private int oldH = 0; - private int totalPages = 0; - private int[] pagesArrays; - private int bookmarks = 0; - public PdfView(Context context, AttributeSet set){ super(context, set); } @@ -287,35 +283,18 @@ protected void onAttachedToWindow() { this.drawPdf(); } + private int getPdfPageCount(File pdfFile) throws IOException { + ParcelFileDescriptor fileDescriptor = + ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY); + PdfRenderer renderer = new PdfRenderer(fileDescriptor); + int pageCount = renderer.getPageCount(); + renderer.close(); + fileDescriptor.close(); + return pageCount; + } + public void drawPdf() { showLog(format("drawPdf path:%s %s", this.path, this.page)); - 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) { - if(this.page>0){ - this.page = Math.max(this.bookmarks-1, 0); - }else{ - this.page=this.totalPages; - } - for (int i = totalPages-1; i>=0; i--) { - pagesArrays[i] =totalPages-1- i; - } - this.pagesArrays = pagesArrays; - - }else{ - this.pagesArrays = null; - this.page = Math.max(this.bookmarks-1, 0); - } - } catch (IOException e) { - Log.e("error", "error read PDF", e); - } - } if (this.path != null){ @@ -342,9 +321,7 @@ public void drawPdf() { configurator = this.fromUri(getURI(this.path)); } - configurator - .pages(this.pagesArrays) - .defaultPage(this.page) + configurator.defaultPage(this.page-1) .swipeHorizontal(this.horizontal) .onPageChange(this) .onLoad(this) @@ -361,7 +338,24 @@ public void drawPdf() { .enableSwipe(!this.singlePage && this.scrollEnabled) .enableDoubletap(!this.singlePage && this.enableDoubleTapZoom) .enableAnnotationRendering(this.enableAnnotationRendering) - .linkHandler(this); + .linkHandler(this) + ; + + if (enableRTL) { + try { + int pageCount = getPdfPageCount(new File(this.path)); + int[] reversedPages = new int[pageCount]; + for (int i=0; i