Skip to content

Commit

Permalink
adding option to display pdf in dual mode and the isLandscape property
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Delubac committed Dec 18, 2019
1 parent d243b39 commit 3488428
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ protected Throwable doInBackground(Void... params) {
if (pdfView != null) {
PdfDocument pdfDocument = docSource.createDocument(pdfView.getContext(), pdfiumCore, password);
pdfFile = new PdfFile(pdfiumCore, pdfDocument, pdfView.getPageFitPolicy(), getViewSize(pdfView),
userPages, pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.isAutoSpacingEnabled(),
pdfView.isFitEachPage());
userPages, pdfView.isOnDualPageMode(), pdfView.isSwipeVertical(), pdfView.getSpacingPx(), pdfView.isAutoSpacingEnabled(),
pdfView.isFitEachPage(), pdfView.isOnLandscapeOrientation());
return null;
} else {
return new NullPointerException("pdfView == null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ enum ScrollDir {

private int defaultPage = 0;

private boolean dualPageMode = false;

private boolean isLandscapeOrientation = false;

/** True if should scroll through pages vertically instead of horizontally */
private boolean swipeVertical = true;

Expand Down Expand Up @@ -1179,6 +1183,18 @@ public boolean isBestQuality() {
return bestQuality;
}

public boolean isOnDualPageMode() {
return dualPageMode;
}

public boolean isOnLandscapeOrientation() { return isLandscapeOrientation; }

public void setLandscapeOrientation(boolean landscapeOrientation) {this.isLandscapeOrientation = landscapeOrientation; }

public void setDualPageMode(boolean dualPageMode) {
this.dualPageMode = dualPageMode;
}

public boolean isSwipeVertical() {
return swipeVertical;
}
Expand Down Expand Up @@ -1353,6 +1369,10 @@ public class Configurator {

private int defaultPage = 0;

private boolean landscapeOrientation = false;

private boolean dualPageMode = false;

private boolean swipeHorizontal = false;

private boolean annotationRendering = false;
Expand Down Expand Up @@ -1461,6 +1481,16 @@ public Configurator defaultPage(int defaultPage) {
return this;
}

public Configurator landscapeOrientation(boolean landscapeOrientation) {
this.landscapeOrientation = landscapeOrientation;
return this;
}

public Configurator dualPageMode(boolean dualPageMode) {
this.dualPageMode = dualPageMode;
return this;
}

public Configurator swipeHorizontal(boolean swipeHorizontal) {
this.swipeHorizontal = swipeHorizontal;
return this;
Expand Down Expand Up @@ -1542,6 +1572,8 @@ public void load() {
PDFView.this.setNightMode(nightMode);
PDFView.this.enableDoubletap(enableDoubletap);
PDFView.this.setDefaultPage(defaultPage);
PDFView.this.setLandscapeOrientation(landscapeOrientation);
PDFView.this.setDualPageMode(dualPageMode);
PDFView.this.setSwipeVertical(!swipeHorizontal);
PDFView.this.enableAnnotationRendering(annotationRendering);
PDFView.this.setScrollHandle(scrollHandle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class PdfFile {
private SizeF maxHeightPageSize = new SizeF(0, 0);
/** Scaled page with maximum width */
private SizeF maxWidthPageSize = new SizeF(0, 0);
/** True if dualPageMode is on*/
private boolean showTwoPages;
/** True if scrolling is vertical, else it's horizontal */
private boolean isVertical;
/** Fixed spacing between pages in pixels */
Expand All @@ -69,14 +71,17 @@ class PdfFile {
* else the largest page fits and other pages scale relatively
*/
private final boolean fitEachPage;

private final boolean isLandscape;
/**
* The pages the user want to display in order
* (ex: 0, 2, 2, 8, 8, 1, 1, 1)
*/
private int[] originalUserPages;

PdfFile(PdfiumCore pdfiumCore, PdfDocument pdfDocument, FitPolicy pageFitPolicy, Size viewSize, int[] originalUserPages,
boolean isVertical, int spacing, boolean autoSpacing, boolean fitEachPage) {
boolean showTwoPages, boolean isVertical, int spacing, boolean autoSpacing, boolean fitEachPage, boolean isLandscape) {
this.showTwoPages = showTwoPages;
this.pdfiumCore = pdfiumCore;
this.pdfDocument = pdfDocument;
this.pageFitPolicy = pageFitPolicy;
Expand All @@ -85,6 +90,7 @@ class PdfFile {
this.spacingPx = spacing;
this.autoSpacing = autoSpacing;
this.fitEachPage = fitEachPage;
this.isLandscape = isLandscape;
setup(viewSize);
}

Expand Down Expand Up @@ -122,7 +128,7 @@ public void recalculatePageSizes(Size viewSize) {
maxHeightPageSize = calculator.getOptimalMaxHeightPageSize();

for (Size size : originalPageSizes) {
pageSizes.add(calculator.calculate(size));
pageSizes.add(calculator.calculate(size, showTwoPages, isLandscape));
}
if (autoSpacing) {
prepareAutoSpacing(viewSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ private void calculateMiddle() {
pos = getY();
viewSize = getHeight();
pdfViewSize = pdfView.getHeight();
} else {
} else if (pdfView.isOnDualPageMode()){
pos = getX();
viewSize = getWidth() / 2;
pdfViewSize = pdfView.getWidth() / 2;
} else {
pos = getX();
viewSize = getWidth();
pdfViewSize = pdfView.getWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.github.barteksc.pdfviewer.util;

import android.app.Activity;
import android.util.Log;
import android.content.pm.ActivityInfo;
import com.shockwave.pdfium.util.Size;
import com.shockwave.pdfium.util.SizeF;

Expand All @@ -40,11 +43,16 @@ public PageSizeCalculator(FitPolicy fitPolicy, Size originalMaxWidthPageSize, Si
calculateMaxPages();
}

public SizeF calculate(Size pageSize) {
public SizeF calculate(Size pageSize, boolean showTwoPages, boolean isLandscape) {
if (pageSize.getWidth() <= 0 || pageSize.getHeight() <= 0) {
return new SizeF(0, 0);
}
float maxWidth = fitEachPage ? viewSize.getWidth() : pageSize.getWidth() * widthRatio;
float maxWidth = 0;
if(showTwoPages && !isLandscape){
maxWidth = fitEachPage ? viewSize.getWidth() : pageSize.getWidth() / 2 * widthRatio;
} else {
maxWidth = fitEachPage ? viewSize.getWidth() : pageSize.getWidth() * widthRatio;
}
float maxHeight = fitEachPage ? viewSize.getHeight() : pageSize.getHeight() * heightRatio;
switch (fitPolicy) {
case HEIGHT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
Expand Down Expand Up @@ -71,6 +72,8 @@ public class PDFViewActivity extends AppCompatActivity implements OnPageChangeLi

String pdfFileName;



@OptionsItem(R.id.pickFile)
void pickFile() {
int permissionCheck = ContextCompat.checkSelfPermission(this,
Expand Down Expand Up @@ -112,15 +115,24 @@ void afterViews() {
}

private void displayFromAsset(String assetFileName) {
boolean isLandscape = false;
int orientation = this.getResources().getConfiguration().orientation;
isLandscape = orientation == Configuration.ORIENTATION_LANDSCAPE;
pdfFileName = assetFileName;

pdfView.fromAsset(SAMPLE_FILE)
.defaultPage(pageNumber)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
.landscapeOrientation(isLandscape)
.dualPageMode(true)
.scrollHandle(new DefaultScrollHandle(this))
.spacing(10) // in dp
.spacing(0) // in dp
.enableSwipe(true)
.swipeHorizontal(true)
.pageFling(true)
.fitEachPage(false)
.onPageError(this)
.pageFitPolicy(FitPolicy.BOTH)
.load();
Expand All @@ -135,7 +147,11 @@ private void displayFromUri(Uri uri) {
.enableAnnotationRendering(true)
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.spacing(10) // in dp
.spacing(0) // in dp
.dualPageMode(true)
.enableSwipe(true)
.swipeHorizontal(true)
.pageFling(true)
.onPageError(this)
.load();
}
Expand Down

0 comments on commit 3488428

Please sign in to comment.