簡體   English   中英

WPF DocumentPaginator:將 DocumentPage 大小更改為橫向

[英]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. (對於 DocumentPaginator,我使用此處描述的方法: WPF - 打印視覺時的分頁

錯在哪里? 除非格式問題它完美無缺。 如何調整 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM