Skip to main content
 首页 » 编程设计

c#之将用户控件打印到 A4 页面的中心

2024年02月06日18jyk

我有一个需要打印的用户控件。 该表格只是一个基本的填写表格。我将所有控件都放在一个命名的堆栈面板中,用于打印的按钮位于堆栈面板的外部,因此未打印该按钮。

<UserControl x:Class="*****.PrintInitiateRequestFormView" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit" 
         mc:Ignorable="d" 
         Style="{StaticResource TransitionAnimation}"> 
<ScrollViewer> 
    <Border Style="{StaticResource PanelWindowBackground}"> 
        <Grid> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="Auto" /> 
            </Grid.ColumnDefinitions> 
            <Grid.RowDefinitions> 
                <RowDefinition Height="Auto" /> 
                <RowDefinition Height="Auto" /> 
            </Grid.RowDefinitions> 
            <StackPanel x:Name="PrntFrm" 
                        Grid.Row="0"> 
                <Border Grid.Row="1" 
                        Grid.Column="1" 
                        Margin="3,3,3,0" 
                        VerticalAlignment="Stretch" 
                        HorizontalAlignment="Stretch" 
                        Style="{StaticResource PanelWindowBackground}"> 
                    <Grid> 
                        <Grid.RowDefinitions> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                            <RowDefinition Height="Auto" /> 
                        </Grid.RowDefinitions> 
                        <Grid.ColumnDefinitions> 
                            <ColumnDefinition Width="Auto" /> 
                            <ColumnDefinition Width="200" /> 
                            <ColumnDefinition Width="200" /> 
                            <ColumnDefinition Width="200" /> 
                            <ColumnDefinition /> 
                        </Grid.ColumnDefinitions> 
                        <Image Source="/Lamacs_8;component/Resources/Images/NMBM_Logo.jpg" 
                               Stretch="Fill" 
                               Grid.ColumnSpan="2" 
                               Margin="0,0,74,0" 
                               Width="265"> 
 
                        </Image> 
                        <Label Grid.Row="1" 
                               Grid.ColumnSpan="4" 
                               HorizontalAlignment="Center" 
                               Content="WORK REQUEST" 
                               FontSize="20" /> 
 
                        <Label Grid.Row="2" 
                               Grid.ColumnSpan="2" 
                               Content="Call Centre" 
                               FontSize="20"> 
 
                        </Label> 
 
                        <Label Grid.Row="2" 
                               Grid.Column="2" 
                               FontSize="20" 
                               Content="Reference Number:" /> 
 
                        <Label Grid.Row="2" 
                               Grid.Column="3" 
                               FontSize="16" 
                               Content="{Binding RefNo}" /> 
 
                        <Label Grid.Row="3" 
                               Grid.Column="2" 
                               FontSize="16" 
                               Content="Date &amp; Time Logged:" /> 
 
                        <Label Grid.Column="3" 
                               Grid.Row="3" 
                               FontSize="16" 
                               Content="{Binding CaptureDate}" /> 
 
                        <Label Grid.Row="4" 
                               Grid.Column="0" 
                               Content="Building:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="4" 
                               Grid.Column="1" 
                               Content="{Binding LandObjectBuilding}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="5" 
                               Grid.Column="0" 
                               Content="Address:" 
                               FontSize="16" /> 
 
                        <Label Grid.Column="1" 
                               Grid.Row="5" 
                               FontSize="16" 
                               Grid.ColumnSpan="2" 
                               Content="{Binding LandObjectAddress}" /> 
 
                        <Label Grid.Row="6" 
                               Grid.Column="0" 
                               Content="Caller:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="6" 
                               Grid.Column="1" 
                               Content="{Binding CallerName}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="6" 
                               Grid.Column="2" 
                               Content="Telephone:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="6" 
                               Grid.Column="3" 
                               Content="{Binding CallerTel}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="7" 
                               Grid.Column="0" 
                               Content="Problem Description:" 
                               FontSize="16" /> 
 
                        <TextBox Grid.Row="7" 
                                 Grid.Column="1" 
                                 Grid.ColumnSpan="3" 
                                 Height="50" 
                                 TextWrapping="Wrap" 
                                 Text="{Binding ProblemDesc}" 
                                 FontSize="16" 
                                 Background="Transparent" 
                                 BorderBrush="Transparent" /> 
 
                        <Label Grid.Row="8" 
                               Grid.Column="2" 
                               Content="Person Responsible:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="8" 
                               Grid.Column="3" 
                               Content="{Binding Assistant}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="8" 
                               Grid.Column="0" 
                               Content="Sector" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="8" 
                               Grid.Column="1" 
                               Content="{Binding Sector}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="9" 
                               Grid.Column="0" 
                               Content="Component" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="9" 
                               Grid.Column="1" 
                               Content="{Binding Component}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="9" 
                               Grid.Column="2" 
                               Content="Element" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="9" 
                               Grid.Column="3" 
                               Content="{Binding Element}" 
                               FontSize="16" /> 
 
                        <Separator Grid.Row="11" 
                                   Grid.ColumnSpan="4" /> 
 
                        <Label Grid.Row="12" 
                               Grid.Column="0" 
                               Content="Official:" 
                               FontSize="20" /> 
 
                        <Label Grid.Row="13" 
                               Grid.Column="0" 
                               Content="Scope Of Work:" 
                               FontSize="16" /> 
 
                        <TextBox Grid.Row="13" 
                                 Grid.Column="1" 
                                 Grid.ColumnSpan="3" 
                                 FontSize="16" 
                                 TextWrapping="Wrap" 
                                 Text="{Binding ScopeOfWork}" 
                                 Background="Transparent" 
                                 BorderBrush="Transparent" /> 
 
                        <Label Grid.Row="14" 
                               Grid.Column="0" 
                               Content="Cost Estimate:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="14" 
                               Grid.Column="1" 
                               FontSize="16" 
                               Content="{Binding CostEstimate}" /> 
 
                        <Label Grid.Row="14" 
                               Grid.Column="2" 
                               Content="Name:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="14" 
                               Grid.Column="3" 
                               FontSize="16" 
                               Content="{Binding ReportName}" /> 
 
                        <Label FontSize="16" 
                               Grid.Row="15" 
                               Grid.Column="0" 
                               Content="Date:" /> 
                        <Label Grid.Row="15" 
                               Grid.Column="1" 
                               Content="{Binding FormDate}" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="16" 
                               Grid.Column="0" 
                               Grid.ColumnSpan="4" 
                               Content="(Obtain Quotes if required)" 
                               FontSize="16" 
                               HorizontalAlignment="Center" /> 
 
                        <Separator Grid.Row="17" 
                                   Grid.ColumnSpan="4" /> 
 
                        <Label FontSize="20" 
                               Content="Admin" 
                               Grid.Column="0" 
                               Grid.Row="18" /> 
 
                        <Label FontSize="16" 
                               Content="Funds Available:" 
                               Grid.Column="0" 
                               Grid.Row="19" /> 
 
                        <Label FontSize="16" 
                               Content="Yes" 
                               Grid.Column="1" 
                               Grid.Row="19" /> 
 
                        <Label FontSize="16" 
                               Content="No" 
                               Grid.Column="1" 
                               Grid.Row="19" 
                               Margin="50,0,0,0" /> 
 
                        <Label Grid.Column="2" 
                               Grid.Row="19" 
                               Content="Vote/ISO Number:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="19" 
                               Grid.Column="3" 
                               FontSize="16" 
                               Content="{Binding VISONum}" /> 
 
                        <Separator Grid.ColumnSpan="4" 
                                   Grid.Row="20" /> 
 
                        <Label Grid.Row="21" 
                               Grid.Column="0" 
                               Content="Official:" 
                               FontSize="20" /> 
 
                        <Label Grid.Row="22" 
                               Grid.Column="0" 
                               Content="Contractor:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="22" 
                               Grid.Column="1" 
                               Grid.ColumnSpan="2" 
                               FontSize="16" 
                               Content="{Binding Contractor}" /> 
 
                        <Label Grid.Row="23" 
                               Grid.Column="0" 
                               Content="Order No:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="23" 
                               Grid.Column="1" 
                               FontSize="16" 
                               Content="{Binding OrderNo}" /> 
 
                        <Label Grid.Row="23" 
                               Grid.Column="2" 
                               Content="Date Issued:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="23" 
                               Grid.Column="3" 
                               Content="{Binding DateIssued}" 
                               FontSize="16" /> 
 
                        <Label Grid.Column="0" 
                               Grid.Row="24" 
                               FontSize="16" 
                               Content="Cost of Works:" /> 
 
                        <Label Grid.Row="24" 
                               Grid.Column="1" 
                               FontSize="16" 
                               Content="{Binding CostWorks}" /> 
 
                        <Label Content="Variance:" 
                               FontSize="16" 
                               Grid.Row="24" 
                               Grid.Column="2" /> 
 
                        <Label Grid.Row="24" 
                               Grid.Column="3" 
                               FontSize="16" 
                               Content="{Binding Variance}" /> 
 
                        <Label Grid.Column="0" 
                               Grid.Row="25" 
                               FontSize="16" 
                               Content="Reason:" /> 
 
                        <Label Grid.Row="25" 
                               Grid.Column="1" 
                               Grid.ColumnSpan="3" 
                               FontSize="16" 
                               Content="{Binding Reason}" /> 
 
                        <Separator Grid.ColumnSpan="4" 
                                   Grid.Row="26" /> 
                        <Label Grid.Row="27" 
                               Grid.Column="0" 
                               Grid.ColumnSpan="3" 
                               Content="I hereby declare that the cost of the works is fair and reasonable." 
                               FontSize="16" /> 
 
                        <Label Grid.Row="28" 
                               Grid.Column="0" 
                               Content="Name:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="28" 
                               Grid.Column="1" 
                               Grid.ColumnSpan="3" 
                               VerticalContentAlignment="Bottom" 
                               FontSize="16" 
                               Height="40" 
                               Content="......................................................................................................................" /> 
 
                        <Label Grid.Row="29" 
                               Grid.Column="0" 
                               Content="Date:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="29" 
                               Grid.Column="1" 
                               Grid.ColumnSpan="3" 
                               VerticalContentAlignment="Bottom" 
                               FontSize="16" 
                               Height="40" 
                               Content="......................................................................................................................" /> 
 
                        <Label Grid.Row="30" 
                               Grid.Column="0" 
                               Content="Signature:" 
                               FontSize="16" /> 
 
                        <Label Grid.Row="30" 
                               Grid.Column="1" 
                               Grid.ColumnSpan="3" 
                               VerticalContentAlignment="Bottom" 
                               FontSize="16" 
                               Height="40" 
                               Content="......................................................................................................................" /> 
                    </Grid> 
                </Border> 
            </StackPanel> 
            <StackPanel Grid.Row="1" 
                        Orientation="Horizontal"> 
                <Button Width="150" 
                        Height="25" 
                        Margin="4,0,0,5" 
                        HorizontalAlignment="Left" 
                        Template="{StaticResource PrintButtonTemplate}" 
                        Click="Button_Click" /> 
                <Button Width="150" 
                        Height="25" 
                        Margin="4,0,0,5" 
                        HorizontalAlignment="Left" 
                        Template="{StaticResource CloseButtonTemplate}" 
                        x:Name="CloseButton" 
                        Click="CloseButton_Click" /> 
            </StackPanel> 
        </Grid> 
    </Border> 
</ScrollViewer> 

您可以介意所有 MVVM 加载,这是单独完成的。 无论如何,使用隐藏代码,我基本上将其添加到我的按钮点击中:

PrintDialog prnt = new PrintDialog(); 
 
prnt.PrintVisual(PrntFrm, "Initiate Work Request"); 

这会打印出我的表单(stackpanel),但它并不完全位于页面的中央。它位于左上角,一些文本在左侧被截断。

使用 PrintDialog 方法,如何设置要打印的页面大小,或者只是将可打印表单的内容居中?还有,如何从 PrintDialog 设置页面方向? 我是否必须设置 PrintableAreaHeight/Width,我该怎么做?

请您参考如下方法:

我明白了。

PrintDialog prnt = new PrintDialog(); 
        if (prnt.ShowDialog() == true) 
        { 
            Size pageSize = new Size(prnt.PrintableAreaWidth - 30, prnt.PrintableAreaHeight - 30); 
            PrntFrm.Measure(pageSize); 
            PrntFrm.Arrange(new Rect(15, 15, pageSize.Width, pageSize.Height)); 
            prnt.PrintVisual(PrntFrm, "Work Request"); 
        } 

用于肖像屏幕。尺寸可能会根据需要打印的数量而变化

和

PrintDialog prnt = new PrintDialog(); 
        if (prnt.ShowDialog() == true) 
        { 
            prnt.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape; 
            Size pageSize = new Size(prnt.PrintableAreaWidth + 30, prnt.PrintableAreaHeight + 300); 
            PrntFrm.Measure(pageSize); 
            PrntFrm.Arrange(new Rect(15, 15, pageSize.Height, pageSize.Width)); 
            prnt.PrintVisual(PrntFrm, "Job Card"); 
        } 

用于横向。再次,尺寸在需要时改变