Печать всей веб-страницы - PullRequest
       7

Печать всей веб-страницы

0 голосов
/ 29 сентября 2018

Я создаю базовое веб-приложение для Android, и мне нужна функция печати.Я попробовал это:

var printMgr = (PrintManager)activity.GetSystemService(Context.PrintService);

if (((int)Android.OS.Build.VERSION.SdkInt) >= 21)
    printMgr.Print("my page", webview.CreatePrintDocumentAdapter("my page"), null);
else
    printMgr.Print("my page", webview.CreatePrintDocumentAdapter(), null);

Однако он печатает только видимую часть страницы.

1 Ответ

0 голосов
/ 16 октября 2018

Если вы хотите растровое изображение, используйте следующий класс, чтобы получить один

public class Screenshot {

    public static Bitmap get(Context context,WebView view){
        float width=getWindowWidth(context);
        float height=view.getContentHeight() * getDensity(context);
        return capture(view,width,height);
    }
    private static Bitmap capture(View view, float width, float height) {
        if (!view.isDrawingCacheEnabled()) {
            view.setDrawingCacheEnabled(true);
        }
        view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(Color.WHITE);
        Canvas canvas = new Canvas(bitmap);
        int left = view.getLeft();
        int top = view.getTop();
        int status = canvas.save();
        canvas.translate(-left, -top);
        float scale = width / view.getWidth();
        canvas.scale(scale, scale, left, top);
        view.draw(canvas);
        canvas.restoreToCount(status);
        Paint alphaPaint = new Paint();
        alphaPaint.setColor(Color.TRANSPARENT);
        canvas.drawRect(0f, 0f, 1f, height, alphaPaint);
        canvas.drawRect(width - 1f, 0f, width, height, alphaPaint);
        canvas.drawRect(0f, 0f, width, 1f, alphaPaint);
        canvas.drawRect(0f, height - 1f, width, height, alphaPaint);
        canvas.setBitmap(null);
        return bitmap;
    }
    private static int getWindowWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }
    private static float getDensity(Context context) {
        return context.getResources().getDisplayMetrics().density;
    }
}

Если вы хотите напечатать, попробуйте заменить

    printMgr.Print("my page", webview.CreatePrintDocumentAdapter(), null);

на

printMgr.Print("my page", webview.CreatePrintDocumentAdapter(), new PrintAttributes.Builder().build()));

thisработал на меня

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...