2013-10-11 11 views
6

Я пытаюсь использовать пример печати MSDN с помощью PrintDocument, но это не так хорошо. У меня есть все, чтобы скомпилировать, но когда я нажимаю печать, появляется окно «Параметры отправки факса». Это должно произойти? Я пытаюсь распечатать, а не отправлять факс!Печать форм с использованием PrintDocument

Что мне нужно изменить, чтобы напечатать это прямо на принтере по умолчанию?

Спасибо!

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using System.Drawing.Printing; 

namespace WindowsFormsApplication1 
{ 
public partial class Form4 : System.Windows.Forms.Form 
{ 
    private System.ComponentModel.Container components; 
    private System.Windows.Forms.Button printButton; 
    private Font printFont; 
    private StreamReader streamToPrint; 

    public Form4() 
    { 
     // The Windows Forms Designer requires the following call. 
     InitializeComponent(); 
    } 

    // The Click event is raised when the user clicks the Print button. 
    private void printButton_Click(object sender, EventArgs e) 
    { 
     PrintDocument pd = new PrintDocument(); 
     pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); 
     pd.Print(); 

    } 

    // The PrintPage event is raised for each page to be printed. 
    private void pd_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     Single yPos = 0; 
     Single leftMargin = e.MarginBounds.Left; 
     Single topMargin = e.MarginBounds.Top; 
     Image img = Image.FromFile("logo.bmp"); 
     Rectangle logo = new Rectangle(40, 40, 50, 50); 
     using (Font printFont = new Font("Arial", 10.0f)) 
     { 
      e.Graphics.DrawImage(img, logo); 
      e.Graphics.DrawString("Testing!", printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); 
     } 
    } 


    // The Windows Forms Designer requires the following procedure. 
    private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     this.printButton = new System.Windows.Forms.Button(); 

     this.ClientSize = new System.Drawing.Size(504, 381); 
     this.Text = "Print Example"; 

     printButton.ImageAlign = 
      System.Drawing.ContentAlignment.MiddleLeft; 
     printButton.Location = new System.Drawing.Point(32, 110); 
     printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; 
     printButton.TabIndex = 0; 
     printButton.Text = "Print the file."; 
     printButton.Size = new System.Drawing.Size(136, 40); 
     printButton.Click += new System.EventHandler(printButton_Click); 

     this.Controls.Add(printButton); 
    } 

} 
} 
+1

Пожалуйста осмотрите его http://stackoverflow.com/questions/15985909/show-print-dialog-before-printing Вы должны также создать PrintDialog. – progpow

+1

@StanislavAgeyev: Я думаю, это подходит лучше: http://stackoverflow.com/questions/5164323/how-to-set-to-default-printer –

+0

Я добавил диалог печати, теперь он показывает, а затем настройки факса показывают , – Nathan

ответ

2

Это кажется, что факсимильный аппарат является принтером по умолчанию, самый простой способ, чтобы исправить это было бы добавить диалог перед печатью страницы

PrintDialog printDialog = new PrintDialog(); 
printDialog.Document = printDocument; 
//Show Print Dialog 
if (printDialog.ShowDialog() == DialogResult.OK) 
{ 
//Print the page 
printDocument.Print(); 
} 

Это позволит пользователю выбрать нужный им принтер перед печатью

2

Елки вы должны объявить объект System.Drawing.Printing.PrintDocument:

private System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument(); 

Затем добавьте код, описанный в предыдущем ответе:

PrintDialog printDialog = new PrintDialog(); 
printDialog.Document = printDocument; 
//Show Print Dialog 
if (printDialog.ShowDialog() == DialogResult.OK) 
{ 
//Print the page 
printDocument.Print(); 
}