2011-05-11 44 views
0

我一次或兩次讀取WPF/Silverlight打印視覺效果有一些缺點,所以人們通常傾向於使用FlowDocument或FixedDocument進行打印。WPF/Silverlight:視覺印刷 - 缺點

我想打印一些圖形強烈的儀表板,並直接打印視覺似乎是最簡單的方法。我不必關心分頁,因爲我想打印的每個儀表板都應該放在一個頁面上。

在選擇這種印刷方式之前,我還必須考慮一些缺點嗎?

回答

0

您可以通過將它們託管在FrameworkElement中打印可視對象,然後將FrameworkElement作爲固定頁面的內容添加到FixedDocument中。可視主機看起來是這樣的:

/// <summary> 
/// Implements a FrameworkElement host for a Visual 
/// </summary> 
public class VisualHost : FrameworkElement 
{ 
    Visual _visual; 


    /// <summary> 
    /// Gets the number of visual children (always 1) 
    /// </summary> 
    protected override int VisualChildrenCount 
    { 
     get { return 1; } 
    } 


    /// <summary> 
    /// Constructor 
    /// </summary> 
    /// <param name="visual">The visual to host</param> 
    public VisualHost(Visual visual) 
    { 
     _visual = visual; 

     AddVisualChild(_visual); 
     AddLogicalChild(_visual); 
    } 


    /// <summary> 
    /// Get the specified visual child (always 
    /// </summary> 
    /// <param name="index">Index of visual (should always be 0)</param> 
    /// <returns>The visual</returns> 
    protected override Visual GetVisualChild(int index) 
    { 
     if (index != 0) 
      throw new ArgumentOutOfRangeException("index out of range"); 
     return _visual; 
    } 
} 

然後,您可以添加它們,並打印出來是這樣的:

 // Start the fixed document 
     FixedDocument fixedDoc = new FixedDocument(); 
     Point margin = new Point(96/2, 96/2);  // Half inch margins 

     // Add the visuals 
     foreach (Visual nextVisual in visualCollection) 
     { 
      // Host the visual 
      VisualHost host = new VisualHost(nextVisual); 
      Canvas canvas = new Canvas(); 
      Canvas.SetLeft(host, margin.X); 
      Canvas.SetTop(host, margin.Y); 
      canvas.Children.Add(host); 

      // Make a FixedPage out of the canvas and add it to the document 
      PageContent pageContent = new PageContent(); 
      FixedPage fixedPage = new FixedPage(); 
      fixedPage.Children.Add(canvas); 
      ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage); 
      fixedDoc.Pages.Add(pageContent); 
     } 

     // Write the finished FixedDocument to the print queue 
     XpsDocumentWriter xpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(queue); 
     xpsDocumentWriter.Write(fixedDoc); 
+0

感謝您的片段。但我的問題更多的是關於這種印刷的實際經驗。會出現什麼樣的問題,我會不得不比平常更多地將我的頭靠在牆上,等等。 – Amenti 2011-05-17 13:41:15

+0

我遇到過沒有缺點。我已經使用上面的方法來打印充滿WPF對象的頁面,並且我還用它來使用DrawingVisual打印具有數千行和填充區域的令人難以置信的複雜繪圖。 – 2011-05-17 19:22:13