Skip to content

Commit c5ff7f3

Browse files
authored
fix: Loaded page is null error when bookmarks is 0 (#978)
* fix: `Loaded page is null` error when `bookmarks` is 0 * fix: enableRTL implementation on Android which caused many issues --------- Co-authored-by: war-in <[email protected]>
1 parent d24e733 commit c5ff7f3

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

android/src/main/java/org/wonday/pdf/PdfView.java

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ public class PdfView extends PDFView implements OnPageChangeListener,OnLoadCompl
8989
private int oldW = 0;
9090
private int oldH = 0;
9191

92-
private int totalPages = 0;
93-
private int[] pagesArrays;
94-
private int bookmarks = 0;
95-
9692
public PdfView(Context context, AttributeSet set){
9793
super(context, set);
9894
}
@@ -251,7 +247,7 @@ public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int d
251247
if (originalWidth == 0) {
252248
originalWidth = pageWidth;
253249
}
254-
250+
255251
if (lastPageWidth>0 && lastPageHeight>0 && (pageWidth!=lastPageWidth || pageHeight!=lastPageHeight)) {
256252
// maybe change by other instance, restore zoom setting
257253
Constants.Pinch.MINIMUM_ZOOM = this.minScale;
@@ -287,36 +283,19 @@ protected void onAttachedToWindow() {
287283
this.drawPdf();
288284
}
289285

286+
private int getPdfPageCount(File pdfFile) throws IOException {
287+
ParcelFileDescriptor fileDescriptor =
288+
ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY);
289+
PdfRenderer renderer = new PdfRenderer(fileDescriptor);
290+
int pageCount = renderer.getPageCount();
291+
renderer.close();
292+
fileDescriptor.close();
293+
return pageCount;
294+
}
295+
290296
public void drawPdf() {
291297
showLog(format("drawPdf path:%s %s", this.path, this.page));
292-
File file = new File(this.path);
293-
294-
if (file.exists()) {
295-
try {
296-
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
297-
PdfRenderer pdfRenderer = new PdfRenderer(fileDescriptor);
298-
this.totalPages = pdfRenderer.getPageCount();
299-
int[] pagesArrays = new int[this.totalPages];
300-
if (this.enableRTL) {
301-
if(this.page>0){
302-
this.page= this.bookmarks-1;
303-
}else{
304-
this.page=this.totalPages;
305-
}
306-
for (int i = totalPages-1; i>=0; i--) {
307-
pagesArrays[i] =totalPages-1- i;
308-
}
309-
this.pagesArrays = pagesArrays;
310-
311-
}else{
312-
this.pagesArrays = null;
313-
this.page=this.bookmarks-1;
314-
}
315-
} catch (IOException e) {
316-
Log.e("error", "error read PDF", e);
317-
}
318-
}
319-
298+
320299
if (this.path != null){
321300

322301
// set scale
@@ -342,9 +321,7 @@ public void drawPdf() {
342321
configurator = this.fromUri(getURI(this.path));
343322
}
344323

345-
configurator
346-
.pages(this.pagesArrays)
347-
.defaultPage(this.page)
324+
configurator.defaultPage(this.page-1)
348325
.swipeHorizontal(this.horizontal)
349326
.onPageChange(this)
350327
.onLoad(this)
@@ -361,7 +338,24 @@ public void drawPdf() {
361338
.enableSwipe(!this.singlePage && this.scrollEnabled)
362339
.enableDoubletap(!this.singlePage && this.enableDoubleTapZoom)
363340
.enableAnnotationRendering(this.enableAnnotationRendering)
364-
.linkHandler(this);
341+
.linkHandler(this)
342+
;
343+
344+
if (enableRTL) {
345+
try {
346+
int pageCount = getPdfPageCount(new File(this.path));
347+
int[] reversedPages = new int[pageCount];
348+
for (int i=0; i<pageCount; i++) {
349+
reversedPages[i] = pageCount-1 - i;
350+
}
351+
configurator.pages(reversedPages);
352+
if(this.page != 1){
353+
this.page = pageCount;
354+
}
355+
} catch (IOException e) {
356+
Log.e("error", "error while reading PDF", e);
357+
}
358+
}
365359

366360
if (this.singlePage) {
367361
configurator.pages(this.page-1);
@@ -384,14 +378,12 @@ public void setPath(String path) {
384378

385379
// page start from 1
386380
public void setPage(int page) {
387-
this.page = page;
388-
this.bookmarks = page;
381+
this.page = Math.max(page, 1);
389382
}
390383

391-
public void setEnableRTL(boolean enableRTL){
392-
this.enableRTL= enableRTL;
393-
394-
}
384+
public void setEnableRTL(boolean enableRTL) {
385+
this.enableRTL = enableRTL;
386+
}
395387

396388
public void setScale(float scale) {
397389
this.scale = scale;

0 commit comments

Comments
 (0)