如何在Windows商店应用程序上打印Webview

时间:2013-09-04 10:49:58

标签: c# printing webview windows-runtime windows-store-apps

嘿伙计们,我一直在尝试使用Jerry Nixon在How do I print WebView content in a Windows Store App?上提供的教程和来自Adam Nathan的“windows 8 apps with xaml and c#”的教程来打印我的Webview,我附上了他的下面的代码例如:

using System;
using Windows.Graphics.Printing;
using Windows.Graphics.Printing.OptionDetails;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Printing;

namespace Chapter19
{
public sealed partial class MainPage : Page
{
    // Supports printing pages, where each page is a UIElement
    PrintDocument doc = new PrintDocument();

    public MainPage()
    {
        InitializeComponent();

        // Attach handlers to relevant events
        doc.GetPreviewPage += OnGetPreviewPage;
        doc.AddPages += OnAddPages;

        PrintManager printManager = PrintManager.GetForCurrentView();
        printManager.PrintTaskRequested += OnPrintTaskRequested;
    }

    // Prepare the print preview pages
    void OnGetPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        this.doc.SetPreviewPageCount(2, PreviewPageCountType.Final);
        if (e.PageNumber == 1)
        {
            this.doc.SetPreviewPage(1, new Viewbox
            {
                Child = new Button
                {
                    Content = "PAGE 1!",
                    Background = new SolidColorBrush(Colors.Red)
                }
            });
        }
        else
        {
            this.doc.SetPreviewPage(2, new Viewbox
            {
                Child = new Button
                {
                    Content = "PAGE 2!",
                    Background = new SolidColorBrush(Colors.Red)
                }
            });
        }
    }

    // Prepare the real pages
    void OnAddPages(object sender, AddPagesEventArgs e)
    {
        this.doc.AddPage(new Viewbox
        {
            Child = new Button
            {
                Content = "PAGE 1!",
                Background = new SolidColorBrush(Colors.Red)
            }
        });
        this.doc.AddPage(new Viewbox
        {
            Child = new Button
            {
                Content = "PAGE 2!",
                Background = new SolidColorBrush(Colors.Red)
            }
        });
        this.doc.AddPagesComplete();
    }

    // Prepare and perform the printing
    void OnPrintTaskRequested(PrintManager sender,
                              PrintTaskRequestedEventArgs args)
    {
        // This gets invoked as soon as the Devices pane is shown
        PrintTask task = args.Request.CreatePrintTask("Document Title",
          async (taskArgs) =>
          {
              // This is invoked on a background thread when the Print
              // button is clicked
              var deferral = taskArgs.GetDeferral();
              await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
              {
                  // This must run on the main thread
                  taskArgs.SetSource(doc.DocumentSource);
                  deferral.Complete();
              });
          });

        // Show custom options

        PrintTaskOptionDetails details =
        PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options);
        details.DisplayedOptions.Clear();
        details.DisplayedOptions.Add(StandardPrintTaskOptions.MediaSize);

        // A custom text option
        PrintCustomTextOptionDetails option1 = details.CreateTextOption(
          "CustomId1", "Header");
        details.DisplayedOptions.Add("CustomId1");

        // A custom list option
        PrintCustomItemListOptionDetails option2 = details.CreateItemListOption(
          "CustomId2", "Contents");
        option2.AddItem("customItemId1", "As Seen on Screen");
        option2.AddItem("customItemId2", "Summary View");
        option2.AddItem("customItemId3", "Full Details");
        option2.AddItem("customItemId4", "Multiple Columns");
        details.DisplayedOptions.Add("CustomId2");

        // Handle options changes
        details.OptionChanged += OnOptionChanged;
    }

    void OnOptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
    {
        // TODO: Handle custom options
    }
}
}

我遇到的问题是我正在努力获得杰里生成的“MywebviewPages”     “this.doc.SetPreviewPage” 和     “this.doc.AddPage” 来自Nathans的例子。由于winRT上缺少PDF api,我被迫使用webview,因为它是获取表格的唯一方法。请帮忙

0 个答案:

没有答案