3

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.

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?

Jimmy
  • 27,142
  • 5
  • 87
  • 100

3 Answers3

5

Steve Py's answer was 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).

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:

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");
}
Community
  • 1
  • 1
Jimmy
  • 27,142
  • 5
  • 87
  • 100
2

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

Steve Py
  • 26,149
  • 3
  • 25
  • 43
  • I tried using the method described there (using XpsDocumentWriter with a newly created PrintTicket), but ran into mostly the same issue. – Jimmy Oct 08 '12 at 07:03
0

try

PrintDialog printDlg = new PrintDialog();
PrintTicket pt = printDlg.PrintTicket;
pt.PageMediaSize = new PageMediaSize(PageMediaSizeName.ISOA5Rotated);
Oh My Dog
  • 781
  • 13
  • 32