FileDocCategorySizeDatePackage
PdfEditor.javaAPI DocAndroid 5.1 API10579Thu Mar 12 22:22:30 GMT 2015android.graphics.pdf

PdfEditor

public final class PdfEditor extends Object
Class for editing PDF files.
hide

Fields Summary
private final dalvik.system.CloseGuard
mCloseGuard
private final long
mNativeDocument
private int
mPageCount
private android.os.ParcelFileDescriptor
mInput
Constructors Summary
public PdfEditor(android.os.ParcelFileDescriptor input)
Creates a new instance.

Note: The provided file descriptor must be seekable, i.e. its data being randomly accessed, e.g. pointing to a file. After finishing with this class you must call {@link #close()}.

Note: This class takes ownership of the passed in file descriptor and is responsible for closing it when the editor is closed.

param
input Seekable file descriptor to read from.
throws
java.io.IOException If an error occurs while reading the file.
throws
java.lang.SecurityException If the file requires a password or the security scheme is not supported.
see
#close()


                                                                                                               
          
        if (input == null) {
            throw new NullPointerException("input cannot be null");
        }

        final long size;
        try {
            Libcore.os.lseek(input.getFileDescriptor(), 0, OsConstants.SEEK_SET);
            size = Libcore.os.fstat(input.getFileDescriptor()).st_size;
        } catch (ErrnoException ee) {
            throw new IllegalArgumentException("file descriptor not seekable");
        }

        mInput = input;
        mNativeDocument = nativeOpen(mInput.getFd(), size);
        mPageCount = nativeGetPageCount(mNativeDocument);
        mCloseGuard.open("close");
    
Methods Summary
public voidclose()
Closes this editor. You should not use this instance after this method is called.

        throwIfClosed();
        doClose();
    
private voiddoClose()

        nativeClose(mNativeDocument);
        IoUtils.closeQuietly(mInput);
        mInput = null;
        mCloseGuard.close();
    
protected voidfinalize()

        try {
            mCloseGuard.warnIfOpen();
            if (mInput != null) {
                doClose();
            }
        } finally {
            super.finalize();
        }
    
public intgetPageCount()
Gets the number of pages in the document.

return
The page count.

        throwIfClosed();
        return mPageCount;
    
public booleangetPageCropBox(int pageIndex, android.graphics.Rect outCropBox)
Gets the crop box of a given page in mils (1/72").

param
pageIndex The page index.
param
outCropBox The crop box output.

        throwIfClosed();
        throwIfOutCropBoxNull(outCropBox);
        throwIfPageNotInDocument(pageIndex);
        return nativeGetPageCropBox(mNativeDocument, pageIndex, outCropBox);
    
public booleangetPageMediaBox(int pageIndex, android.graphics.Rect outMediaBox)
Gets the media box of a given page in mils (1/72").

param
pageIndex The page index.
param
outMediaBox The media box output.

        throwIfClosed();
        throwIfOutMediaBoxNull(outMediaBox);
        throwIfPageNotInDocument(pageIndex);
        return nativeGetPageMediaBox(mNativeDocument, pageIndex, outMediaBox);
    
public voidgetPageSize(int pageIndex, android.graphics.Point outSize)
Gets the size of a given page in mils (1/72").

param
pageIndex The page index.
param
outSize The size output.

        throwIfClosed();
        throwIfOutSizeNull(outSize);
        throwIfPageNotInDocument(pageIndex);
        nativeGetPageSize(mNativeDocument, pageIndex, outSize);
    
private static native voidnativeClose(long documentPtr)

private static native intnativeGetPageCount(long documentPtr)

private static native booleannativeGetPageCropBox(long documentPtr, int pageIndex, android.graphics.Rect outMediaBox)

private static native booleannativeGetPageMediaBox(long documentPtr, int pageIndex, android.graphics.Rect outMediaBox)

private static native voidnativeGetPageSize(long documentPtr, int pageIndex, android.graphics.Point outSize)

private static native longnativeOpen(int fd, long size)

private static native intnativeRemovePage(long documentPtr, int pageIndex)

private static native booleannativeScaleForPrinting(long documentPtr)

private static native voidnativeSetPageCropBox(long documentPtr, int pageIndex, android.graphics.Rect mediaBox)

private static native voidnativeSetPageMediaBox(long documentPtr, int pageIndex, android.graphics.Rect mediaBox)

private static native voidnativeSetTransformAndClip(long documentPtr, int pageIndex, long transformPtr, int clipLeft, int clipTop, int clipRight, int clipBottom)

private static native voidnativeWrite(long documentPtr, int fd)

public voidremovePage(int pageIndex)
Removes the page with a given index.

param
pageIndex The page to remove.

        throwIfClosed();
        throwIfPageNotInDocument(pageIndex);
        mPageCount = nativeRemovePage(mNativeDocument, pageIndex);
    
public voidsetPageCropBox(int pageIndex, android.graphics.Rect cropBox)
Sets the crop box of a given page in mils (1/72").

param
pageIndex The page index.
param
cropBox The crop box.

        throwIfClosed();
        throwIfCropBoxNull(cropBox);
        throwIfPageNotInDocument(pageIndex);
        nativeSetPageCropBox(mNativeDocument, pageIndex, cropBox);
    
public voidsetPageMediaBox(int pageIndex, android.graphics.Rect mediaBox)
Sets the media box of a given page in mils (1/72").

param
pageIndex The page index.
param
mediaBox The media box.

        throwIfClosed();
        throwIfMediaBoxNull(mediaBox);
        throwIfPageNotInDocument(pageIndex);
        nativeSetPageMediaBox(mNativeDocument, pageIndex, mediaBox);
    
public voidsetTransformAndClip(int pageIndex, android.graphics.Matrix transform, android.graphics.Rect clip)
Sets a transformation and clip for a given page. The transformation matrix if non-null must be affine as per {@link android.graphics.Matrix#isAffine()}. If the clip is null, then no clipping is performed.

param
pageIndex The page whose transform to set.
param
transform The transformation to apply.
param
clip The clip to apply.

        throwIfClosed();
        throwIfPageNotInDocument(pageIndex);
        throwIfNotNullAndNotAfine(transform);
        if (transform == null) {
            transform = Matrix.IDENTITY_MATRIX;
        }
        if (clip == null) {
            Point size = new Point();
            getPageSize(pageIndex, size);
            nativeSetTransformAndClip(mNativeDocument, pageIndex, transform.native_instance,
                    0, 0, size.x, size.y);
        } else {
            nativeSetTransformAndClip(mNativeDocument, pageIndex, transform.native_instance,
                    clip.left, clip.top, clip.right, clip.bottom);
        }
    
public booleanshouldScaleForPrinting()
Gets whether the document prefers to be scaled for printing.

return
Whether to scale the document.

        throwIfClosed();
        return nativeScaleForPrinting(mNativeDocument);
    
private voidthrowIfClosed()

        if (mInput == null) {
            throw new IllegalStateException("Already closed");
        }
    
private voidthrowIfCropBoxNull(android.graphics.Rect cropBox)

        if (cropBox == null) {
            throw new NullPointerException("cropBox cannot be null");
        }
    
private voidthrowIfMediaBoxNull(android.graphics.Rect mediaBox)

        if (mediaBox == null) {
            throw new NullPointerException("mediaBox cannot be null");
        }
    
private voidthrowIfNotNullAndNotAfine(android.graphics.Matrix matrix)

        if (matrix != null && !matrix.isAffine()) {
            throw new IllegalStateException("Matrix must be afine");
        }
    
private voidthrowIfOutCropBoxNull(android.graphics.Rect outCropBox)

        if (outCropBox == null) {
            throw new NullPointerException("outCropBox cannot be null");
        }
    
private voidthrowIfOutMediaBoxNull(android.graphics.Rect outMediaBox)

        if (outMediaBox == null) {
            throw new NullPointerException("outMediaBox cannot be null");
        }
    
private voidthrowIfOutSizeNull(android.graphics.Point outSize)

        if (outSize == null) {
            throw new NullPointerException("outSize cannot be null");
        }
    
private voidthrowIfPageNotInDocument(int pageIndex)

        if (pageIndex < 0 || pageIndex >= mPageCount) {
            throw new IllegalArgumentException("Invalid page index");
        }
    
public voidwrite(android.os.ParcelFileDescriptor output)
Writes the PDF file to the provided destination.

Note: This method takes ownership of the passed in file descriptor and is responsible for closing it when writing completes.

param
output The destination.

        try {
            throwIfClosed();
            nativeWrite(mNativeDocument, output.getFd());
        } finally {
            IoUtils.closeQuietly(output);
        }