question

RezaJaferi1992 avatar image
0 Votes"
RezaJaferi1992 asked RezaJaferi1992 edited

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

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



dotnet-csharpwindows-wpfdotnet-wpf-xaml
print.png (71.6 KiB)
print-example.png (114.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

HuiLiu-MSFT avatar image
2 Votes"
HuiLiu-MSFT answered RezaJaferi1992 commented

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 to enable e-mail notifications if you want to receive the related email notification for this thread. 

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much, there is a problem when I print from right to left, Persian words are printed in reverse.
This problem is something like the image below in English (but I have this problem in Persian).
125646-r.png


0 Votes 0 ·
r.png (5.9 KiB)

Hi,@ RezaJafery. I replied on your new question, you could click here to check it.

0 Votes 0 ·

This is not a complete answer, but I accepted part of it.
I answered the second question (or the second problem) in the same link.
Thanks


0 Votes 0 ·