Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 35 additions & 43 deletions android/src/main/java/org/wonday/pdf/PdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -251,7 +247,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;
Expand Down Expand Up @@ -287,36 +283,19 @@ 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= this.bookmarks-1;
}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=this.bookmarks-1;
}
} catch (IOException e) {
Log.e("error", "error read PDF", e);
}
}


if (this.path != null){

// set scale
Expand All @@ -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)
Expand All @@ -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<pageCount; i++) {
reversedPages[i] = pageCount-1 - i;
}
configurator.pages(reversedPages);
if(this.page != 1){
this.page = pageCount;
}
} catch (IOException e) {
Log.e("error", "error while reading PDF", e);
}
}

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

// page start from 1
public void setPage(int page) {
this.page = page;
this.bookmarks = page;
this.page = Math.max(page, 1);
}

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

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

public void setScale(float scale) {
this.scale = scale;
Expand Down