2017-10-31 15 views
1

URLの内容を印刷するために、問題を手伝ってもらえますか? 私は自分のアプリケーションのWebビューでPDFファイルを開き、別のボタンを押してそこから文書を印刷しようとしています。以下のコードを見てください。これは携帯電話のデフォルトのプリンタ機能を使用して、XamarinのAndroidでWebViewから開いたPDFの内容を印刷する方法

[Activity(Label = "PdfView")] 
    public class PdfView : Activity 
    { 
     public static PdfView _context; 
     Android.Content.Res.Resources res; 
     private static string ErrorTitle, ErrorMsg; 
     private static ProgressBar progressBar; 

     WebView mWebView, Web_Print; 
     RelativeLayout LL_Print,LL_input; 
     string MyUrl; 
     private WebClient _webClient = new WebClient(); 
     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      _context = this; 
      res = _context.Resources; 
      SetContentView(Resource.Layout.web_pdf); 
      ErrorTitle = res.GetString(Resource.String.error_title); 
      ErrorMsg = res.GetString(Resource.String.Somethingwentwrong); 
      progressBar = FindViewById<ProgressBar>(Resource.Id.progressBar1); 
      LL_Print = FindViewById<RelativeLayout>(Resource.Id.LL_Print); 
      LL_input = FindViewById<RelativeLayout>(Resource.Id.LL_input); 
      Web_Print = FindViewById<WebView>(Resource.Id.Web_Print); 
      Web_Print.Visibility = ViewStates.Gone; 
      LL_Print.Visibility = ViewStates.Gone; 
      var customWebViewClient = new WebViewLoaderClient(); 
      mWebView = FindViewById<WebView>(Resource.Id.Web); 
      mWebView.Download += MWebview_Download;    
      mWebView.Settings.JavaScriptEnabled = true; 
      mWebView.Settings.BuiltInZoomControls = true; 
      mWebView.SetWebViewClient(customWebViewClient); 
      mWebView.SetWebChromeClient(new WebChromeClient()); 
      String myPdfUrl = APIConstants.InvoicePdfUrl; 
      MyUrl = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl; 
      mWebView.LoadUrl(MyUrl); 

      InitComonents(); 
     } 

     private void MWebview_Download(object sender, DownloadEventArgs e) 
     { 
      mWebView.Visibility = ViewStates.Gone; 
      LL_input.Visibility = ViewStates.Gone; 
      LL_Print.Visibility = ViewStates.Visible; 
      Web_Print.Visibility = ViewStates.Visible; 
      var Url = e.Url; 
      CustomWebViewClient client = new CustomWebViewClient(this); 
      Web_Print.SetWebViewClient(client); 
      Web_Print.SetWebChromeClient(new WebChromeClient()); 
      Web_Print.Settings.JavaScriptEnabled = true; 
      Web_Print.LoadUrl(Url); 
      Web_Print.Settings.UseWideViewPort = true; 
      Web_Print.Settings.LoadWithOverviewMode = true; 
     } 


     #region // Initializing widgets 
     public void InitComonents() 
     { 
      try 
      { 
       LinearLayout img_viw = FindViewById<LinearLayout>(Resource.Id.img_WV_back); 
       LinearLayout img_viw_Home = FindViewById<LinearLayout>(Resource.Id.img_WV_home); 

      } 
      catch (Exception ex) 
      { 
       ex.Message.ToString(); 
      } 
     } 
     #endregion 

     #region // Webview client 
     public class WebViewLoaderClient : WebViewClient 
     { 
      public event EventHandler<bool> OnPageLoaded; 
      public override bool ShouldOverrideUrlLoading(WebView view, string url) 
      { 
       view.LoadUrl(url); 
       return true; 
      } 
      public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon) 
      { 
       base.OnPageStarted(view, url, favicon); 
       progressBar.Visibility = ViewStates.Visible; 
      } 
      public override void OnPageFinished(WebView view, string url) 
      { 
       base.OnPageFinished(view, url); 
       progressBar.Visibility = ViewStates.Gone; 
      } 
      public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl) 
      { 
       base.OnReceivedError(view, errorCode, description, failingUrl); 
       view.Visibility = ViewStates.Gone; 
       Error(); 
      } 
     } 
     class MonkeyWebViewClient : WebViewClient 
     { 
      public override bool ShouldOverrideUrlLoading(WebView view, string url) 
      { 
       view.LoadUrl(url); 
       return true; 
      } 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
      { 
       view.LoadUrl("file:///android_asset/myerrorpage.html"); 
      } 
     } 
     #endregion 


     #region Error Message for Web View Error 
     public static void Error() 
     { 
      AlertDialog.Builder alert = new AlertDialog.Builder(_context); 
      alert.SetTitle(ErrorTitle); 
      alert.SetMessage(ErrorMsg); 
      alert.SetPositiveButton(Resource.String.ok, (senderAlert, args) => 
      { 
       _context.Finish(); 
      }); 
      alert.SetCancelable(false); 
      Dialog dialog = alert.Create(); 
      dialog.Show(); 
     } 

     public void doWebViewPrint(string url) 
     { 
      PrintDocumentAdapter adapter; 
      if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) 
       adapter = mWebView.CreatePrintDocumentAdapter("test"); 
      else 
       adapter = mWebView.CreatePrintDocumentAdapter(); 
      var printMgr = (PrintManager)GetSystemService(PrintService); 
      printMgr.Print("printTest", adapter, null); 
     } 
     #endregion 
    } 

    public class CustomWebViewClient : WebViewClient 
    { 
     private PdfView myActivity; 
     public bool shouldOverrideUrlLoading(WebView view, string url) 
     { 
      view.LoadUrl(url); 
      return false; 
     } 
     public override void OnPageFinished(WebView view, string url) 
     { 
      Log.Debug("PRINT", "page finished loading " + url); 
      myActivity.doWebViewPrint(url); 
      //CreateWebPrintJob(view); 
      base.OnPageFinished(view, url); 
     } 

     private void CreateWebPrintJob(WebView view) 
     { 
      throw new NotImplementedException(); 
     } 

     public CustomWebViewClient(PdfView activity) 
     { 
      myActivity = activity; 
     } 
    } 
} 

できショーは、PDFの内容で、プリンタ機能を開くが、PDFの内容がPrinter Default

を次のように私を取得、すなわちプレビューとして、プリンタ機能で表示することができません

この問題を解決するために、Webで閲覧されたPDFコンテンツを入手して印刷することができます。

+0

あなたが問題を再現することができ、基本的なデモを共有していただけますか? PDFリンクは偽造品になる可能性があります。 –

+0

PrintManagerがプレビューおよび印刷用のコンテンツを見つける方法を決定するonLayout()およびonWrite()の実装が表示されません – Nonos

+0

@ROHIT:問題の解決策を見つけましたか? – MilanG

答えて

1

ここでは、完全なゲームチェンジャーは、規模計算である ::印刷することを可能にした次のコードを参照してください、

. 
. 
. 
. 
. 
. 
. 
     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      _context = this; 
      res = _context.Resources; 
      SetContentView(Resource.Layout.xxxxxxxxxxxxxxxx); 
      var customWebViewClient = new WebViewLoaderClient(); 
      mWebView = FindViewById<WebView>(Resource.Id.Web); 
      //mWebView.Download += MWebview_Download; 
      UserLoginDetailList = UserInfoDBServices.getUserLoginDetailsfromDBfordroid(); 
      ObjLangList = UserInfoDBServices.GetCurrentLanguageListfordroid(); 
      mWebView.Settings.JavaScriptEnabled = true; 
      mWebView.Settings.BuiltInZoomControls = true; 
      mWebView.SetWebViewClient(customWebViewClient); 
      String myPdfUrl = APIConstants.InvoicePdfUrl; 
      MyUrl = "http://docs.google.com/gview?embedded=true&url=" + myPdfUrl;//Here in myPdfUrl variable place the url of the pdf you are getting from your server 
      mWebView.LoadUrl(MyUrl); 
      InitComonents(); 
     } 
     #region // Initializing widgets 
     public void InitComonents() 
     { 
      try 
      { 
       Print = FindViewById<LinearLayout>(Resource.Id.ll_TNP_Pirnt); 
       Print.Visibility = ViewStates.Visible; 
       Print.Click += delegate 
        { 
         LL_Print.Clickable = false; 
         var printManager = (PrintManager)GetSystemService(Context.PrintService); 
         var content = FindViewById<LinearLayout>(Resource.Id.LL_input); 
         var printAdapter = new GenericPrintAdapter(this, content); 

         printManager.Print("MyPrintJob", printAdapter, null); 
        }; 
      } 
      catch (Exception ex) 
      { 
       ex.Message.ToString(); 
      } 
     } 
     #endregion 
     public class GenericPrintAdapter:PrintDocumentAdapter 
     { 
      View view; 
      Context context; 
      PrintedPdfDocument document; 
      float scale; 
      private DownloadPdfView downloadPdfView; 
      private RelativeLayout content; 

      public GenericPrintAdapter(DownloadPdfView downloadPdfView, View view) 
      { 
       this.downloadPdfView = downloadPdfView; 
       this.view = view; 
      } 

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

        CalculateScale(newAttributes); 

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

        callback.OnLayoutFinished(printInfo, true); 
       } 
       catch(Exception ex) 
       { 
        ex.Message.ToString(); 
       } 

      } 

      public override void OnWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) 
      { 

       try 
       { 
        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); 
       } 
       catch (Exception ex) 
       { 
        ex.Message.ToString(); 
       } 

      } 
      void CalculateScale(PrintAttributes newAttributes) 
      { 
       try 
       { 
        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/3500) - leftMargin - rightMargin; 
        int h = (int)(dpi * (float)newAttributes.GetMediaSize().HeightMils/3500) - topMargin - bottomMargin; 

        scale = Math.Min((float)document.PageContentRect.Width()/w, (float)document.PageContentRect.Height()/h); 
       } 
       catch (Exception ex) 
       { 
        ex.Message.ToString(); 
       } 

      } 
      void WritePrintedPdfDoc(ParcelFileDescriptor destination) 
      { 
       try 
       { 
        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); 
        } 
       } 
       catch (Exception ex) 
       { 
        ex.Message.ToString(); 
       } 
      } 
     } 

     #region // Webview client 
     public class WebViewLoaderClient : WebViewClient 
     { 
      public event EventHandler<bool> OnPageLoaded; 
      public override bool ShouldOverrideUrlLoading(WebView view, string url) 
      { 
       view.LoadUrl(url); 
       return true; 
      } 
      public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon) 
      { 
       base.OnPageStarted(view, url, favicon); 
       progressBar.Visibility = ViewStates.Visible; 
      } 
      public override void OnPageFinished(WebView view, string url) 
      { 
       base.OnPageFinished(view, url); 
       progressBar.Visibility = ViewStates.Gone; 
      } 
      public override void OnReceivedError(WebView view, ClientError errorCode, string description, string failingUrl) 
      { 
       base.OnReceivedError(view, errorCode, description, failingUrl); 
       view.Visibility = ViewStates.Gone; 
       Error(); 
      } 
     } 
     class MonkeyWebViewClient : WebViewClient 
     { 
      public override bool ShouldOverrideUrlLoading(WebView view, string url) 
      { 
       view.LoadUrl(url); 
       return true; 
      } 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
      { 
       view.LoadUrl("file:///android_asset/myerrorpage.html"); 
      } 
     } 
     #endregion 

     #region Error Message for Web View Error 
     public static void Error() 
     { 
      AlertDialog.Builder alert = new AlertDialog.Builder(_context); 
      alert.SetTitle(ErrorTitle); 
      alert.SetMessage(ErrorMsg); 
      alert.SetPositiveButton(Resource.String.ok, (senderAlert, args) => 
      { 
       _context.Finish(); 
      }); 
      alert.SetCancelable(false); 
      Dialog dialog = alert.Create(); 
      dialog.Show(); 
     } 
     #endregion 
    } 
} 
関連する問題