2013-01-22 62 views
-1

如何在WPF中使用下面的代碼?在WPF中使用PrintDocument

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     e.Graphics.DrawString("Name", new Font("tahoma", 10), Brushes.Black, 100, 100); 
     e.Graphics.DrawString("Last", new Font("tahoma", 10), Brushes.Black, 100, 120); 
    } 

回答

0

我不能因點尚未置評,但是你展示的是一個方法添加到事件處理程序,這是我已經爲打印文本文檔的例子:

添加方法的事件處理程序:

printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument_PrintPage); 

的事件處理方法(你的情況已經發布了什麼):

void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    { 
     int charactersOnPage = 0; 
     int linesPerPage = 0; 

     // Sets the value of charactersOnPage to the number of characters 
     // of stringToPrint that will fit within the bounds of the page. 
     e.Graphics.MeasureString(stringToPrint, printFont, 
      e.MarginBounds.Size, System.Drawing.StringFormat.GenericTypographic, 
      out charactersOnPage, out linesPerPage); 

     // Draws the string within the bounds of the page 
     e.Graphics.DrawString(stringToPrint, printFont, System.Drawing.Brushes.Black, 
      e.MarginBounds, System.Drawing.StringFormat.GenericTypographic); 

     // Remove the portion of the string that has been printed. 
     stringToPrint = stringToPrint.Substring(charactersOnPage); 

     // Check to see if more pages are to be printed. 
     e.HasMorePages = (stringToPrint.Length > 0); 
    } 

火掉了打印:

printDocument.Print(); 
+0

我的代碼在WINDOWSFORMAPPLICATION中使用,所以我可以在WPF中使用您的代碼? –

+0

我認爲WPF dosnt支持'Graphics.DrawString' –

+0

我只回答瞭如何使用您發佈的方法,這些都是system.drawing的一部分,專門用於打印。我以爲你想從你的wpf應用程序打印 –