2013-06-11 5 views
16

मेरे पास मेट्रो ऐप है और मैं WebView नियंत्रण की सामग्री को मुद्रित करने का प्रयास कर रहा हूं। मेरे स्रोत संदर्भ के रूप में एमएसडीएन प्रिंट नमूना का उपयोग करना। मैं बस के रूप में निम्नानुसार printableArea में XAML बदलने के लिए:मैं विंडोज स्टोर ऐप में वेबव्यू सामग्री कैसे प्रिंट करूं?

<RichTextBlock> 
     <Paragraph> 
      <InlineUIContainer> 
       <WebView Width="800" Height="2000" Source="http://www.stackoverflow.com"/> 
      </InlineUIContainer> 
     </Paragraph> 
    </RichTextBlock> 

यह आंशिक रूप से काम करता है। समस्या यह है कि निर्दिष्ट आयामों में Visible क्षेत्र मुद्रित है, यानी। जिस क्षेत्र को स्क्रॉल किया जा सकता है वह मुद्रित नहीं होता है और प्रिंटप्रूव में एकाधिक पृष्ठ के रूप में दिखाई नहीं देता है।

मैं लगभग वहां हूं, उम्मीद के अनुसार काम करने के लिए थोड़ी सी मदद की सराहना करता हूं।

मुझे किसी भी नमूने को कहीं भी नहीं मिला है, जो इस विशिष्ट समस्या से निपटता है।

मैं भी समाधान यहाँ की कोशिश की है: 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 बिंदु इनाम हल कर सकते हैं देने के लिए तैयार । एक समाधान के रूप में एक walkthrough, नमूना कोड या नकली परियोजना की सराहना करेंगे।

+0

RichTextBlock की ऊंचाई क्या है? क्या यह क्लिप करने के लिए सेट है? मुझे आश्चर्य है कि क्या यह वेबव्यू के बजाय आरटीबी/पैराग्राफ की ऊंचाई प्रिंट कर रहा है। –

उत्तर

28

निश्चित रूप से, आप यहां जाते हैं।

सबसे पहले, आप वास्तविक सामग्री में 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 इस तरह:

enter image description here

लेकिन है कि आप क्या चाहते हैं नहीं है।

भले ही आप परिणामी आयत का आकार बदल सकें ताकि यह इतना पागल आकार न हो, विंडोज 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, दूसरे स्तंभ (मध्य) है की तरह कुछ हो जाएगा सब-इन-वन हमारी पहली समाधान की तरह, और तीसरा, एक दोहराना है जो प्रिंट करने के लिए तैयार अलग-अलग पेज दिखा रहा है।

enter image description here

बेशक

जादू() विधि GetWebPages में वास्तव में है! मुझे यह नहीं लगता कि यह सी # और ट्रांसफॉर्म काम के तरीके से बहुत आसान बना है।

कृपया ध्यान दें, यह पूरा नहीं है। हाँ, यह आपके लिए पृष्ठ को तोड़ देता है, लेकिन मुझे यकीन नहीं है कि आप अपने पेज पर कितना मार्जिन चाहते हैं। तो आवश्यक tweaking छोटा है, लेकिन मैं इसका जिक्र करना चाहता था। यह 98% कोड है जिसे आपको वेबव्यू को तोड़ने और पेज 8 के लिए प्रतिक्रिया के लिए विंडोज 8 प्रिंट टास्क के लिए तैयार करना होगा। फिर आयतों को एक समय में पास करें।

ऐसा कहकर, यह शायद इंटरनेट पर इस समस्या का सबसे व्यापक समाधान है। :)

शुभकामनाएं !!

+1

धन्यवाद श्री निक्सन !!! आपके सुझाव स्पॉट पर थे। मैंने मार्जिन के लिए कोड शामिल करने के लिए अपना उत्तर अपडेट कर दिया है। बहुत - बहुत धन्यवाद। आप एक जीवन रक्षक है। मेरा ऐप अब ऐप स्टोर के लिए तैयार है :) – c0D3l0g1c

+1

बहुत चालाक - वास्तव में नेट पर सबसे अच्छा :) –

+0

मुझे इसे काम करने में कठिनाई हो रही है - मैं अपनी सामग्री को 'नेविगेट टॉस्ट्रिंग' का उपयोग करके स्थिर रूप से लोड करता हूं। कोड लगभग 4000 शब्दों की फाइल के लिए काम करता है, लेकिन अगर मैं थोड़ा सा पैड करता हूं तो यह काम करना बंद कर देता है। वेबव्यू घटता है, आइटम्स कंट्रोल खाली है और लगभग 4 सेकंड के बाद स्क्रीन ग्रे हो जाती है। कोई अपवाद या त्रुटियां नहीं। कोई विचार? – roryok

0

यहां मूल प्रिंटिंग वाक्यविन्यास है।

PrintManager.PrintTaskRequested += printManager_PrintTaskRequested; 
void printManager_PrintTaskRequested(
    Windows.Graphics.Printing.PrintManager sender, 
    Windows.Graphics.Printing.PrintTaskRequestedEventArgs args) 
    { 
     ... 
    } 
संबंधित मुद्दे