2012-10-07 50 views
3

我已經編寫了一個小應用程序,它以編程方式創建視覺效果,並且我試圖在橫向打印頁面(它以縱向剪輯)。當我印刷時,它確實出現在風景中,但我的視覺仍然被剪裁,因爲它僅限於縱向。在橫向打印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"); 
} 

執行在此之後,印刷的內容被正確地佈置,但似乎仍然被限幅到的816的寬度,切斷的內容的顯著塊。我已經通過在印刷品上拿着另一張紙來檢查它,並且它完全適合在裏面。

有沒有什麼我做錯了測量和安排控制?如何讓我的打印機使用橫向全景空間?

回答

3

Steve Py's answer是用於描述的核心問題(PrintVisual不尊重所使用的打印標籤設置)正確。然而,在我嘗試使用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"); 
} 
0

嘗試

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