WPF DocumentPaginator:将 DocumentPage 大小更改为横向

WPF DocumentPaginator: Change DocumentPage Size to landscape

我尝试使用 DocumentPaginator 以 A4 横向格式(大小(96 * 11、96 * 8.5))将 WPF Window 打印为 pdf 文件(Microsoft Print to pdf),但总是能得到它A4 纵向格式。 (对于 DocumentPaginator,我使用此处描述的方法:WPF - pagination when printing a visual

错在哪里?除非格式问题它工作完美。 如何调整 DocumentPage 的大小,正如 DocumentPage.Size 属性 的文档中所写:“备注...使用受保护的 SetSize 方法来设置此 属性。”?

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

我在 CSharp 论坛上找到的有效解决方案是使用 PageSize 初始化 DocumentPage,例如:

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