Printing Group Box without displaying Print Dialog in WPF (by C #)

رضا جافری 1,291 Reputation points
2021-08-21T03:26:20.277+00:00

First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I designed a window that you can see it in below:

125203-print.png

125168-print-example.png

I just want to print the preview box.
Suppose the printer name is Microsoft XPS Document Writer.

//number of copies  
int Copies = int.Parse(Copies_TextBox.Text.Trim());  

            <GroupBox x:Name="PrintPreview_GroupBox" Header="Preview" Width="400" Background="Transparent" HorizontalAlignment="Left" Visibility="Visible" Margin="14,228,0,14">  
            <GroupBox.Style>  
                <Style TargetType="{x:Type GroupBox}">  
                    <Setter Property="BorderBrush" Value="DarkGray"/>  
                    <Setter Property="BorderThickness" Value="1"/>  
                    <Setter Property="Template">  
                        <Setter.Value>  
                            <ControlTemplate TargetType="{x:Type GroupBox}">  
                                <Grid SnapsToDevicePixels="true">  
                                    <Grid.ColumnDefinitions>  
                                        <ColumnDefinition Width="*"/>  
                                        <ColumnDefinition Width="*"/>  
                                        <ColumnDefinition Width="*"/>  
                                        <ColumnDefinition Width="*"/>  
                                    </Grid.ColumnDefinitions>  
                                    <Grid.RowDefinitions>  
                                        <RowDefinition Height="*"/>  
                                        <RowDefinition Height="*"/>  
                                        <RowDefinition Height="*"/>  
                                        <RowDefinition Height="*"/>  
                                    </Grid.RowDefinitions>  
                                    <Border BorderBrush="DarkGray"   
                                BorderThickness="1"  
                                CornerRadius="9"  
                                Grid.ColumnSpan="4"   
                                Grid.RowSpan="4">  
                                        <Border.OpacityMask>  
                                            <MultiBinding ConverterParameter="9">  
                                                <MultiBinding.Converter>  
                                                    <BorderGapMaskConverter/>  
                                                </MultiBinding.Converter>  
                                                <Binding ElementName="Header" Path="ActualWidth"/>  
                                                <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>  
                                                <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>  
                                            </MultiBinding>  
                                        </Border.OpacityMask>  
                                    </Border>  
                                    <Border x:Name="Header" Grid.RowSpan="1" Grid.ColumnSpan="4"  Margin="9,-9.5,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">  
                                        <ContentPresenter  ContentSource="Header"  
                                              RecognizesAccessKey="True"   
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>  
                                    </Border>  
                                    <ContentPresenter  Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"   
                                          Margin="{TemplateBinding Padding}"  
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>  
                                </Grid>  
                            </ControlTemplate>  
                        </Setter.Value>  
                    </Setter>  
                </Style>  
            </GroupBox.Style>  
            <Grid>  
                <ItemsControl x:Name="PrintPreview_ItemsControl" FontSize="12" Margin="-99,-46,-99,-104">  
                    <Grid>  
                        <Image x:Name="LogoImage" Source="{Binding}" Height="50" Stretch="Fill" VerticalAlignment="Top" Width="50" Margin="174,0,174,0">  
                            <Image.Clip>  
                                <RectangleGeometry RadiusX="9" RadiusY="9" Rect="0,0,50,50"/>  
                            </Image.Clip>  
                        </Image>  
                        <TextBlock x:Name="Title_TextBlock" Text="Title" Height="30" Foreground="Gray" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Width="Auto" Margin="174,57,174,0"/>  
                        <Image x:Name="MemberImage" Source="{Binding}" Height="100" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100" Margin="7,7,0,0">  
                            <Image.Clip>  
                                <RectangleGeometry RadiusX="9" RadiusY="9" Rect="0,0,100,100"/>  
                            </Image.Clip>  
                        </Image>  
                        <Label x:Name="FullName_Label" Content="Full name:" Padding="1" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Margin="0,114,0,0"/>  
                        <Label x:Name="FullNameValue_Label" Content="value" Padding="1" Height="30" Foreground="Gray" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Margin="55,114,0,0"/>  
                        <Label x:Name="IDNumber_Label" Content="ID number:" Padding="1" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Margin="0,144,0,0"/>  
                        <Label x:Name="IDNumberValue_Label" Content="value" Padding="1" Height="30" Foreground="Gray" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Margin="60,144,0,0"/>  
                        <Label x:Name="MembershipDate_Label" Content="Membership date:" Padding="1" Height="30" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Margin="0,174,0,0"/>  
                        <Label x:Name="MembershipDateValue_Label" Content="value" Padding="1" Height="30" Foreground="Gray" HorizontalAlignment="Left" VerticalAlignment="Top" Width="Auto" Margin="97,174,0,0"/>  
                        <Label x:Name="Librarian_Label" Content="Librarian" Height="Auto" Foreground="Gray" HorizontalAlignment="Center" VerticalAlignment="Top" Width="Auto" Margin="0,144,0,0"/>  
                        <Image x:Name="QRImage" Source="{Binding}" Height="100" Stretch="Fill" HorizontalAlignment="Right" Width="100" Margin="0,85,7,0">  
                            <Image.Clip>  
                                <RectangleGeometry RadiusX="9" RadiusY="9" Rect="0,0,100,100"/>  
                            </Image.Clip>  
                        </Image>  
                    </Grid>  
                </ItemsControl>  
            </Grid>  
        </GroupBox>  

Thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,687 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,391 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
772 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 44,186 Reputation points Microsoft Vendor
    2021-08-23T08:29:46.887+00:00

    For Printing Group Box without displaying Print Dialog you could try to refer to the following code.
    I added a button and its click event:

    <Button Click="Button_Click">Print PrintPreview_GroupBox</Button>  
    

    And I added height to your PrintPreview_GroupBox:

    <GroupBox x:Name="PrintPreview_GroupBox" Header="Preview" Width="400" Height="250" Background="Transparent" HorizontalAlignment="Left" Visibility="Visible" Margin="14,228,0,14" >  
    

    The code of click event: (Here I print the GroupBox into pdf to view the result. You could change "Microsoft Print to PDF" to "Microsoft XPS Document Writer". And it prints out the .oxps file. You can refer to here to display .oxps file.)

    private void Button_Click(object sender, RoutedEventArgs e)  
        {  
          PrintDialog pDialog = new PrintDialog();  
          var printers = new LocalPrintServer().GetPrintQueues();  
          var selectedPrinter = printers.FirstOrDefault(p => p.Name == "Microsoft Print to PDF"); //Replacement string: "Microsoft XPS Document Writer"  
          if (selectedPrinter == null)  
          {  
            MessageBox.Show("not found!");  
            return;  
          }  
          pDialog.PrintQueue = selectedPrinter;  
          var size = new Size(pDialog.PrintableAreaWidth, pDialog.PrintableAreaHeight);  
          PrintPreview_GroupBox.Measure(size);  
          PrintPreview_GroupBox.Arrange(new Rect(new Point(0, 0), size));  
          pDialog.PrintVisual(PrintPreview_GroupBox, "test");  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it. 
    Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [3]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful