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);
감사 관용구. 그러나 제 질문은 이런 종류의 인쇄에 대한 실제 경험에 관한 것입니다. 어떤 문제가 발생할 것인가, 나는 평소보다 벽에 머리를 부딪쳐 야 할 것이다. – Amenti
내가 직면 한 단점은 없습니다. 위의 메서드를 사용하여 WPF 객체로 채워진 페이지를 인쇄했으며 DrawingVisual을 사용하여 수천 줄의 선들과 채워진 영역을 가진 믿을 수 없을 정도로 복잡한 도면을 인쇄하는데도 사용했습니다. –