2011-10-27 38 views
1

我在C#中打印自定義頁面。當實際打印文檔時,它可以正常工作,就像在對話框中顯示它一樣(通過相同的代碼)。當代碼用於PrintPreview時,對話框以橫向模式顯示頁面,但創建的Graphics具有縱向文檔的尺寸,因此預覽顯示不正確。這裏是代碼的削減版本,我使用從PrintPreview創建的圖形是縱向而不是橫向?

using (PrintDocument pd = new PrintDocument()) 
{ 
    pd.PrinterSettings.PrintToFile = false; 
    pd.DefaultPageSettings.Landscape = true; 
    pd.PrinterSettings.DefaultPageSettings.Landscape = true; 
    pd.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape = true; 

    PrintDialog pDialog = new PrintDialog(); 
    pDialog.Document = pd; 
    pDialog.PrinterSettings.DefaultPageSettings.Landscape = true; 
    pDialog.PrinterSettings.PrintToFile = false; 
    pDialog.Document.DefaultPageSettings.Landscape = true; 

    PrintPreviewDialog printPreview = new PrintPreviewDialog(); 

    printPreview.Document = pd; 
    printPreview.ShowDialog(); 
} 

然後當PrintPreview對話框要求打印的Print_Me函數被調用:

private void Print_Me(object sender, PrintPageEventArgs e) 
{ 
    using (Graphics g = e.Graphics) 
    {  
     DrawToDC(g); 
     e.HasMorePages = hasMorePages; 
    } 
} 

DrawToDC我使用以下方法來獲得,其尺寸,正如我所提到的,對於真正的打印和顯示對話框來說工作正常:

dc.VisibleClipBounds.Width 
dc.VisibleClipBounds.Height 

回答

4

我有完全相同的iss你最終找到了這個。添加一個OnQueryPageSettings委託處理程序。

void OnQueryPageSettings(object obj,QueryPageSettingsEventArgs e) 
{ 
    if (e.PageSettings.PrinterSettings.LandscapeAngle != 0) 
     e.PageSettings.Landscape = true;    
} 

和你的PrintDocument

prnDoc.QueryPageSettings + =新QueryPageSettingsEventHandler(OnQueryPageSettings);

已經爲我修好了。

1

我有完全相同的問題。但是,如果我繪製了正確的寬度和高度的頁面內容(即交換它們),一切正常。

int width = dc.VisibleClipBounds.Width; 
int height = dc.VisibleClipBounds.Height; 
if(width < height) 
{ 
    int temp = width; 
    width = height; 
    height = temp; 
} 

然後根據寬度和高度繪製頁面內容。

不是最好的解決方案,但確保我們總是繪製到橫向頁面。

相關問題