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

时间:2012-10-07 19:39:17

标签: wpf printing

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

这是我的代码:

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

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

有什么我在衡量和安排控件方面做错了吗?如何让我的打印机使用完整的横向空间?

3 个答案:

答案 0 :(得分:3)

Steve Py's answer对于描述核心问题是正确的(PrintVisual不遵守所使用的PrintTicket设置)。然而,在我尝试使用XpsDocumentWriter和新的PrintTicket之后,我遇到了同样的问题(如果我将新PrintTicket的方向设置为Landscape,它仍然被剪裁)。

相反,我只是通过设置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");
}

答案 1 :(得分:2)

景观视觉印刷存在一个已知问题。这应该提供有关如何解决它的详细信息。

http://social.msdn.microsoft.com/Forums/en/wpf/thread/56fb78b1-efc2-4ff7-aa2c-73c198a790b4

答案 2 :(得分:0)

PrintDialog printDlg = new PrintDialog();
PrintTicket pt = printDlg.PrintTicket;
pt.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA5Rotated);