WPF打印控件内容

当我们想打印控件内容时,如一个Grid中的内容,可以用WPF中PrintDialog类的PrintVisual()方法来实现

界面如下:

XAML代码如下

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="300"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0">

            <Grid Width="800" Name="grid1">
                <TextBlock TextWrapping="Wrap" FontSize="15">
                Control authors who want to customize the arrange pass of layout processing should override this method. The implementation pattern should call M:System.Windows.UIElement.Arrange(System.Windows.Rect) on each visible child element, and pass the final desired size for each child element as the finalRect parameter. Parent elements should call M:System.Windows.UIElement.Arrange(System.Windows.Rect) on each child, otherwise the child elements will not be rendered.
Many derived classes offer implementations of this method. Prominent ones include: M:System.Windows.Window.ArrangeOverride(System.Windows.Size), M:System.Windows.Controls.Page.ArrangeOverride(System.Windows.Size) and M:System.Windows.Controls.Control.ArrangeOverride(System.Windows.Size).
                </TextBlock>
            </Grid>
        </Grid>

        <Grid Grid.Column="1">
            <Button Height="30" VerticalAlignment="Top" Click="Button_Click">打印</Button>
        </Grid>
    </Grid>

当我们点击按钮时,进行打印

按钮事件:

           PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() == true)
            {
                pd.PrintVisual(this.grid1, "");
            }

这时我们会发现,虽然 打印的内容是指定的,但打印的大小却是整个窗体的大小,而不仅仅是指定的区域大小。

然后我们就需要用到UIElement的Arrange 方法

MSDN上的解释是

定位子元素,并确定 UIElement 的大小。 父元素从它们的 ArrangeCore 实现(或者是 WPF 框架级别等效项)调用此方法,以便形成递归布局更新。此方法产生第二次布局更新。

修改后的代码如下:

1 if (pd.ShowDialog() == true)
2             {                
3                 this.grid1.Arrange(new Rect(new Size(grid1.ActualWidth, grid1.ActualHeight)));
4                 pd.PrintVisual(this.grid1, "");               
5             }

这样操作以后,打印的大小不再是整 个窗体的大小了,但打印完之后 ,控件 的位置却发生了变化 ,这时候我们只需要再调用一次Arrange方法,将它放回原来的位置就行了

 if (pd.ShowDialog() == true)
             {               
                 Window window = Window.GetWindow(grid1);
                 Point point = grid1.TransformToAncestor(window).Transform(new Point(0, 0));//获取当前控件 的坐标
                 this.grid1.Measure(new Size(grid1.ActualWidth,grid1.ActualHeight));
                 this.grid1.Arrange(new Rect(new Size(grid1.ActualWidth, grid1.ActualHeight)));
                 pd.PrintVisual(this.grid1, "");
                 this.grid1.Arrange(new Rect(point.X, point.Y, grid1.ActualWidth, grid1.ActualHeight));//设置为原来的位置
             }

这样就可以打印控件 内容了。

如果 想对打印机进行设置,可以查找 WPF PrintDialog的使用方法,下面是简单的设置

                 PrintTicket pt = new PrintTicket();
                 PageMediaSize p = new PageMediaSize(PageMediaSizeName.ISOA4);
                 //pt.PageBorderless = PageBorderless.Unknown;
                 pt.PageMediaSize = p;
                 //pt.PageOrientation = PageOrientation.Portrait;
                 pd.PrintTicket = pt;

如果你想在 WPF打印窗口中只打印部分内容,你可以使用 `PrintDialog` 类和 `PrintVisual` 方法结合其他可视化控件来实现。下面是一个示例代码,演示如何在 WPF 中只打印部分内容: ```csharp using System.Printing; using System.Windows; using System.Windows.Controls; private void ShowPrintPreview() { PrintDialog printDialog = new PrintDialog(); // 检查是否支持打印 if (printDialog.ShowDialog() == true) { // 创建一个 StackPanel,并将部分内容添加到其中 StackPanel stackPanel = new StackPanel(); stackPanel.Children.Add(new TextBlock() { Text = "打印的部分内容" }); stackPanel.Children.Add(new Button() { Content = "Print Me" }); // 设置打印的尺寸和边距 stackPanel.Measure(new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight)); stackPanel.Arrange(new Rect(new Point(0, 0), stackPanel.DesiredSize)); // 打印预览 printDialog.PrintVisual(stackPanel, "打印预览"); } } ``` 在上面的示例代码中,首先创建了一个 `PrintDialog` 对象,并通过 `ShowDialog` 方法显示打印对话框。如果用户选择了打印选项并点击了确定按钮,就会进入下一步。 然后,创建一个 `StackPanel` 对象,并将要打印的部分内容添加到其中(在示例中是一个 `TextBlock` 和一个 `Button`)。你可以根据需要自定义内容。 接着,使用 `Measure` 和 `Arrange` 方法来设置打印的尺寸和边距。 最后,调用 `PrintVisual` 方法将 `StackPanel` 对象传递给 `PrintDialog`,并指定打印作业的名称(在示例中是 "打印预览")。 这样就可以实现在 WPF打印窗口中只打印部分内容了。请根据你的具体需求修改示例代码中的内容,并确保在调用 `PrintVisual` 方法之前进行正确的测量和布局操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值