如何在Windowsapp store应用中打印WebView内容?

我有一个Metro应用程序 ,我正在尝试打印WebView控件的内容。 使用MSDN Print Sample作为我的源参考。 我只需更改printableAreaXAML ,如下所示:

         

这部分工作。 问题是指定尺寸中的Visible区域是打印的,即可以滚动的区域不打印,也不会在PrintPreview中显示为多页

我几乎在那里,希望得到一些帮助,让它按预期工作。

我没有在任何地方找到任何样品,这解决了这个具体问题。

我甚至尝试过这里的解决方案: http : //social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/5edcb239-7a5b-49f7-87e3-e5a253b809c4

我不是第一个遇到相同/类似问题的人: http : //social.msdn.microsoft.com/Search/en-US/?Refinement = 112&query = print%20webview #refinementChanges = 180&pageNumber = 1&showMore = false

愿意给任何可以解决这个问题的人带来100分的赏金。 非常感谢演练,示例代码或模拟项目作为解决方案。

喏,给你。

首先,您可以将WebView大小调整为实际内容。 然后,将WebView缩放回原始大小。 它需要脚本调用和ScaleTransform 。 很简单,真的。

像这样:

    void MyWebView_LoadCompleted(object sender, NavigationEventArgs e) { var _Original = MyWebView.RenderSize; // ask the content its width var _WidthString = MyWebView.InvokeScript("eval", new[] { "document.body.scrollWidth.toString()" }); int _Width; if (!int.TryParse(_WidthString, out _Width)) throw new Exception(string.Format("failure/width:{0}", _WidthString)); // ask the content its height var _HeightString = MyWebView.InvokeScript("eval", new[] { "document.body.scrollHeight.toString()" }); int _Height; if (!int.TryParse(_HeightString, out _Height)) throw new Exception(string.Format("failure/height:{0}", _HeightString)); // resize the webview to the content MyWebView.Width = _Width; MyWebView.Height = _Height; // scale the webview back to original height (can't do both height & width) var _Transform = (MyWebView.RenderTransform as ScaleTransform) ?? (MyWebView.RenderTransform = new ScaleTransform()) as ScaleTransform; var _Scale = _Original.Height / _Height; _Transform.ScaleX = _Transform.ScaleY = _Scale; } 

这将导致一个非常高,很窄的WebView如下所示:

在此处输入图像描述

但那不是你想要的。

尽管您可以调整生成的矩形的大小以使其不那么疯狂,但Windows 8中的“打印合同”要求您为其提供单个页面。 它不会为你做分页。 因此,您真正需要的是一次一页地检索单个网站。

第一种方法是如何做到这一点的基础。 但是您需要将矩形的大小固定为Windows 8的“打印”任务传递给您的页面大小。 这将基于用户的打印机选择。 例如,Letter与A4(英国)。 然后,使用画笔的“拉伸”属性可以确保它自行裁剪。 然后,使用画笔的“变换”属性,可以在矩形内部上下滑动,直到它显示您要打印的页面。

这是如何做:

                   public MainPage() { this.InitializeComponent(); MyWebView.LoadCompleted += MyWebView_LoadCompleted; } void MyWebView_LoadCompleted(object sender, NavigationEventArgs e) { MyWebViewRectangle.Fill = GetWebViewBrush(MyWebView); MyPrintPages.ItemsSource = GetWebPages(MyWebView, new Windows.Foundation.Size(100d, 150d)); MyWebView.Visibility = Windows.UI.Xaml.Visibility.Visible; } WebViewBrush GetWebViewBrush(WebView webView) { // resize width to content var _OriginalWidth = webView.Width; var _WidthString = webView.InvokeScript("eval", new[] { "document.body.scrollWidth.toString()" }); int _ContentWidth; if (!int.TryParse(_WidthString, out _ContentWidth)) throw new Exception(string.Format("failure/width:{0}", _WidthString)); webView.Width = _ContentWidth; // resize height to content var _OriginalHeight = webView.Height; var _HeightString = webView.InvokeScript("eval", new[] { "document.body.scrollHeight.toString()" }); int _ContentHeight; if (!int.TryParse(_HeightString, out _ContentHeight)) throw new Exception(string.Format("failure/height:{0}", _HeightString)); webView.Height = _ContentHeight; // create brush var _OriginalVisibilty = webView.Visibility; webView.Visibility = Windows.UI.Xaml.Visibility.Visible; var _Brush = new WebViewBrush { SourceName = webView.Name, Stretch = Stretch.Uniform }; _Brush.Redraw(); // reset, return webView.Width = _OriginalWidth; webView.Height = _OriginalHeight; webView.Visibility = _OriginalVisibilty; return _Brush; } IEnumerable GetWebPages(WebView webView, Windows.Foundation.Size page) { // ask the content its width var _WidthString = webView.InvokeScript("eval", new[] { "document.body.scrollWidth.toString()" }); int _ContentWidth; if (!int.TryParse(_WidthString, out _ContentWidth)) throw new Exception(string.Format("failure/width:{0}", _WidthString)); webView.Width = _ContentWidth; // ask the content its height var _HeightString = webView.InvokeScript("eval", new[] { "document.body.scrollHeight.toString()" }); int _ContentHeight; if (!int.TryParse(_HeightString, out _ContentHeight)) throw new Exception(string.Format("failure/height:{0}", _HeightString)); webView.Height = _ContentHeight; // how many pages will there be? var _Scale = page.Width / _ContentWidth; var _ScaledHeight = (_ContentHeight * _Scale); var _PageCount = (double)_ScaledHeight / page.Height; _PageCount = _PageCount + ((_PageCount > (int)_PageCount) ? 1 : 0); // create the pages var _Pages = new List(); for (int i = 0; i < (int)_PageCount; i++) { var _TranslateY = -page.Height * i; var _Page = new Windows.UI.Xaml.Shapes.Rectangle { Height = page.Height, Width = page.Width, Margin = new Thickness(5), Tag = new TranslateTransform { Y = _TranslateY }, }; _Page.Loaded += (s, e) => { var _Rectangle = s as Windows.UI.Xaml.Shapes.Rectangle; var _Brush = GetWebViewBrush(webView); _Brush.Stretch = Stretch.UniformToFill; _Brush.AlignmentY = AlignmentY.Top; _Brush.Transform = _Rectangle.Tag as TranslateTransform; _Rectangle.Fill = _Brush; }; _Pages.Add(_Page); } return _Pages; } 

因此UI将是这样的,其中左列是WebView,第二列(中间)是像我们的第一个解决方案一体化,第三列是转发器,显示准备打印的各个页面。

在此处输入图像描述

当然魔术真的在GetWebPages()方法中! 我不介意说,这是一个简单的奇迹,通过C#和Transforms的工作方式变得非常简单。

请注意,这还不完整 。 是的,它打破了你的页面,但我无法确定你想要在你的页面上多少边距。 因此所需的调整很小,但我想提一下。 这是分解WebView所需代码的98%,如果是针对paginate事件的Windows 8打印任务,则需要准备。 然后一次一个地将矩形传递给它。

话虽如此,这可能是互联网上这个问题最全面的解决方案。 🙂

祝你好运!

这是基本的打印语法。

 PrintManager.PrintTaskRequested += printManager_PrintTaskRequested; void printManager_PrintTaskRequested( Windows.Graphics.Printing.PrintManager sender, Windows.Graphics.Printing.PrintTaskRequestedEventArgs args) { ... }