निश्चित रूप से, आप यहां जाते हैं।
सबसे पहले, आप वास्तविक सामग्री में WebView
का आकार बदल सकते हैं। फिर, आप मूल आकार में 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;
// 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
इस तरह:
लेकिन है कि आप क्या चाहते हैं नहीं है।
भले ही आप परिणामी आयत का आकार बदल सकें ताकि यह इतना पागल आकार न हो, विंडोज 8 में प्रिंट अनुबंध की आवश्यकता है कि आप इसे एक पृष्ठ के साथ प्रदान करें। यह आपके लिए अंकन नहीं करता है। नतीजतन, आपको वास्तव में क्या चाहिए, व्यक्तिगत वेबसाइट, एक समय में एक पृष्ठ को पुनर्प्राप्त करना है।
पहला दृष्टिकोण यह है कि यह कैसे करें। लेकिन आपको विंडोज 8 के प्रिंट टास्क द्वारा पारित पृष्ठ आकार में आयताकार के आकार को ठीक करने की आवश्यकता है। यह उपयोगकर्ता के प्रिंटर चयन पर आधारित होगा। उदाहरण के लिए, पत्र बनाम ए 4 (यूके में)। फिर, ब्रश की खिंचाव संपत्ति का उपयोग करके आप यह सुनिश्चित कर सकते हैं कि यह स्वयं फसलें। फिर, ब्रश की ट्रांसफॉर्म प्रॉपर्टी का उपयोग करके, आप इसे आयत के अंदर ऊपर और नीचे स्लाइड कर सकते हैं जब तक कि वह उस पृष्ठ को प्रकट नहीं कर रहा हो जिसे आप मुद्रित करना चाहते हैं।
आइए जानें:
<Grid Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="995" />
<ColumnDefinition Width="300" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<WebView Grid.Column="0" x:Name="MyWebView" Source="http://www.stackoverflow.com" HorizontalAlignment="Right" />
<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()
{
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<FrameworkElement> 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<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 += (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;
}
तो यूआई यह है, जहां बाएँ स्तंभ WebView, दूसरे स्तंभ (मध्य) है की तरह कुछ हो जाएगा सब-इन-वन हमारी पहली समाधान की तरह, और तीसरा, एक दोहराना है जो प्रिंट करने के लिए तैयार अलग-अलग पेज दिखा रहा है।
बेशक
जादू() विधि GetWebPages में वास्तव में है! मुझे यह नहीं लगता कि यह सी # और ट्रांसफॉर्म काम के तरीके से बहुत आसान बना है।
कृपया ध्यान दें, यह पूरा नहीं है। हाँ, यह आपके लिए पृष्ठ को तोड़ देता है, लेकिन मुझे यकीन नहीं है कि आप अपने पेज पर कितना मार्जिन चाहते हैं। तो आवश्यक tweaking छोटा है, लेकिन मैं इसका जिक्र करना चाहता था। यह 98% कोड है जिसे आपको वेबव्यू को तोड़ने और पेज 8 के लिए प्रतिक्रिया के लिए विंडोज 8 प्रिंट टास्क के लिए तैयार करना होगा। फिर आयतों को एक समय में पास करें।
ऐसा कहकर, यह शायद इंटरनेट पर इस समस्या का सबसे व्यापक समाधान है। :)
शुभकामनाएं !!
RichTextBlock की ऊंचाई क्या है? क्या यह क्लिप करने के लिए सेट है? मुझे आश्चर्य है कि क्या यह वेबव्यू के बजाय आरटीबी/पैराग्राफ की ऊंचाई प्रिंट कर रहा है। –