locked
How to print a long page from webview RRS feed

  • Question

  • User358544 posted

    Hello, I implemented a print function in webview based my project but it only prints the visible part of the page. This is the function:

                var printMgr = (PrintManager)activity.GetSystemService(Context.PrintService);
                PrintAttributes.Builder builder = new PrintAttributes.Builder();
                builder.SetMediaSize(PrintAttributes.MediaSize.UnknownPortrait); //some tries
    
                if (((int)Android.OS.Build.VERSION.SdkInt) >= 21)
                    printMgr.Print("page", webview.CreatePrintDocumentAdapter("page"), builder.Build());
                else
                    printMgr.Print("page", webview.CreatePrintDocumentAdapter(), builder.Build());
    

    I don't find any help on that. Is it possible to print the full page ?

    Thanks in advance.

    Saturday, September 29, 2018 10:24 AM

Answers

  • User358544 posted

    I found the the problem was with my css (body{height:100%;}). Thanks

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 1, 2018 10:13 AM

All replies

  • User364855 posted

    @entretoize I have tested your code, it works fine. How do you set the webview? Could you share your project?

    The page and code I'm using:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    android:orientation="vertical">
    <Button     android:layout_width="match_parent"
        android:layout_height="wrap_content" android:text="Button" android:id="@+id/button1" />
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    </LinearLayout>
    
    public class MainActivity : AppCompatActivity
    {
        WebView web_view;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
    
            web_view = FindViewById<WebView>(Resource.Id.webview);
            web_view.Settings.JavaScriptEnabled = true;
            web_view.SetWebViewClient(new WebViewClient());
            web_view.LoadUrl("https://www.xamarin.com/university");
    
            Button button = FindViewById<Button>(Resource.Id.button1);
            button.Click += Button_Click;           
        }
    
        private void Button_Click(object sender, System.EventArgs e)
        {
            var printMgr = (PrintManager)this.GetSystemService(Context.PrintService);
            PrintAttributes.Builder builder = new PrintAttributes.Builder();
            builder.SetMediaSize(PrintAttributes.MediaSize.UnknownPortrait); //some tries
    
            if (((int)Android.OS.Build.VERSION.SdkInt) >= 21)
                printMgr.Print("page", web_view.CreatePrintDocumentAdapter("page"), builder.Build());
            else
                printMgr.Print("page", web_view.CreatePrintDocumentAdapter(), builder.Build());
        }
    }
    
    Monday, October 1, 2018 8:27 AM
  • User358544 posted

    I found the the problem was with my css (body{height:100%;}). Thanks

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, October 1, 2018 10:13 AM