WPF 打印界面窗口内容

目前在做个测试项目,需要把窗口显示出来的内容全部打印出来,打印大小以图片model.jpg为准,因窗口显示内容较多,所以有滚动条,采用滚动控件,加Cavans,将显示内容加到容器里,代码如下

 

窗口界面代码:

<Window x:Class="ocr识别演示.BussLiecence"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="BussLiecence" FontSize="8" Height="768" Width="1366" WindowState="Maximized" WindowStyle="None" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="135*"/>
            <ColumnDefinition Width="405*"/>
            <ColumnDefinition Width="65*"/>
            <ColumnDefinition Width="62*"/>
            <ColumnDefinition Width="12*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="9*"/>
            <RowDefinition Height="160*"/>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="11*"/>
            <RowDefinition Height="7*"/>
        </Grid.RowDefinitions>
        <ScrollViewer  Grid.Column="1" Grid.Row="1" >
            <Canvas x:Name="grid" Width="{Binding ActualWidth, ElementName=image, Mode=OneWay}" Height="{Binding ActualHeight, ElementName=image, Mode=OneWay}">
                <Image x:Name="image" Source="image/model.jpg" />
                <TextBox x:Name="Scope" HorizontalAlignment="Left" BorderThickness="0" Height="34" TextWrapping="Wrap" Text="经营范围" VerticalAlignment="Top" Width="554" Foreground="Red" Canvas.Left="169" Canvas.Top="241" VerticalContentAlignment="Center"/>
                <TextBox x:Name="RegisteredFund" BorderThickness="0" HorizontalAlignment="Left" Height="15" TextWrapping="Wrap" Text="注册资金" VerticalAlignment="Top" Width="97" Foreground="Red" Canvas.Left="369" Canvas.Top="223" VerticalContentAlignment="Center"/>
                <TextBox x:Name="Name" BorderThickness="0" HorizontalAlignment="Left" Height="15" TextWrapping="Wrap" Text="名称" VerticalAlignment="Top" Width="554" Foreground="Red" Canvas.Left="169" Canvas.Top="111" VerticalContentAlignment="Center"/>
                <TextBox x:Name="Address" BorderThickness="0" HorizontalAlignment="Left" Height="15" TextWrapping="Wrap" Text="地址" VerticalAlignment="Top" Width="396" Foreground="Red" Canvas.Left="169" Canvas.Top="130" VerticalContentAlignment="Center"/>
                <TextBox x:Name="Type" BorderThickness="0" HorizontalAlignment="Left" Height="15" TextWrapping="Wrap" Text="类别" VerticalAlignment="Top" Width="396" Foreground="Red" Canvas.Left="169" Canvas.Top="148" VerticalContentAlignment="Center"/>
                <TextBox x:Name="RegNum" BorderThickness="0" HorizontalAlignment="Left" Height="15" TextWrapping="Wrap" Text="注册号" VerticalAlignment="Top" Width="554" Foreground="Red" Canvas.Left="169" Canvas.Top="296" VerticalContentAlignment="Center"/>
                <TextBox x:Name="textBox" Height="59" Canvas.Left="320" TextWrapping="Wrap" Text="TextBox" Canvas.Top="716" Width="195"/>
                <TextBox x:Name="textBox1" Height="19" Canvas.Left="169" TextWrapping="Wrap" Text="TextBox" Canvas.Top="651" Width="297"/>
            </Canvas>
        </ScrollViewer>
     
        <Button x:Name="button1"  Grid.Column="2"  Grid.Row="3"  Click="button1_Click" >
            <Button.Background>
                <ImageBrush ImageSource="image/btPrint.png"/>
            </Button.Background>
        </Button>
    </Grid>

</Window>

窗口后台代码:

namespace ocr识别演示
{
    /// <summary>
    /// BussLiecence.xaml 的交互逻辑
    /// </summary>
    public partial class BussLiecence : Window
    {
        public BussLiecence()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //this.grid.Measure(new Size(1075, 1520));
            this.grid.Arrange(new Rect(new Point(0, 0), new Size(grid.ActualWidth, grid.ActualHeight)));

            PrintDialog printDialog = new PrintDialog();
            printDialog.PrintVisual(this.grid, "申请书");
        }
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 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、付费专栏及课程。

余额充值