wpfprintingdocumentpaginatordocumentpage

WPF DocumentPaginator: Change DocumentPage Size to landscape


I try to print out a WPF Window as a pdf file (Microsoft Print to pdf) with the DocumentPaginator in A4 landscape format (Size(96 * 11, 96 * 8.5)) but always get it in A4 portrait format. (For the DocumentPaginator I use the method as described here: WPF - pagination when printing a visual)

Where is the fault? Unless the format problem it works perfect. How can I resize the Size of the DocumentPage as it is written in the Documentation of the DocumentPage.Size Property: "Remarks...Use the protected SetSize method to set this property."?

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
    <Grid>
        <Button x:Name="but_print" HorizontalAlignment="Center" VerticalAlignment="Top" Width="35" Height="35" Padding="0" Margin="0,5,10,5" BorderThickness="0" BorderBrush="#FFDDDDDD"  Background="{x:Null}" VerticalContentAlignment="Top" Click="InvokePrint">
            <Border  x:Name="bor_print" CornerRadius="5,5,5,5" Height="Auto" Width="Auto" BorderThickness="2" Padding="1" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="#FFDDDDDD" ScrollViewer.VerticalScrollBarVisibility="Auto">
                <Image x:Name="image23" Source="C:\Users\RB\Documents\Visual Studio 2015\Projects\WpfApplication1\WpfApplication1\Icons\Drucken.png"/>
            </Border>
        </Button>
        <ScrollViewer x:Name="scrollv1" Grid.Column="0" Grid.Row="2" VerticalScrollBarVisibility="Auto"  HorizontalScrollBarVisibility="Auto" Margin="0,0,0,0" Height ="Auto" Width ="Auto" Padding="0" VerticalContentAlignment="Stretch" HorizontalAlignment="Left">
            <StackPanel x:Name="grdMain" Margin="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                <TextBlock x:Name="textBlock1" HorizontalAlignment="Right" Text="Test" TextWrapping="Wrap" FontSize="24" FlowDirection="RightToLeft" Margin="0,0,0,0"/>
            </StackPanel>
        </ScrollViewer>
    </Grid>
</Window>

C#:

  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void InvokePrint(object sender, RoutedEventArgs e)
        {
            var dlg = new PrintDialog();
            if ((bool)dlg.ShowDialog())
            {
                Size lv_size = new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight);
                FixedPagePaginator fixedpagepagi = new FixedPagePaginator(scrollv1, lv_size);

                fixedpagepagi.PageSize = new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight);
                dlg.PrintDocument(fixedpagepagi, "Test");
            }
        }
    }

    class FixedPagePaginator : DocumentPaginator
    {
        private FrameworkElement Element;
        private Size Size;
        private FixedPagePaginator()
        {
        }

        public FixedPagePaginator(FrameworkElement element, Size size)
        {
            Element = element;
            Size = size;
        }

        public override DocumentPage GetPage(int pageNumber)
        {
            Size elementSize = new Size(
                Element.ActualWidth,
                Element.ActualHeight);
            Element.Measure(elementSize);
            Element.Arrange(new Rect(new Point(0, 0), elementSize));

            Element.Measure(Size);
            Element.Arrange(new Rect(Size));

            var page = new DocumentPage(Element);
            return page;
        }

        public override bool IsPageCountValid
        {
            get { return true; }
        }

        public int Columns
        {
            get
            {
                return 1;
            }
        }
        public int Rows
        {
            get
            {
                return (int)Math.Ceiling(Element.ActualHeight / PageSize.Height);
            }
        }

        public override int PageCount
        {
            get
            {
                return Columns * Rows;
            }
        }

        public override System.Windows.Size PageSize
        {
            get
            {
                return Size;
            }

            set
            {
                if (Size.Equals(value) != true)
                {
                    Size = value;
                }
            }
        }

        public override IDocumentPaginatorSource Source
        {
            get { return null; }
        }
    }

Solution

  • The working solution I got in the CSharp Forum is to initialize the DocumentPage with the PageSize like this for example:

    private DocumentPage CreatePage(Grid grid)
    {
        var box = this.SizingPageGrid(grid);
    
        return new DocumentPage(grid, this.PageSize, box, box);
    }
    
    private Rect SizingPageGrid(Grid grid)
    {
        var box = new Rect(new Point(0, 0), this.PageSize);
    
        grid.InvalidateArrange();
        grid.UpdateLayout();
        grid.Measure(this.PageSize);
        grid.Arrange(box);
    
        return box;
    }