Reverse printing problem from right to left in WPF (by C#)

رضا جافری 1,296 Reputation points
2021-08-24T03:23:15.123+00:00

First and foremost, I apologize for my grammatical errors; my first language is Persian (Iran).
I have a problem when I try to print from right to left, you can see my details and problem in this link:
printing-groupbox-without-using-printdialog-in-wpf.html
Thanks

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | 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.
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. رضا جافری 1,296 Reputation points
    2021-08-28T19:29:14.16+00:00

    I found the solution. To solve this problem, we can use ScaleTransform class.
    First: we must use this <ElementName.RenderTransform> <ScaleTransform ScaleX="{Binding}"/> <ElementName.RenderTransform/> in XAML.
    For example:

                     <Grid x:Name="PrintPreview_Grid" Height="206" Width="398" Margin="1,0,1,0">  
                       <Grid.RenderTransform>  
                         <ScaleTransform ScaleX="{Binding}"/>  
                       </Grid.RenderTransform>  
                     </Grid>  
    

    Then I used the following code for printing:

        private void PrintButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)  
        {  
            switch (App.EnumLanguage)  
            {  
                //For left to right languages  
                case AllLanguage.English:  
                    (PrintPreview_Grid.RenderTransform as ScaleTransform).ScaleX = 1;  
                    break;  
                //For right to left languages  
                default:  
                    (PrintPreview_Grid.RenderTransform as ScaleTransform).ScaleX = -1;  
                    break;  
            }  
            PrintDialog PD = new PrintDialog();  
            var Printer = new LocalPrintServer().GetPrintQueues();  
            //We can change the name of the printer we want  
            var SelectedPrinter = Printer.FirstOrDefault(P => P.Name == "Microsoft Print to PDF");  
            PD.PrintQueue = SelectedPrinter;  
            var Size = new Size(PD.PrintableAreaHeight, PD.PrintableAreaWidth);  
            PrintPreview_Grid.Measure(Size);  
            PrintPreview_Grid.Arrange(new Rect(new Point(29, 0), Size));  
            PD.PrintVisual(PrintPreview_Grid, "Print");  
            //To return to the default state  
            (PrintPreview_Grid.RenderTransform as ScaleTransform).ScaleX = 1;  
        }  
    

    Result for left-to-right languages:
    127352-left-to-right.gif
    Result for right-to-left languages:
    127361-right-to-left.gif
    Thanks

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,706 Reputation points Microsoft External Staff
    2021-08-25T02:01:08.957+00:00

    Your print result is the same as the window display. To print the result from right to left, we can set the window content from right to left.
    I added FlowDirection="RightToLeft" to the GroupBox and the code is as follows:
    The updated code is as follows:

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

    The print result is as shown in the figure:
    126221-2.png

    For the flow direction of specific strings, you could refer to here.


    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

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.