2013-03-20 51 views
1

我在打印PrintDocument和PrintPreviewDialog類來學習它們,但我似乎無法讓文檔在發送到打印機時打印任何東西,也沒有任何東西在PrintPreviewDialog中繪製,甚至沒有白色的空白頁面,都是灰色的。我嘗試過改變邊距,畫出位置,畫巨型矩形等等,沒有出現。我對GDI +不是很熟悉,所以我相信我錯過了一些東西,但我找不到。PrintDocument/PrintPreviewDialog Nothing Draws or Prints

這裏是我使用的相關代碼:

//Print 
private void menuPrint_click(Object source, EventArgs e) 
{ 
    printDocumentPage = 0; 

    printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage); 
    printDocument1.BeginPrint += new System.Drawing.Printing.PrintEventHandler(printDocument1_BeginPrint); 

    printPreviewDialog1.Document = printDocument1; 
    printPreviewDialog1.ShowDialog(); 
} 

private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) 
{ 
    // Save our print action so we know if we are printing 
    // a preview or a real document. 
    printAction = e.PrintAction; 

    // Set some preferences, our method should print a box with any 
    // combination of these properties being true/false. 
    printDocument1.OriginAtMargins = false; 

    if (printAction != PrintAction.PrintToPreview) 
    { 
     PrintDialog printDlg = new PrintDialog(); 
     printDocument1.DocumentName = "Print Document Simple Text"; 
     printDlg.Document = printDocument1; 

     // Show printer dialog 
     if (printDlg.ShowDialog() == DialogResult.OK) 
     { 
      printDocument1.PrinterSettings = printDlg.PrinterSettings; 
     } 
     else 
     { 
      e.Cancel = true; 
     } 
    } 
} 

void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
{ 
    Graphics g = e.Graphics; 

    RectangleF marginBounds = e.MarginBounds; 

    RectangleF printableArea = e.PageSettings.PrintableArea; 

    //if (printAction == PrintAction.PrintToPreview) 
     //g.TranslateTransform(printableArea.X, printableArea.Y); 

    int availableWidth = (int)Math.Floor(printDocument1.OriginAtMargins ? marginBounds.Width : (e.PageSettings.Landscape ? printableArea.Height : printableArea.Width)); 
    int availableHeight = (int)Math.Floor(printDocument1.OriginAtMargins ? marginBounds.Height : (e.PageSettings.Landscape ? printableArea.Width : printableArea.Height)); 

    //g.DrawRectangle(Pens.Red, 0, 0, availableWidth - 1, availableHeight - 1); 

    // Doesnt work either 
    //g.DrawRectangle(Pens.Red, 0, 0, 100, 100); 

    System.Drawing.Font myFont = new System.Drawing.Font("Courier New", 10, FontStyle.Underline, GraphicsUnit.Pixel); 

    float lineHeight = myFont.GetHeight(g); 
    float yLineTop = e.MarginBounds.Top; 

    if (printDocumentPage >= m_report.PageCount) 
    { 
     e.HasMorePages = false; 
     return; 
    } 

    otPage page = m_report.pageAt((int)printDocumentPage); 

    for (int i = 0; i < page.LineCount ; i++) 
    { 
     otLine line = page.lineAt(i); 

     g.DrawString(line.getPrintLine(true), myFont, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic); 

     yLineTop += lineHeight; 
    } 

    printDocumentPage++; 

    if (printDocumentPage < m_report.PageCount) 
    { 
     e.HasMorePages = true; 
     return; 
    } 

    e.HasMorePages = false; 
} 

PrintPreviewDialog上出現,並且兩個事件處理程序運行時,它遍歷正確的線,並提請他們中的每一個,但沒有被繪製在對話框中當發送到打印機時,它會打印空白頁面。 PrintPreviewDialog全部是灰色的,沒有白色的頁面或任何東西,並且在所有級別上縮放都不起作用。

編輯:擺脫我目前的頁/行處理,只是試圖繪製一個字符串,任何字符串,或長方形,東西沒有工作。

以下是打印預覽屏幕的屏幕截圖。更改縮放/頁不執行任何操作之一:enter image description here

EDIT2:

我下載了這個非常簡單的示例項目從http://www.c-sharpcorner.com/uploadfile/mahesh/printpreviewcontrol-in-c-sharp/我假設按預期工作。直接出箱,這是做同樣的事情。只顯示一個灰色屏幕(和表格上的按鈕)。在printpreviewcontrol中根本沒有打印任何東西。

可能是什麼問題?

EDIT3:

我測試了另一種開發機上,它工作正常。爲什麼它不能在我的電腦上運行?另一臺計算機是64位,而這是32位,但是使用相同的設置構建。難道它是我的.NET框架搞砸了嗎?

EDIT4:

對不起,所有的編輯。我發現,如果我切換我的默認打印機,它完美的作品。與e.MarginBounds有關。

回答

-1

,因爲你沒有使用yLineTop變量你總是在你的e.MarginBounds財產在同一地點進行打印:

for (int i = 0; i < page.LineCount; i++) 
{ 
    otLine line = page.lineAt(i); 
    g.DrawString(line.getPrintLine(true), myFont, Brushes.Black, 
       e.MarginBounds, StringFormat.GenericTypographic); 
    yLineTop += lineHeight; 
} 

我懷疑它應該是這個樣子:

g.DrawString(line.getPrintLine(true), myFont, Brushes.Black, 
       new PointF(e.MarginBounds.Left, yLineTop), 
       StringFormat.GenericTypographic); 

但是你有一個問題,你沒有檢查,看看yLineTop是否觸及頁面的底部,在這種情況下,這是當你需要設置e.HasMorePages = true;。因爲您必須跟蹤此函數外部的行,所以for循環中的i變量無法在此方案中使用。

+0

謝謝,我用新的PointF(e.MarginBounds.Left,yLineTop + lineHeight * i)解決了線問題,但仍然沒有任何工作。即使我只畫一條線,也只是畫一個灰色的屏幕。我已經把所有東西都分成了幾頁,我將縮放字體以使它們合適,但現在我只是試圖讓它打印任何東西。 – Joshjje 2013-03-20 15:17:52

+0

@Joshjje無法複製它。然後再次,你沒有記錄你的'otPage'對象是什麼。在打印代碼中放置一個調試器中斷,並檢查該m_Report變量的值。 – LarsTech 2013-03-20 15:21:11

+0

如果我註釋掉所有這些垃圾,只用一個字符串文字做一個DrawString,它不起作用。或者如果我只是做一個DrawRectangle,什麼都不畫,並且打印機上沒有任何東西可以打印:(。 – Joshjje 2013-03-20 15:33:10