com.shockwave.pdfium.PdfDocument Java Examples

The following examples show how to use com.shockwave.pdfium.PdfDocument. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DecodingAsyncTask.java    From AndroidPdfViewer with Apache License 2.0 6 votes vote down vote up
@Override
protected Throwable doInBackground(Void... params) {
    try {
        PDFView pdfView = pdfViewReference.get();
        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());
            return null;
        } else {
            return new NullPointerException("pdfView == null");
        }

    } catch (Throwable t) {
        return t;
    }
}
 
Example #2
Source File: PDFView.java    From AndroidPdfViewerV2 with Apache License 2.0 5 votes vote down vote up
/**
 * Called when the PDF is loaded
 */
void loadComplete(PdfDocument pdfDocument, int pageWidth, int pageHeight) {
    state = State.LOADED;
    this.documentPageCount = pdfiumCore.getPageCount(pdfDocument);

    this.pdfDocument = pdfDocument;

    this.pageWidth = pageWidth;
    this.pageHeight = pageHeight;
    calculateOptimalWidthAndHeight();

    pagesLoader = new PagesLoader(this);

    if (!renderingHandlerThread.isAlive()) {
        renderingHandlerThread.start();
    }
    renderingHandler = new RenderingHandler(renderingHandlerThread.getLooper(),
            this, pdfiumCore, pdfDocument);
    renderingHandler.start();

    if (scrollHandle != null) {
        scrollHandle.setupLayout(this);
        isScrollHandleInit = true;
    }

    if (onLoadCompleteListener != null) {
        onLoadCompleteListener.loadComplete(documentPageCount);
    }

    jumpTo(defaultPage, false);
}
 
Example #3
Source File: PDFViewActivity.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}
 
Example #4
Source File: PDFViewActivity.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
@Override
public void loadComplete(int nbPages) {
    PdfDocument.Meta meta = pdfView.getDocumentMeta();
    Log.e(TAG, "title = " + meta.getTitle());
    Log.e(TAG, "author = " + meta.getAuthor());
    Log.e(TAG, "subject = " + meta.getSubject());
    Log.e(TAG, "keywords = " + meta.getKeywords());
    Log.e(TAG, "creator = " + meta.getCreator());
    Log.e(TAG, "producer = " + meta.getProducer());
    Log.e(TAG, "creationDate = " + meta.getCreationDate());
    Log.e(TAG, "modDate = " + meta.getModDate());

    printBookmarksTree(pdfView.getTableOfContents(), "-");

}
 
Example #5
Source File: DragPinchManager.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
private boolean checkLinkTapped(float x, float y) {
    PdfFile pdfFile = pdfView.pdfFile;
    if (pdfFile == null) {
        return false;
    }
    float mappedX = -pdfView.getCurrentXOffset() + x;
    float mappedY = -pdfView.getCurrentYOffset() + y;
    int page = pdfFile.getPageAtOffset(pdfView.isSwipeVertical() ? mappedY : mappedX, pdfView.getZoom());
    SizeF pageSize = pdfFile.getScaledPageSize(page, pdfView.getZoom());
    int pageX, pageY;
    if (pdfView.isSwipeVertical()) {
        pageX = (int) pdfFile.getSecondaryPageOffset(page, pdfView.getZoom());
        pageY = (int) pdfFile.getPageOffset(page, pdfView.getZoom());
    } else {
        pageY = (int) pdfFile.getSecondaryPageOffset(page, pdfView.getZoom());
        pageX = (int) pdfFile.getPageOffset(page, pdfView.getZoom());
    }
    for (PdfDocument.Link link : pdfFile.getPageLinks(page)) {
        RectF mapped = pdfFile.mapRectToDevice(page, pageX, pageY, (int) pageSize.getWidth(),
                (int) pageSize.getHeight(), link.getBounds());
        mapped.sort();
        if (mapped.contains(mappedX, mappedY)) {
            pdfView.callbacks.callLinkHandler(new LinkTapEvent(x, y, mappedX, mappedY, mapped, link));
            return true;
        }
    }
    return false;
}
 
Example #6
Source File: LinkTapEvent.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
public LinkTapEvent(float originalX, float originalY, float documentX, float documentY, RectF mappedLinkRect, PdfDocument.Link link) {
    this.originalX = originalX;
    this.originalY = originalY;
    this.documentX = documentX;
    this.documentY = documentY;
    this.mappedLinkRect = mappedLinkRect;
    this.link = link;
}
 
Example #7
Source File: PdfFile.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
PdfFile(PdfiumCore pdfiumCore, PdfDocument pdfDocument, FitPolicy pageFitPolicy, Size viewSize, int[] originalUserPages,
        boolean isVertical, int spacing, boolean autoSpacing, boolean fitEachPage) {
    this.pdfiumCore = pdfiumCore;
    this.pdfDocument = pdfDocument;
    this.pageFitPolicy = pageFitPolicy;
    this.originalUserPages = originalUserPages;
    this.isVertical = isVertical;
    this.spacingPx = spacing;
    this.autoSpacing = autoSpacing;
    this.fitEachPage = fitEachPage;
    setup(viewSize);
}
 
Example #8
Source File: PDFView.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
/** Will be empty until document is loaded */
public List<PdfDocument.Link> getLinks(int page) {
    if (pdfFile == null) {
        return Collections.emptyList();
    }
    return pdfFile.getPageLinks(page);
}
 
Example #9
Source File: PDFView.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
/** Will be empty until document is loaded */
public List<PdfDocument.Bookmark> getTableOfContents() {
    if (pdfFile == null) {
        return Collections.emptyList();
    }
    return pdfFile.getBookmarks();
}
 
Example #10
Source File: PDFView.java    From AndroidPdfViewer with Apache License 2.0 5 votes vote down vote up
/** Returns null if document is not loaded */
public PdfDocument.Meta getDocumentMeta() {
    if (pdfFile == null) {
        return null;
    }
    return pdfFile.getMetaData();
}
 
Example #11
Source File: PDFViewActivity.java    From AndroidPdfViewerV1 with Apache License 2.0 5 votes vote down vote up
public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}
 
Example #12
Source File: PDFViewActivity.java    From AndroidPdfViewerV1 with Apache License 2.0 5 votes vote down vote up
@Override
public void loadComplete(int nbPages) {
    PdfDocument.Meta meta = pdfView.getDocumentMeta();
    Log.e(TAG, "title = " + meta.getTitle());
    Log.e(TAG, "author = " + meta.getAuthor());
    Log.e(TAG, "subject = " + meta.getSubject());
    Log.e(TAG, "keywords = " + meta.getKeywords());
    Log.e(TAG, "creator = " + meta.getCreator());
    Log.e(TAG, "producer = " + meta.getProducer());
    Log.e(TAG, "creationDate = " + meta.getCreationDate());
    Log.e(TAG, "modDate = " + meta.getModDate());

    printBookmarksTree(pdfView.getTableOfContents(), "-");

}
 
Example #13
Source File: PDFView.java    From AndroidPdfViewerV1 with Apache License 2.0 5 votes vote down vote up
/**
 * Called when the PDF is loaded
 */
public void loadComplete(PdfDocument pdfDocument) {
    this.documentPageCount = pdfiumCore.getPageCount(pdfDocument);

    int firstPageIdx = 0;
    if (originalUserPages != null) {
        firstPageIdx = originalUserPages[0];
    }

    // We assume all the pages are the same size
    this.pdfDocument = pdfDocument;
    pdfiumCore.openPage(pdfDocument, firstPageIdx);
    openedPages.add(firstPageIdx);
    this.pageWidth = pdfiumCore.getPageWidth(pdfDocument, firstPageIdx);
    this.pageHeight = pdfiumCore.getPageHeight(pdfDocument, firstPageIdx);
    state = State.LOADED;
    calculateOptimalWidthAndHeight();

    if (!renderingHandlerThread.isAlive()) {
        renderingHandlerThread.start();
    }
    renderingHandler = new RenderingHandler(renderingHandlerThread.getLooper(),
            this, pdfiumCore, pdfDocument);

    if (scrollBar != null) {
        scrollBar.pdfLoaded();
    }

    // Notify the listener
    jumpTo(defaultPage);
    if (onLoadCompleteListener != null) {
        onLoadCompleteListener.loadComplete(documentPageCount);
    }
}
 
Example #14
Source File: PDFViewActivity.java    From AndroidPdfViewerV2 with Apache License 2.0 5 votes vote down vote up
public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}
 
Example #15
Source File: PDFViewActivity.java    From AndroidPdfViewerV2 with Apache License 2.0 5 votes vote down vote up
@Override
public void loadComplete(int nbPages) {
    PdfDocument.Meta meta = pdfView.getDocumentMeta();
    Log.e(TAG, "title = " + meta.getTitle());
    Log.e(TAG, "author = " + meta.getAuthor());
    Log.e(TAG, "subject = " + meta.getSubject());
    Log.e(TAG, "keywords = " + meta.getKeywords());
    Log.e(TAG, "creator = " + meta.getCreator());
    Log.e(TAG, "producer = " + meta.getProducer());
    Log.e(TAG, "creationDate = " + meta.getCreationDate());
    Log.e(TAG, "modDate = " + meta.getModDate());

    printBookmarksTree(pdfView.getTableOfContents(), "-");

}
 
Example #16
Source File: PDFView.java    From AndroidPdfViewerV1 with Apache License 2.0 4 votes vote down vote up
public PdfDocument.Meta getDocumentMeta() {
    if (pdfDocument == null) {
        return null;
    }
    return pdfiumCore.getDocumentMeta(pdfDocument);
}
 
Example #17
Source File: PDFView.java    From AndroidPdfViewerV2 with Apache License 2.0 4 votes vote down vote up
public PdfDocument.Meta getDocumentMeta() {
    if (pdfDocument == null) {
        return null;
    }
    return pdfiumCore.getDocumentMeta(pdfDocument);
}
 
Example #18
Source File: PDFView.java    From AndroidPdfViewerV2 with Apache License 2.0 4 votes vote down vote up
public List<PdfDocument.Bookmark> getTableOfContents() {
    if (pdfDocument == null) {
        return new ArrayList<>();
    }
    return pdfiumCore.getTableOfContents(pdfDocument);
}
 
Example #19
Source File: FileSource.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    return core.newDocument(pfd, password);
}
 
Example #20
Source File: ByteArraySource.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    return core.newDocument(data, password);
}
 
Example #21
Source File: AssetSource.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    File f = FileUtils.fileFromAsset(context, assetName);
    ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
    return core.newDocument(pfd, password);
}
 
Example #22
Source File: InputStreamSource.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    return core.newDocument(Util.toByteArray(inputStream), password);
}
 
Example #23
Source File: UriSource.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
    return core.newDocument(pfd, password);
}
 
Example #24
Source File: RenderingHandler.java    From AndroidPdfViewerV2 with Apache License 2.0 4 votes vote down vote up
RenderingHandler(Looper looper, PDFView pdfView, PdfiumCore pdfiumCore, PdfDocument pdfDocument) {
    super(looper);
    this.pdfView = pdfView;
    this.pdfiumCore = pdfiumCore;
    this.pdfDocument = pdfDocument;
}
 
Example #25
Source File: LinkTapEvent.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
public PdfDocument.Link getLink() {
    return link;
}
 
Example #26
Source File: UriSource.java    From AndroidPdfViewerV2 with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
    return core.newDocument(pfd, password);
}
 
Example #27
Source File: PdfFile.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
public List<PdfDocument.Link> getPageLinks(int pageIndex) {
    int docPage = documentPage(pageIndex);
    return pdfiumCore.getPageLinks(pdfDocument, docPage);
}
 
Example #28
Source File: PdfFile.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
public List<PdfDocument.Bookmark> getBookmarks() {
    if (pdfDocument == null) {
        return new ArrayList<>();
    }
    return pdfiumCore.getTableOfContents(pdfDocument);
}
 
Example #29
Source File: PdfFile.java    From AndroidPdfViewer with Apache License 2.0 4 votes vote down vote up
public PdfDocument.Meta getMetaData() {
    if (pdfDocument == null) {
        return null;
    }
    return pdfiumCore.getDocumentMeta(pdfDocument);
}
 
Example #30
Source File: InputStreamSource.java    From AndroidPdfViewerV2 with Apache License 2.0 4 votes vote down vote up
@Override
public PdfDocument createDocument(Context context, PdfiumCore core, String password) throws IOException {
    return core.newDocument(Util.toByteArray(inputStream), password);
}