Skip to content

Commit

Permalink
Fixed bug when pdf should open as normal but was throwing exception
Browse files Browse the repository at this point in the history
Added more descriptive exceptions
Updated README
Updated version
  • Loading branch information
barteksc committed Jun 16, 2016
1 parent 1ebe3b8 commit d4c24a5
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 76 deletions.
31 changes: 21 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ Forked specially for use with [PdfViewPager](https://github.com/barteksc/PdfView
* Added methods to get width and height of page in points (1/72") (like in `PdfRenderer.Page` class):
* `int getPageWidthPoint(PdfDocument doc, int index);`
* `int getPageHeightPoint(PdfDocument doc, int index);`
* `newDocument()` throws IOException
API is fully compatible with original version, only additional methods were created.
## What's new in 1.0.3'?
* probably fixed bug when pdf should open as normal but was throwing exception
* added much more descriptive exception messages
## Installation
Add to _build.gradle_:
`compile 'com.github.barteksc:pdfium-android:1.0.2'`
`compile 'com.github.barteksc:pdfium-android:1.0.3'`
Library is available in jcenter and Maven Central repositories.
Expand All @@ -31,19 +36,25 @@ ImageView iv = (ImageView) findViewById(R.id.imageView);
FileDescriptor fd = ...;
int pageNum = 0;
PdfiumCore pdfiumCore = new PdfiumCore(context);
PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
try {
PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
pdfiumCore.openPage(pdfDocument, pageNum);
pdfiumCore.openPage(pdfDocument, pageNum);
int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);
int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
width, height);
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
width, height);
iv.setImageBitmap(bitmap);
iv.setImageBitmap(bitmap);
pdfiumCore.closeDocument(pdfDocument);
} catch(IOException ex) {
ex.printStackTrace();
}
```
## Build native part
Go to `PROJECT_PATH/src/main/jni` and run command `$ ndk-build`.
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ext {
siteUrl = 'https://github.com/barteksc/PdfiumAndroid'
gitUrl = 'https://github.com/barteksc/PdfiumAndroid.git'

libraryVersion = '1.0.2'
libraryVersion = '1.0.3'

developerId = 'barteksc'
developerName = 'Bartosz Schiller'
Expand All @@ -44,7 +44,7 @@ android {
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0.2"
versionName "1.0.3"
}
buildTypes {
release {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/shockwave/pdfium/PdfDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
public class PdfDocument {
public final Object Lock = new Object();

/*package*/ PdfDocument(){}
/*package*/ PdfDocument() {
}

/*package*/ long mNativeDocPtr;

/*package*/ final Map<Integer, Long> mNativePagesPtr = new ArrayMap<>();
public boolean hasPage(int index){ return mNativePagesPtr.containsKey(index); }

public boolean hasPage(int index) {
return mNativePagesPtr.containsKey(index);
}
}
11 changes: 0 additions & 11 deletions src/main/java/com/shockwave/pdfium/PdfOpenException.java

This file was deleted.

109 changes: 62 additions & 47 deletions src/main/java/com/shockwave/pdfium/PdfiumCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,68 @@
import android.view.Surface;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.reflect.Field;

public class PdfiumCore {
private static final String TAG = PdfiumCore.class.getName();

static{
static {
System.loadLibrary("pdfium");
System.loadLibrary("jniPdfium");
}

private native long nativeOpenDocument(int fd);

private native void nativeCloseDocument(long docPtr);

private native int nativeGetPageCount(long docPtr);

private native long nativeLoadPage(long docPtr, int pageIndex);

private native long[] nativeLoadPages(long docPtr, int fromIndex, int toIndex);

private native void nativeClosePage(long pagePtr);

private native void nativeClosePages(long[] pagesPtr);

private native int nativeGetPageWidthPixel(long pagePtr, int dpi);

private native int nativeGetPageHeightPixel(long pagePtr, int dpi);

private native int nativeGetPageWidthPoint(long pagePtr);

private native int nativeGetPageHeightPoint(long pagePtr);

//private native long nativeGetNativeWindow(Surface surface);
//private native void nativeRenderPage(long pagePtr, long nativeWindowPtr);
private native void nativeRenderPage(long pagePtr, Surface surface, int dpi,
int startX, int startY,
int drawSizeHor, int drawSizeVer);

private native void nativeRenderPageBitmap(long pagePtr, Bitmap bitmap, int dpi,
int startX, int startY,
int drawSizeHor, int drawSizeVer);
int startX, int startY,
int drawSizeHor, int drawSizeVer);

private static final Class FD_CLASS = FileDescriptor.class;
private static final String FD_FIELD_NAME = "descriptor";
private static Field mFdField = null;

private int mCurrentDpi;

public PdfiumCore(Context ctx){
public PdfiumCore(Context ctx) {
mCurrentDpi = ctx.getResources().getDisplayMetrics().densityDpi;
}

public static int getNumFd(FileDescriptor fdObj){
try{
if(mFdField == null){
public static int getNumFd(FileDescriptor fdObj) {
try {
if (mFdField == null) {
mFdField = FD_CLASS.getDeclaredField(FD_FIELD_NAME);
mFdField.setAccessible(true);
}

return mFdField.getInt(fdObj);
}catch(NoSuchFieldException e){
} catch (NoSuchFieldException e) {
e.printStackTrace();
return -1;
} catch (IllegalAccessException e) {
Expand All @@ -63,116 +76,118 @@ public static int getNumFd(FileDescriptor fdObj){
}
}

public PdfDocument newDocument(FileDescriptor fd) throws PdfOpenException {
public PdfDocument newDocument(FileDescriptor fd) throws IOException {
PdfDocument document = new PdfDocument();

document.mNativeDocPtr = nativeOpenDocument(getNumFd(fd));
if(document.mNativeDocPtr == -1) {
Log.e(TAG, "Open document failed");
throw new PdfOpenException("Open document failed");
}

return document;
}
public int getPageCount(PdfDocument doc){
synchronized (doc.Lock){

public int getPageCount(PdfDocument doc) {
synchronized (doc.Lock) {
return nativeGetPageCount(doc.mNativeDocPtr);
}
}

public long openPage(PdfDocument doc, int pageIndex){
synchronized (doc.Lock){
public long openPage(PdfDocument doc, int pageIndex) {
synchronized (doc.Lock) {
long pagePtr = nativeLoadPage(doc.mNativeDocPtr, pageIndex);
doc.mNativePagesPtr.put(pageIndex, pagePtr);
return pagePtr;
}
}
public long[] openPage(PdfDocument doc, int fromIndex, int toIndex){
synchronized (doc.Lock){

public long[] openPage(PdfDocument doc, int fromIndex, int toIndex) {
synchronized (doc.Lock) {
long[] pagesPtr = nativeLoadPages(doc.mNativeDocPtr, fromIndex, toIndex);
int pageIndex = fromIndex;
for(long page : pagesPtr){
if(pageIndex > toIndex) break;
for (long page : pagesPtr) {
if (pageIndex > toIndex) break;
doc.mNativePagesPtr.put(pageIndex, page);
pageIndex++;
}

return pagesPtr;
}
}
public int getPageWidth(PdfDocument doc, int index){
synchronized (doc.Lock){

public int getPageWidth(PdfDocument doc, int index) {
synchronized (doc.Lock) {
Long pagePtr;
if( (pagePtr = doc.mNativePagesPtr.get(index)) != null ){
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageWidthPixel(pagePtr, mCurrentDpi);
}
return 0;
}
}
public int getPageHeight(PdfDocument doc, int index){
synchronized (doc.Lock){

public int getPageHeight(PdfDocument doc, int index) {
synchronized (doc.Lock) {
Long pagePtr;
if( (pagePtr = doc.mNativePagesPtr.get(index)) != null ){
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageHeightPixel(pagePtr, mCurrentDpi);
}
return 0;
}
}
public int getPageWidthPoint(PdfDocument doc, int index){
synchronized (doc.Lock){

public int getPageWidthPoint(PdfDocument doc, int index) {
synchronized (doc.Lock) {
Long pagePtr;
if( (pagePtr = doc.mNativePagesPtr.get(index)) != null ){
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageWidthPoint(pagePtr);
}
return 0;
}
}
public int getPageHeightPoint(PdfDocument doc, int index){
synchronized (doc.Lock){

public int getPageHeightPoint(PdfDocument doc, int index) {
synchronized (doc.Lock) {
Long pagePtr;
if( (pagePtr = doc.mNativePagesPtr.get(index)) != null ){
if ((pagePtr = doc.mNativePagesPtr.get(index)) != null) {
return nativeGetPageHeightPoint(pagePtr);
}
return 0;
}
}

public void renderPage(PdfDocument doc, Surface surface, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY){
synchronized (doc.Lock){
try{
int startX, int startY, int drawSizeX, int drawSizeY) {
synchronized (doc.Lock) {
try {
//nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi);
nativeRenderPage(doc.mNativePagesPtr.get(pageIndex), surface, mCurrentDpi,
startX, startY, drawSizeX, drawSizeY);
}catch(NullPointerException e){
startX, startY, drawSizeX, drawSizeY);
} catch (NullPointerException e) {
Log.e(TAG, "mContext may be null");
e.printStackTrace();
}catch(Exception e){
} catch (Exception e) {
Log.e(TAG, "Exception throw from native");
e.printStackTrace();
}
}
}

public void renderPageBitmap(PdfDocument doc, Bitmap bitmap, int pageIndex,
int startX, int startY, int drawSizeX, int drawSizeY){
synchronized (doc.Lock){
try{
int startX, int startY, int drawSizeX, int drawSizeY) {
synchronized (doc.Lock) {
try {
nativeRenderPageBitmap(doc.mNativePagesPtr.get(pageIndex), bitmap, mCurrentDpi,
startX, startY, drawSizeX, drawSizeY);
}catch(NullPointerException e){
} catch (NullPointerException e) {
Log.e(TAG, "mContext may be null");
e.printStackTrace();
}catch(Exception e){
} catch (Exception e) {
Log.e(TAG, "Exception throw from native");
e.printStackTrace();
}
}
}

public void closeDocument(PdfDocument doc){
synchronized (doc.Lock){
for(Integer index : doc.mNativePagesPtr.keySet()){
public void closeDocument(PdfDocument doc) {
synchronized (doc.Lock) {
for (Integer index : doc.mNativePagesPtr.keySet()) {
nativeClosePage(doc.mNativePagesPtr.get(index));
}
doc.mNativePagesPtr.clear();
Expand Down
Loading

0 comments on commit d4c24a5

Please sign in to comment.