2014-10-29 61 views
1

我有我需要打印的用戶控件。 表格只是一個基本的填寫表格。我把所有的控件放置在一個名爲的堆棧面板中,並且打印的按鈕位於堆棧面板之外,所以按鈕不打印。將用戶控件打印到A4頁面的中心

<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負載,即sepratly完成。 總之,使用後面的代碼,我基本內容添加到我的按鈕點擊:

PrintDialog prnt = new PrintDialog(); 

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

這打印出自己的狀態(StackPanel中),但它是不完全在頁面上居中。它位於左上角,一些文字在左側被切斷。

使用PrintDialog方法,如何設置要打印頁面的大小,或者只打印可打印表格的內容?另外,我如何從PrintDialog設置頁面方向? 我必須設置PrintableAreaHeight/Width,我該怎麼做?

回答

2

我明白了。

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"); 
     } 

是一個potrait屏幕。尺寸可以根據打印

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"); 
     } 

爲風景所需要的量而改變。再次,需要時更改尺寸