我搜索了如何打印一个简单WebView
的 as :
<WebView x:Name="MyWebView" Source="http://www.stackoverflow.com" />
我尝试了很多事情:
如何在 Windows 应用商店应用中打印 WebView 内容?
遗憾的是,此解决方案并未针对 Windows 10 UWP 应用程序进行更新,但是,我对其进行了转换,以便过时的功能可以正常工作,这会导致我遇到另一个问题。
为了打印,我使用了示例中的一些类(Windows-universal-samples/Samples/Printing/cs/),因为我需要预览。我将上述问题中给出的示例设置为:
<RichTextBlock
x:Name="TextContent"
Grid.Row="1"
Grid.ColumnSpan="2"
Width="595"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Foreground="Black"
IsTextSelectionEnabled="True"
OverflowContentTarget="{Binding ElementName=FirstLinkedContainer}">
<Paragraph>
<InlineUIContainer> [The question XAML]
溢出不受管理,因此WebView
看起来像这样:
注意:我是 XAML 语言的新手,也是这背后的逻辑。
我想了解如何执行此操作以及是否可行?
编辑:
这是我背后的代码PageToPrint.xaml.cs
:
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace TestPrint
{
/// <summary>
/// Page content to send to the printer
/// </summary>
public sealed partial class PageToPrint : Page
{
public RichTextBlock TextContentBlock { get; set; }
public PageToPrint()
{
this.InitializeComponent();
TextContentBlock = TextContent;
MyWebView.LoadCompleted += MyWebView_LoadCompletedAsync;
}
async void MyWebView_LoadCompletedAsync(object sender, NavigationEventArgs e)
{
MyWebViewRectangle.Fill = await GetWebViewBrushAsync(MyWebView);
MyPrintPages.ItemsSource = await GetWebPagesAsync(MyWebView, new Windows.Foundation.Size(100d, 150d));
MyWebView.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
async System.Threading.Tasks.Task<WebViewBrush> GetWebViewBrushAsync(WebView webView)
{
// resize width to content
var _OriginalWidth = webView.Width;
var _WidthString = await webView.InvokeScriptAsync("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 = await webView.InvokeScriptAsync("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;
}
async System.Threading.Tasks.Task<IEnumerable<FrameworkElement>> GetWebPagesAsync(WebView webView, Windows.Foundation.Size page)
{
// ask the content its width
var _WidthString = await webView.InvokeScriptAsync("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 = await webView.InvokeScriptAsync("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<Windows.UI.Xaml.Shapes.Rectangle>();
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 += async (s, e) =>
{
var _Rectangle = s as Windows.UI.Xaml.Shapes.Rectangle;
var _Brush = await GetWebViewBrushAsync(webView);
_Brush.Stretch = Stretch.UniformToFill;
_Brush.AlignmentY = AlignmentY.Top;
_Brush.Transform = _Rectangle.Tag as TranslateTransform;
_Rectangle.Fill = _Brush;
};
_Pages.Add(_Page);
}
return _Pages;
}
}
}