我在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