在横向方向上打印 WPF 可视化内容时,打印机仍然会按照纵向页面的尺寸进行裁剪。

3

我编写了一个小应用程序,可以通过编程方式创建可视化效果,并尝试在页面上横向打印它(在纵向模式下会被剪辑)。当我打印时,它确实以横向方式显示,但是我的可视化效果仍然被剪辑,好像受到纵向模式的限制。

这是我的代码:

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个回答

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

我一直在努力尝试,你的建议解决了我的问题。谢谢。 - agileDev

2

我尝试使用那里描述的方法(使用新创建的PrintTicket与XpsDocumentWriter),但遇到了基本相同的问题。 - Jimmy

0

尝试

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接