在Xamarin Android中打印活动的内容?

时间:2017-06-12 23:05:01

标签: android xamarin printing xamarin.android

我想在Xamarin.Droid项目中打印Activity的布局内容(通过打印我的意思是将屏幕上显示的内容以无线方式发送到打印机以进行打印)。

我尝试按照Xamarin博客文章(https://blog.xamarin.com/native-printing-with-android/)上的步骤进行操作,但代码似乎不完整/不清楚。有没有人有完整的解决方案来实现这一目标?

1 个答案:

答案 0 :(得分:1)

  

我尝试按照Xamarin博客文章(https://blog.xamarin.com/native-printing-with-android/)上的步骤进行操作,但代码似乎不完整/不清楚。有没有人有完整的解决方案来实现这一目标?

根据您发布的博客,您需要创建自己的适配器来实现PrintDocumentAdapter,如下所示:

public class MyPrintDocumentAdapter : PrintDocumentAdapter
{

    View view;
    Context context;
    PrintedPdfDocument document;
    float scale;

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

    public override void OnLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
    {
        document = new PrintedPdfDocument(context, newAttributes);

        CalculateScale(newAttributes);

        //set the printed PDF attributes
        var printInfo = new PrintDocumentInfo
            .Builder("MyPrint.pdf")
            .SetContentType(PrintContentType.Document)
            .SetPageCount(1)
            .Build();

        callback.OnLayoutFinished(printInfo, true);
    }

    public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
    {
        PrintedPdfDocument.Page page = document.StartPage(0);

        page.Canvas.Scale(scale, scale);

        view.Draw(page.Canvas);

        document.FinishPage(page);

        WritePrintedPdfDoc(destination);

        document.Close();

        document.Dispose();

        callback.OnWriteFinished(pages);
    }

    void CalculateScale(PrintAttributes newAttributes)
    {
        int dpi = Math.Max(newAttributes.GetResolution().HorizontalDpi, newAttributes.GetResolution().VerticalDpi);

        int leftMargin = (int)(dpi * (float)newAttributes.MinMargins.LeftMils / 1000);
        int rightMargin = (int)(dpi * (float)newAttributes.MinMargins.RightMils / 1000);
        int topMargin = (int)(dpi * (float)newAttributes.MinMargins.TopMils / 1000);
        int bottomMargin = (int)(dpi * (float)newAttributes.MinMargins.BottomMils / 1000);

        int w = (int)(dpi * (float)newAttributes.GetMediaSize().WidthMils / 1000) - leftMargin - rightMargin;
        int h = (int)(dpi * (float)newAttributes.GetMediaSize().HeightMils / 1000) - topMargin - bottomMargin;

        scale = Math.Min((float)document.PageContentRect.Width() / w, (float)document.PageContentRect.Height() / h);
    }

    void WritePrintedPdfDoc(ParcelFileDescriptor destination)
    {
        var javaStream = new Java.IO.FileOutputStream(destination.FileDescriptor);
        var osi = new OutputStreamInvoker(javaStream);
        using (var mem = new MemoryStream())
        {
            document.WriteTo(mem);
            var bytes = mem.ToArray();
            osi.Write(bytes, 0, bytes.Length);
        }
    }

}

并在您的某处使用此MyPrintDocumentAdapter打印活动:

var printManager = (PrintManager)GetSystemService(Context.PrintService);
var content = FindViewById<LinearLayout>(Resource.Id.mRoot);
var printAdapter = new MyPrintDocumentAdapter(this, content);
var printJob = printManager.Print("MyPrintJob", printAdapter, null);

有关详细教程,请参阅Print Custom Document

这是Reference of PrintDocumentAdapter