横向打印 WPF 视觉对象,打印机仍然以纵向尺寸剪辑

Jim*_*mmy 3 printing wpf

我编写了一个小应用程序,它以编程方式创建了一个视觉效果,我试图将它打印在横向页面上(它以纵向剪辑)。当我打印时,它确实以横向方式出现,但我的视觉仍然被剪裁,因为它仅限于纵向。

这是我的代码:

StackPanel page = new StackPanel();
// ... generate stuff to the page to create the visual

PrintDialog dialog = new PrintDialog(); // System.Windows.Controls.PrintDialog
bool? result = dialog.ShowDialog();
if(result.HasValue && result.Value)
{
    dialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
    Size pageSize = new Size { Width = dialog.PrintableAreaWidth, 
        Height = dialog.PrintableAreaHeight };
    // pageSize comes out to {1056, 816}, which is the orientation I expect
    page.Measure(pageSize); 
    // after this, page.DesiredSize is e.g. {944, 657}, wider than portrait (816).
    page.UpdateLayout();
    dialog.PrintVisual(page, "Job description");
}
Run Code Online (Sandbox Code Playgroud)

执行此操作后,打印的内容已正确排列,但似乎仍被裁剪为 816 的宽度,从而切断了大量内容。我通过将另一张纸放在打印的纸上来检查过这一点,它非常适合里面。

我在测量和安排控件时做错了什么吗?如何让我的打印机使用横向的全部空间?

Jim*_*mmy 5

Steve Py 的回答对于描述核心问题是正确的(PrintVisual 不尊重使用的 PrintTicket 设置)。然而,在我尝试使用 XpsDocumentWriter 和一个新的 PrintTicket 之后,我遇到了同样的问题(如果我将新的 PrintTicket 的方向设置为横向,它仍然被剪裁了)。

相反,我通过设置 LayoutTransform 将内容旋转 90 度并以纵向模式打印来解决这个问题。我的最终代码:

StackPanel page = new StackPanel();
// ... generate stuff to the page to create the visual
// rotate page content 90 degrees to fit onto a landscape page
RotateTransform deg90 = new RotateTransform(90);
page.LayoutTransform = deg90;

PrintDialog dialog = new PrintDialog();
bool? result = dialog.ShowDialog();
if (result.HasValue && result.Value)
{
    Size pageSize = new Size { Height = dialog.PrintableAreaHeight, Width = dialog.PrintableAreaWidth };
    page.Measure(pageSize);
    page.UpdateLayout();
    dialog.PrintVisual(page, "Bingo Board");
}
Run Code Online (Sandbox Code Playgroud)