2017-08-03 165 views
0

我正在做一個項目,我只需要一步打印,只需點擊打印按鈕即可直接打印文檔。我找到了一個測試項目,當我點擊打印按鈕時,它會打開第二張圖片中顯示的頁面(我不希望顯示這個頁面)。我想如果可以將設置保存到一個文件,當我做一個測試打印,並在以後使用這些設置(在圖像說打印pdf等忽略,目前我沒有連接打印機)如何在android上直接打印?

https://i.stack.imgur.com/hcZDW.png

https://i.stack.imgur.com/yNTSA.png

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Android.Print; 
using Android.Graphics; 
using Android.Print.Pdf; 
using System.IO; 

namespace PrintIMPORTteste 
{ 
    [Activity (Label = "KitKatPrintDemo", Theme = "@android:style/Theme.Holo.Light")] 
    public class PrintActivity : Activity 
    { 
     protected override void OnCreate (Bundle bundle) 
     { 
      base.OnCreate (bundle); 
      SetContentView (Resource.Layout.Print); 
      var txtview = FindViewById<TextView>(Resource.Id.textview1); 
      txtview.Text = "Hello"; 
      var button = FindViewById<Button> (Resource.Id.button1); 
      button.Click += (object sender, EventArgs e) => { 
       var printManager = (PrintManager)GetSystemService (Context.PrintService); 
       var content = FindViewById<LinearLayout> (Resource.Id.linearLayout1); 
       var printAdapter = new GenericPrintAdapter (this, content); 
       printManager.Print ("MyPrintJob", printAdapter, null); 
      }; 
     } 
    } 

    public class GenericPrintAdapter : PrintDocumentAdapter 
    { 
     View view; 
     Context context; 
     PrintedPdfDocument document; 
     float scale; 

     public GenericPrintAdapter (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); 

      var printInfo = new PrintDocumentInfo 
       .Builder ("MyPrint.pdf") 
       .SetContentType (PrintContentType.Document) 
       .SetPageCount (1) 
       .Build(); 

      callback.OnLayoutFinished (printInfo, true); 
     } 

     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); 
     } 

     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 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); 
      } 
     } 
    } 
} 
+0

https://stackoverflow.com/q/39550539/4606266 – ziLk

回答

0

我發現了一個試驗項目,我只要一點擊打印按鈕,它會打開第二圖像中顯示的頁面(我不希望這是所示) 。

無法隱藏打印對話框,因爲它是android框架的一部分。

參考的PrintManager.print method文檔:

... 注意:調用此方法會帶來打印對話框,並且系統將連接到提供PrintDocumentAdapter ....

且還參考了源碼PrintManager.java

public PrintJob print(String printJobName, PrintDocumentAdapter documentAdapter, 
     PrintAttributes attributes) { 
    ... 
    PrintDocumentAdapterDelegate delegate = new PrintDocumentAdapterDelegate(
      (Activity) mContext, documentAdapter); 
    try { 
     Bundle result = mService.print(printJobName, delegate, 
       attributes, mContext.getPackageName(), mAppId, mUserId); 
     if (result != null) { 
      PrintJobInfo printJob = result.getParcelable(EXTRA_PRINT_JOB); 
      //call the dialog 
      IntentSender intent = result.getParcelable(EXTRA_PRINT_DIALOG_INTENT); 
      if (printJob == null || intent == null) { 
       return null; 
      } 
      ... 
     } 
    ... 
} 

正如你所看到的,PrintManager.print將發送一個意圖EXTRA_PRINT_DIALOG_INTENT,這將帶來對話。