Java 类android.graphics.pdf.PdfDocument 实例源码

项目:cards-app    文件:PrintAdapter.java   
private void drawPage(PdfDocument.Page page, int pageNumber){
    Canvas canvas = page.getCanvas();

    ImageManager manager=new ImageManager(context);

    Bitmap image;
    if (pageNumber%2==0)
        image=manager.decodeBitmapFromFile(manager.createFileNameFromUrl(frontImageName));
    else 
        image=manager.decodeBitmapFromFile(manager.createFileNameFromUrl(backImageName));

    Matrix matrix=new Matrix();
    matrix.setScale(500, 500);

    canvas.drawBitmap(image,0,0,new Paint());
}
项目:IziOzi    文件:PdfCreatorTask.java   
@TargetApi(Build.VERSION_CODES.KITKAT)
private PdfDocument.Page initNewPdfPage() {
    PdfDocument.PageInfo pageInfo;

    if (orientation.equals(IOHelper.Orientation.HORIZONTAL)) {
        pageInfo = new PdfDocument.PageInfo.Builder(PDF_HEIGHT, PDF_WIDTH, ++pageNum).create();
    }
    else {
        pageInfo = new PdfDocument.PageInfo.Builder(PDF_WIDTH, PDF_HEIGHT, ++pageNum).create();
    }

    return document.startPage(pageInfo);
}
项目:slide    文件:StorageController.java   
@Override
protected Boolean doInBackground(Void... params) {
    PdfDocument document = new PdfDocument();
    ParcelFileDescriptor pfd = null;
    try {
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(640, 640 * 9 / 16, 1).create();

        for (Slide slide : store.getState().slides()) {
            PdfDocument.Page page = document.startPage(pageInfo);
            page.getCanvas().drawColor(Style.COLOR_SCHEMES[store.getState().colorScheme()][1]);
            slide.render(mContext,
                    page.getCanvas(),
                    page.getCanvas().getWidth(), page.getCanvas().getHeight(),
                    Style.SLIDE_FONT,
                    Style.COLOR_SCHEMES[App.getState().colorScheme()][0],
                    Style.COLOR_SCHEMES[App.getState().colorScheme()][1],
                    true);
            document.finishPage(page);
        }

        pfd = mContext.getContentResolver().openFileDescriptor(uri, "w");
        if (pfd != null) {
            FileOutputStream fos = new FileOutputStream(pfd.getFileDescriptor());
            document.writeTo(fos);
            return true;
        } else {
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        document.close();
        if (pfd != null) {
            try { pfd.close(); } catch (IOException ignored) {}
        }
    }
}
项目:IziOzi    文件:PdfCreatorTask.java   
@TargetApi(Build.VERSION_CODES.KITKAT)
public PdfCreatorTask(Context context, IOHelper.Orientation orientation) {
    this.context = context;
    pageNum = 0;
    this.orientation = orientation;
    document = new PdfDocument();
}
项目:IziOzi    文件:PdfCreatorTask.java   
private void dfs(IOBoard board) {
    // first check if they are all empty pictograms on a board and short circuit
    boolean foundImg = false;
    for (int i = 0; i < board.getRows(); i++) {
        if (foundImg) break;

        for (int j = 0; j < board.getCols(); j++) {
            int index = i * board.getCols() + j;

            IOSpeakableImageButton img = board.getButtons().get(index);
            if (!img.getmImageFile().equals("")) {
                foundImg = true;
                break;
            }
        }
    }

    if (foundImg) {
        PdfDocument.Page page = initNewPdfPage();
        createPdfPage(board, page);
    }

    // now dfs for every child tree board
    for (int i=0; i < board.getButtons().size(); i++) {
        IOSpeakableImageButton btn = board.getButtons().get(i);
        if (btn.getIsMatrioska() && btn.getLevel() != null) {
            // it's a nested board
            IOLevel level = btn.getLevel();

            for (int j = 0; j < level.getInnerBoards().size(); j++) {
                IOBoard innerBoard = level.getBoardAtIndex(j);
                dfs(innerBoard);
            }
        }
    }
}
项目:bitesize-kitkat    文件:PrintShopPrintDocumentAdapter.java   
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, final WriteResultCallback callback) {

    // Register a cancellation listener
    cancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
        @Override
        public void onCancel() {
            // If cancelled then ensure that the PDF doc gets thrown away
            pdfDocument.close();
            pdfDocument = null;
            // And callback
            callback.onWriteCancelled();
        }
    });

    // Iterate through the pages
    for (int currentPageNumber = 0; currentPageNumber < pageCount; currentPageNumber++) {
        // Has this page been requested?
        if(!pageRangesContainPage(currentPageNumber, pages)) {
            // Skip this page
            continue;
        }

        // Start the current page
        PdfDocument.Page page = pdfDocument.startPage(currentPageNumber);

        // Get the canvas for this page
        Canvas canvas = page.getCanvas();

        // Draw on the page
        drawPage(currentPageNumber, canvas);

        // Finish the page
        pdfDocument.finishPage(page);
    }

    // Attempt to send the completed doc out
    try {
        pdfDocument.writeTo(new FileOutputStream(destination.getFileDescriptor()));
    } catch (IOException e) {
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        pdfDocument.close();
        pdfDocument = null;
    }

    // The print is complete
    callback.onWriteFinished(pages);
}
项目:android-print-plugin-cups    文件:SharePrinterActivity.java   
@Override
public void onWrite(final PageRange[] pageRanges,
                    final ParcelFileDescriptor destination,
                    final CancellationSignal cancellationSignal,
                    final WriteResultCallback callback)
{
    if (pageRanges.length == 0 || !(
        pageRanges[0].getStart() <= 0 && pageRanges[0].getEnd() >= 0 ||
        pageRanges[0] == PageRange.ALL_PAGES))
    {
        Log.d(TAG, "Saving PDF failed - no valid page range");
        return;
    }

    if (cancellationSignal.isCanceled())
    {
        callback.onWriteCancelled();
        pdf.close();
        pdf = null;
        return;
    }

    PdfDocument.Page page = pdf.startPage(0);
    drawPage(page);
    pdf.finishPage(page);

    Log.d(TAG, "Saving PDF");
    try
    {
        pdf.writeTo(new FileOutputStream(destination.getFileDescriptor()));
        Log.w(TAG, "Saving PDF succeeded");
    } catch (IOException e) {
        Log.w(TAG, "Saving PDF failed: " + e.toString());
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        pdf.close();
        pdf = null;
    }
    callback.onWriteFinished(new PageRange[]{ new PageRange(0, 0) });
}
项目:android-print-plugin-cups    文件:SharePrinterActivity.java   
private void drawPage(PdfDocument.Page page)
{
    Log.d(TAG, "Drawing PDF page");
    Canvas canvas = page.getCanvas();
    int w = canvas.getWidth();
    int h = canvas.getHeight();
    int x = w / 2;
    int y = 0;
    int size = h / 50;
    Log.d(TAG, "w " + w + " h " + h + " x " + x + " y " + y + " size " + size + " bounds " + canvas.getClipBounds().toString());
    //layout.draw(canvas);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTextSize(size * 1.5f);
    paint.setTextAlign(Paint.Align.CENTER);

    y += (paint.descent() - paint.ascent()) * 2.5;
    canvas.drawText(getResources().getString(R.string.add_printer_android, name), x, y, paint);
    paint.setTextSize(size / 1.3f);
    y += (paint.descent() - paint.ascent()) * 1.5;
    canvas.drawText(getResources().getString(R.string.add_printer_android_ver), x, y, paint);
    y += (paint.descent() - paint.ascent()) * 1.2;
    canvas.drawText(getResources().getString(R.string.install_barcode_scanner, name), x, y, paint);
    paint.setTextSize(size);
    y += (paint.descent() - paint.ascent()) * 1.5;
    canvas.drawText(getResources().getString(R.string.install_printer_plugin, getResources().getString(R.string.app_name)), x, y, paint);
    y += (paint.descent() - paint.ascent()) * 0.2;

    int qrSize = size * 13;
    paint.setFilterBitmap(false);
    canvas.drawBitmap(myAppAddr, null, new Rect(x - qrSize / 2, y, x + qrSize / 2, y + qrSize), paint);
    y += qrSize;

    y += (paint.descent() - paint.ascent()) * 0.7;
    canvas.drawText(getResources().getString(R.string.install_printer_plugin_enable, getResources().getString(R.string.app_name)), x, y, paint);
    y += (paint.descent() - paint.ascent()) * 1;
    canvas.drawText(getResources().getString(R.string.scan_qr, name), x, y, paint);
    paint.setTextSize(size / 1.3f);
    y += (paint.descent() - paint.ascent()) * 1;
    canvas.drawText(getResources().getString(R.string.scan_qr_open_browser), x, y, paint);
    paint.setTextSize(size);
    y += (paint.descent() - paint.ascent()) * 0.2;

    qrSize = size * 20;
    canvas.drawBitmap(qr, null, new Rect(x - qrSize / 2, y, x + qrSize / 2, y + qrSize), paint);
    y += qrSize;

    y += (paint.descent() - paint.ascent()) * 0.8;
    for (String line: notes.split("\n"))
    {
        canvas.drawText(line, x, y, paint);
        y += (paint.descent() - paint.ascent()) * 1;
    }
}
项目:cards-app    文件:PrintAdapter.java   
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
        CancellationSignal cancellationSignal, WriteResultCallback callback) {
    // TODO Auto-generated method stub

     // Iterate over each page of the document,
    // check if it's in the output range.
    for (int i = 0; i < 2; i++) {
        // Check to see if this page is in the output range.
            // If so, add it to writtenPagesArray. writtenPagesArray.size()
            // is used to compute the next output page index.
            PdfDocument.Page page = document.startPage(i);

            // check for cancellation
            if (cancellationSignal.isCanceled()) {
                callback.onWriteCancelled();
                document.close();
                document = null;
                return;
            }

            // Draw page content for printing
            drawPage(page,i);

            // Rendering is complete, so page can be finalized.
            document.finishPage(page);
    }

    // Write PDF document to file
    try {
        document.writeTo(new FileOutputStream(
                destination.getFileDescriptor()));
    } catch (IOException e) {
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        document.close();
        document = null;
    }
    PageRange[] writtenPages = {PageRange.ALL_PAGES};
    // Signal the print framework the document is complete
    callback.onWriteFinished(writtenPages);
}
项目:IziOzi    文件:PdfCreatorTask.java   
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createPdfPage(IOBoard board, PdfDocument.Page page) {
    int pageWidth = page.getInfo().getPageWidth();
    int pageHeight = page.getInfo().getPageHeight();

    int sizeWidth = pageWidth / board.getCols();
    int sizeHeight = pageHeight / board.getRows();

    int picSize = sizeWidth < sizeHeight ? sizeWidth : sizeHeight;

    int marginLeft = (pageWidth - (picSize * board.getCols())) / (board.getCols() + 1);
    int marginTop = (pageHeight - (picSize * board.getRows())) / (board.getRows() + 1);

    for (int i = 0; i < board.getRows(); i++) {
        int top = (marginTop * (i+1)) + (picSize * i);

        for (int j=0; j < board.getCols(); j++) {
            int index = i * board.getCols() + j;
            IOSpeakableImageButton btn = board.getButtons().get(index);

            ImageSize size = new ImageSize(picSize, picSize);
            Bitmap b = null;

            if (!btn.getmImageFile().equals("")) {
                DisplayImageOptions imgOpts = new DisplayImageOptions.Builder()
                        .imageScaleType(ImageScaleType.EXACTLY)
                        .build();

                b = imageLoader.loadImageSync("file://" + btn.getmImageFile(), size, imgOpts);
            }

            if (b != null) {
                int left = (marginLeft * (j+1)) + (picSize * j);

                page.getCanvas().drawBitmap(b, left, top, null);
            }
        }
    }

    document.finishPage(page);
}