WPF打印控件内容

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

界面如下:

XAML代码如下

XML 复制代码
<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方法,将它放回原来的位置就行了

cs 复制代码
 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的使用方法,下面是简单的设置

cs 复制代码
                 PrintTicket pt = new PrintTicket();
                 PageMediaSize p = new PageMediaSize(PageMediaSizeName.ISOA4);
                 //pt.PageBorderless = PageBorderless.Unknown;
                 pt.PageMediaSize = p;
                 //pt.PageOrientation = PageOrientation.Portrait;
                 pd.PrintTicket = pt;
相关推荐
分布式存储与RustFS21 小时前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU1 天前
WPF 绑定机制实现原理
wpf
攻城狮CSU1 天前
WPF 之数据绑定一(Data Binding)
wpf
wuty0072 天前
记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题
wpf·进程间通信·sendmessage·进程权限
c#上位机2 天前
wpf之ToggleButton控件
c#·wpf
浪扼飞舟3 天前
WPF用户控件和依赖属性
wpf
c#上位机3 天前
wpf之MVVM中只读属性更新界面
c#·wpf·mvvm
就是有点傻3 天前
WPF自定义控件之环形进度条
wpf
He BianGu3 天前
【笔记】WPF中如何的动态设置DataGridTextColumn是否显示
笔记·wpf
Pota-to成长日记3 天前
Redisson 看门狗机制深度解析:分布式锁的守护者
分布式·wpf