WPF文档分页器:将文档页面大小更改为横向



我尝试用DocumentPaginator以A4横向格式(大小(96*11,96*8.5((将WPF窗口打印为pdf文件(Microsoft打印为pdf(,但总是以A4纵向格式打印。(对于DocumentPaginator,我使用这里描述的方法:WPF-打印视觉时分页(

故障在哪里?除非格式有问题,否则它会完美工作。我如何调整DocumentPage的大小,因为它写在DocumentPage的文档中。大小属性:";备注。。。使用受保护的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:UsersRBDocumentsVisual Studio 2015ProjectsWpfApplication1WpfApplication1IconsDrucken.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;
}

相关内容

  • 没有找到相关文章

最新更新