1

I have a user control which I need to print. The form is just a basic filled out form. I have the controls all placed in a stackpanel which is named and the button for printing is outside the stackpanel, so the button is not printed with.

<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>

You can mind all the MVVM loading, that is done sepratly. Anyway, using code behind, I basically add this to my button click:

PrintDialog prnt = new PrintDialog();

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

This prints out my form (stackpanel), but it is not exactly centered on the page. It sits at the top left, and some of the text is cut off on the left side.

Using the PrintDialog methods, how does one set the size of the page to be printed, or just center the contents of the printable form? And also, how can I set the page orientation from PrintDialog? Do I have to set the PrintableAreaHeight/Width, and how do I do so?

Nicholas Aysen
  • 568
  • 3
  • 18
  • 40

1 Answers1

4

I got it.

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

is for a potrait screen. sizes may change depending on the amount needed to print

and

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

is for landscape. again, size changes when needed

Nicholas Aysen
  • 568
  • 3
  • 18
  • 40