2011-05-11 2 views
0

WPF/Silverlight의 인쇄 비주얼에는 몇 가지 단점이 있습니다. 보통 사람들은 보통 FlowDocument 또는 FixedDocument를 사용하여 인쇄하는 경향이 있습니다.WPF/Silverlight : Visuals의 인쇄 - 단점

그래픽으로 강렬한 대시 보드를 인쇄하고 시각적으로 직접 인쇄하는 것이 가장 쉬운 방법 인 것 같습니다. 모든 대시 보드는 한 페이지에 인쇄되기 때문에 페이지 매김에 신경 쓸 필요가 없습니다.

이러한 인쇄 방법을 선택하기 전에 반드시 고려해야 할 단점이 있습니까?

답변

0

Visual 개체를 FrameworkElement에 호스팅하고 FixedPage의 내용으로 FixedDocument에 FrameworkElement를 추가하여 인쇄 할 수 있습니다. 비주얼 호스트는 다음과 같습니다

/// <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

+0

내가 직면 한 단점은 없습니다. 위의 메서드를 사용하여 WPF 객체로 채워진 페이지를 인쇄했으며 DrawingVisual을 사용하여 수천 줄의 선들과 채워진 영역을 가진 믿을 수 없을 정도로 복잡한 도면을 인쇄하는데도 사용했습니다. –