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

Printing android-使用自定义PrintDocumentAdapter锁定打印方向,printing,custom-adapter,printdocument,Printing,Custom Adapter,Printdocument,我想用我的定制PrintDocumentAdapter打印一个表格布局。问题是,当android打印预览窗口弹出时,您可以在水平和横向打印模式之间进行选择。我想将打印方向锁定为水平方向。有没有可能这样做 这是我的密码: @TargetApi(Build.VERSION_CODES.KITKAT) public static class MyPrintDocumentAdapter extends PrintDocumentAdapter { Context context; pr

我想用我的定制PrintDocumentAdapter打印一个表格布局。问题是,当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);
    }

}
@TargetApi(Build.VERSION\u CODES.KITKAT)
公共静态类MyPrintDocumentAdapter扩展了PrintDocumentAdapter{
语境;
私人网页高度;
私有int页面宽度;
公共PDF文档MyPDF文档;
公共int totalpages=1;
私人视野;
私有位图bm;
公共MyPrintDocumentAdapter(上下文、视图){
this.context=上下文;
this.view=视图;
}
@凌驾
public void Only布局(打印属性oldAttributes,
打印属性newAttributes,
取消信号取消信号,
LayoutResultCallback回调,
捆绑包(元数据){
myPdfDocument=新打印的PDFDocument(上下文,新属性);
页高=
newAttributes.getMediaSize().getHeightMils()/500*72;
页面宽度=
newAttributes.getMediaSize().getWidthMils()/500*72;
if(cancellationSignal.isCancelled()){
callback.onLayoutCancelled();
返回;
}
如果(总页数>0){
PrintDocumentInfo.Builder=新的PrintDocumentInfo
.Builder(“print_output.pdf”)
.setContentType(PrintDocumentInfo.CONTENT\u TYPE\u文档)
.setPageCount(总页数);
PrintDocumentInfo=builder.build();
callback.onlayoufinished(info,true);
}否则{
callback.onlayoufailed(“页面计数为零”);
}
}
@凌驾
写入时公共无效(最终页面范围[]页面范围,
最终包裹描述符目的地,
最终取消信号取消信号,
最终写入结果(回调){
对于(int i=0;i=pageRanges[i].getStart())&&
(第页