android - 使用自定义PrintDocumentAdapter锁定打印方向

时间:2016-01-12 12:09:51

标签: printing custom-adapter printdocument

我想用自定义的PrintDocumentAdapter打印tableLayout。问题是当弹出Android打印预览窗口时,您可以在水平和横向打印模式之间进行选择。我想将打印方向锁定为仅水平。有没有可能这样做?

这是我的代码:

@TargetApi(Build.VERSION_CODES.KITKAT)
public static class MyPrintDocumentAdapter extends PrintDocumentAdapter {
    Context context;
    private int pageHeight;
    private int pageWidth;
    public PdfDocument myPdfDocument;
    public int totalpages = 1;
    private View view;
    private Bitmap bm;

    public MyPrintDocumentAdapter(Context context, View view) {
        this.context = context;
        this.view = view;
    }

    @Override
    public void onLayout(PrintAttributes oldAttributes,
                         PrintAttributes newAttributes,
                         CancellationSignal cancellationSignal,
                         LayoutResultCallback callback,
                         Bundle metadata) {
        myPdfDocument = new PrintedPdfDocument(context, newAttributes);
        pageHeight =
                    newAttributes.getMediaSize().getHeightMils() / 500 * 72;
        pageWidth =
                    newAttributes.getMediaSize().getWidthMils() / 500 * 72;

        if (cancellationSignal.isCanceled()) {
            callback.onLayoutCancelled();
            return;
        }

        if (totalpages > 0) {
            PrintDocumentInfo.Builder builder = new PrintDocumentInfo
                    .Builder("print_output.pdf")
                    .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                    .setPageCount(totalpages);

            PrintDocumentInfo info = builder.build();
            callback.onLayoutFinished(info, true);
        } else {
            callback.onLayoutFailed("Page count is zero.");
        }

    }

    @Override
    public void onWrite(final PageRange[] pageRanges,
                        final ParcelFileDescriptor destination,
                        final CancellationSignal cancellationSignal,
                        final WriteResultCallback callback) {
        for (int i = 0; i < totalpages; i++) {
            if (pageInRange(pageRanges, i)) {
                PdfDocument.PageInfo newPage = new PdfDocument.PageInfo.Builder(pageWidth,
                        pageHeight, i).create();

                PdfDocument.Page page =
                        myPdfDocument.startPage(newPage);

                if (cancellationSignal.isCanceled()) {
                    callback.onWriteCancelled();
                    myPdfDocument.close();
                    myPdfDocument = null;
                    return;
                }
                drawPage(page, i);
                myPdfDocument.finishPage(page);
            }
        }

        try {
            myPdfDocument.writeTo(new FileOutputStream(
                    destination.getFileDescriptor()));
        } catch (IOException e) {
            callback.onWriteFailed(e.toString());
            return;
        } finally {
            myPdfDocument.close();
            myPdfDocument = null;
            System.err.println("FINISHED!!");
        }

        callback.onWriteFinished(pageRanges);

    }

    private boolean pageInRange(PageRange[] pageRanges, int page) {
        for (int i = 0; i < pageRanges.length; i++) {
            if ((page >= pageRanges[i].getStart()) &&
                    (page <= pageRanges[i].getEnd()))
                return true;
        }
        return false;
    }

    private void drawPage(PdfDocument.Page page,
                          int pagenumber) {
        Canvas canvas = page.getCanvas();

        pagenumber++; // Make sure page numbers start at 1

        int titleBaseLine = 72;
        int leftMargin = 38;
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(40);
        PdfDocument.PageInfo pageInfo = page.getInfo();

        Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.drawable.testprint);

        canvas.drawBitmap(logo, (pageInfo.getPageWidth() / 2) - (logo.getWidth() / 2), 0, paint);


        bm = createBitmapFromLayoutWithText(view.getContext());
        double toScale = bm.getWidth() / pageWidth;

        canvas.drawBitmap(Bitmap.createScaledBitmap(bm, (int)Math.round(bm.getWidth()/toScale) - 40, (int)Math.round(bm.getHeight()/toScale), false), 10, logo.getHeight() + 20, paint);

        paint.setTextSize(18);
        canvas.drawText("Seite " + pagenumber, (pageInfo.getPageWidth() / 2) - ("Seite " + pagenumber).length(), pageInfo.getPageHeight() - 10, paint);
    }

}

0 个答案:

没有答案