当然,这就是您需要的翻译。首先,您可以将
WebView
调整为实际内容大小,然后将其缩放回原始大小。这需要一个脚本调用和一个
ScaleTransform
。真的很简单。像这样:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="MyWebView" Source="http://www.stackoverflow.com" />
</Grid>
void MyWebView_LoadCompleted(object sender, NavigationEventArgs e)
{
var _Original = MyWebView.RenderSize;
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));
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));
MyWebView.Width = _Width;
MyWebView.Height = _Height;
var _Transform = (MyWebView.RenderTransform as ScaleTransform)
?? (MyWebView.RenderTransform = new ScaleTransform()) as ScaleTransform;
var _Scale = _Original.Height / _Height;
_Transform.ScaleX = _Transform.ScaleY = _Scale;
}
这将导致一个非常高而窄的WebView
,如下所示:
![enter image description here](https://istack.dev59.com/pSoFX.webp)
但这不是你想要的。
即使你可以调整结果矩形的大小,使其不那么奇怪,但Windows 8中的打印合同要求您提供单个页面。它不会为您分页。因此,您真正需要的是逐页检索单个网站。
第一种方法是如何实现这一点的基础。但是,您需要根据Windows 8的打印任务传递给您的页面大小来修复矩形的大小。这将基于用户的打印机选择。例如,Letter与A4(在英国)。然后,使用刷子的Stretch属性,您可以确保它裁剪自己。然后,使用画刷的Transform属性,您可以在矩形内上下滑动,直到它显示您要打印的页面。
具体操作如下:
<Grid Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="995" />
<ColumnDefinition Width="300" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<WebView Grid.Column="0" x:Name="MyWebView" Source="http:
<Rectangle Grid.Column="1" x:Name="MyWebViewRectangle" Fill="Red" />
<ScrollViewer Grid.Column="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ItemsControl x:Name="MyPrintPages" VerticalAlignment="Top" HorizontalAlignment="Left">
<Rectangle Height="150" Width="100" Fill="White" Margin="5" />
<Rectangle Height="150" Width="100" Fill="White" Margin="5" />
<Rectangle Height="150" Width="100" Fill="White" Margin="5" />
<Rectangle Height="150" Width="100" Fill="White" Margin="5" />
<Rectangle Height="150" Width="100" Fill="White" Margin="5" />
</ItemsControl>
</ScrollViewer>
</Grid>
public MainPage()
void MyWebView_LoadCompleted(object sender, NavigationEventArgs e)
WebViewBrush GetWebViewBrush(WebView webView)
);
int _ContentWidth;
if (!int.TryParse(_WidthString, out _ContentWidth))
throw new Exception(string.Format("failure/width:", _WidthString));
webView.Width = _ContentWidth;
var _OriginalHeight = webView.Height;
var _HeightString = webView.InvokeScript("eval",
new[] );
int _ContentHeight;
if (!int.TryParse(_HeightString, out _ContentHeight))
throw new Exception(string.Format("failure/height:", _HeightString));
webView.Height = _ContentHeight;
var _OriginalVisibilty = webView.Visibility;
webView.Visibility = Windows.UI.Xaml.Visibility.Visible;
var _Brush = new WebViewBrush
;
_Brush.Redraw();
webView.Width = _OriginalWidth;
webView.Height = _OriginalHeight;
webView.Visibility = _OriginalVisibilty;
return _Brush;
}
IEnumerable<FrameworkElement> GetWebPages(WebView webView, Windows.Foundation.Size page)
);
int _ContentWidth;
if (!int.TryParse(_WidthString, out _ContentWidth))
throw new Exception(string.Format("failure/width:", _WidthString));
webView.Width = _ContentWidth;
var _HeightString = webView.InvokeScript("eval",
new[] );
int _ContentHeight;
if (!int.TryParse(_HeightString, out _ContentHeight))
throw new Exception(string.Format("failure/height:", _HeightString));
webView.Height = _ContentHeight;
var _Scale = page.Width / _ContentWidth;
var _ScaledHeight = (_ContentHeight * _Scale);
var _PageCount = (double)_ScaledHeight / page.Height;
_PageCount = _PageCount + ((_PageCount > (int)_PageCount) ? 1 : 0);
var _Pages = new List<Windows.UI.Xaml.Shapes.Rectangle>();
for (int i = 0; i < (int)_PageCount; i++)
,
};
_Page.Loaded += (s, e) =>
;
_Pages.Add(_Page);
}
return _Pages;
}
所以,UI 将会像这样,左侧列是 WebView,第二列(中间)是类似于我们第一个解决方案的 all-in-one,第三列是一个重复器,显示准备打印的单个页面。
![enter image description here](https://istack.dev59.com/kTz8r.webp)
当然,神奇的真正在于 GetWebPages() 方法!我不介意说,它是一个通过 C# 和 Transform 的工作方式非常容易而简单的奇迹。
请注意,这不是完整的代码。是的,它为您分割了页面,但我无法确定您希望页面有多少边距。所需的微调很小,但我想提一下。这是您需要将 WebView 分割并准备好响应 paginate 事件的 Windows 8 打印任务的 98% 代码。然后逐个矩形传递给它。
话虽如此,这可能是互联网上这个问题的最全面的解决方案。 :)
祝你好运!!