横向打印 WPF 视觉对象,打印机仍然以纵向尺寸剪辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12772202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing a WPF visual in landscape orientation, printer still clips at portrait dimensions
提问by Jimmy
I've written a small app which creates a visual programmatically, and I'm trying to print it out on a page in landscape orientation (it clips in portrait). When I print, it does come out in landscape, but my visual is still clipped as through it were limited to portrait orientation.
我编写了一个小应用程序,它以编程方式创建了一个视觉效果,我试图将它打印在横向页面上(它以纵向剪辑)。当我打印时,它确实以横向显示,但我的视觉仍然被剪裁,因为它仅限于纵向。
Here's my code:
这是我的代码:
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");
}
After executing this, the printed content is arranged correctly but still seems to be clipped to a width of 816, cutting off a significant chunk of content. I've checked this by holding another piece of paper over the printed one and it fits perfectly inside.
执行此操作后,打印的内容已正确排列,但似乎仍被裁剪为 816 的宽度,从而截断了大量内容。我通过将另一张纸放在打印的纸上来检查过这一点,它非常适合里面。
Is there something I'm doing wrong to measure and arrange the controls? How can I get my printer to use the full space of landscape orientation?
我在测量和安排控件时做错了什么吗?如何让我的打印机使用横向的全部空间?
回答by Jimmy
Steve Py's answerwas correct for describing the core issue (PrintVisual doesn't respect the PrintTicket settings used). However after I tried using XpsDocumentWriter and a new PrintTicket, I ran into the same issue (if I set the new PrintTicket's orientation to Landscape, it was still clipped).
Steve Py 的回答对于描述核心问题是正确的(PrintVisual 不尊重使用的 PrintTicket 设置)。然而,在我尝试使用 XpsDocumentWriter 和一个新的 PrintTicket 之后,我遇到了同样的问题(如果我将新的 PrintTicket 的方向设置为横向,它仍然被剪裁了)。
Instead I worked around the issue by just setting a LayoutTransform to rotate the content 90 degrees, and print in Portrait mode. My final code:
相反,我通过设置 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");
}
回答by Steve Py
There is a known issue around landscape visual printing. This should provide details on how to address it.
横向视觉打印存在一个已知问题。这应该提供有关如何解决它的详细信息。
http://social.msdn.microsoft.com/Forums/en/wpf/thread/56fb78b1-efc2-4ff7-aa2c-73c198a790b4
http://social.msdn.microsoft.com/Forums/en/wpf/thread/56fb78b1-efc2-4ff7-aa2c-73c198a790b4
回答by Oh My Dog
try
尝试
PrintDialog printDlg = new PrintDialog();
PrintTicket pt = printDlg.PrintTicket;
pt.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA5Rotated);

