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の内容が
を次のように私を取得、すなわちプレビューとして、プリンタ機能で表示することができませんこの問題を解決するために、Webで閲覧されたPDFコンテンツを入手して印刷することができます。
あなたが問題を再現することができ、基本的なデモを共有していただけますか? PDFリンクは偽造品になる可能性があります。 –
PrintManagerがプレビューおよび印刷用のコンテンツを見つける方法を決定するonLayout()およびonWrite()の実装が表示されません – Nonos
@ROHIT:問題の解決策を見つけましたか? – MilanG