WPF/Silverlight:视觉效果的打印 - 缺点 [英] WPF/Silverlight: Printing of Visuals - Drawbacks

查看:73
本文介绍了WPF/Silverlight:视觉效果的打印 - 缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过一两次,从 WPF/Silverlight 打印视觉效果有一些缺点,所以人们通常倾向于使用 FlowDocument 或 FixedDocument 进行打印.

I read once or twice that printing visuals from WPF/Silverlight has some drawbacks so people usually tend to use FlowDocument or FixedDocument for printing.

我想打印一些图形密集的仪表板,直接打印视觉效果似乎是最简单的方法.我不必关心分页,因为我想打印的每个仪表板都应该适合一页.

I want to print some graphically intense dashboards and printing the visual directly seems to be the easiest way to go. I don't have to care about pagination since every dashboard I want to print is supposed to fit on one page.

在选择这种打印方式之前,我还必须考虑哪些缺点吗?

Are there still drawbacks I must consider before choosing this way of printing?

推荐答案

您可以打印 Visual 对象,方法是将它们托管在 FrameworkElement 中,然后将 FrameworkElement 添加到 FixedDocument 作为 FixedPage 的内容.可视主机如下所示:

You can print Visual objects by hosting them in a FrameworkElement and then adding the FrameworkElement to a FixedDocument as content of a FixedPage. The Visual host looks like this:

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

然后你可以添加它们并像这样打印它们:

Then you can add them and print them like this:

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

这篇关于WPF/Silverlight:视觉效果的打印 - 缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆