2013-10-11 8 views
7

Прямо сейчас, я пытаюсь создать свою форму на PrintDocument, но единственный способ увидеть, где на самом деле появляется на странице, - это напечатать документ. Это работает, но у меня есть много вещей, которые мне нужно добавить, и я бы предпочел не тратить кучу бумаги. Прямо сейчас у меня появился диалог печати, но нет кнопки предварительного просмотра печати. Есть ли способ заставить меня появиться? Благодаря!Показывать Предварительный просмотр в C#

public partial class Form4 : System.Windows.Forms.Form 
    { 
     private System.ComponentModel.Container components; 
     private System.Windows.Forms.Button printButton; 

     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); 
      PrintDialog pdi = new PrintDialog(); 
      pdi.Document = pd; 
      if (pdi.ShowDialog() == DialogResult.OK) 
      { 
       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", 20.0f)) 
      { 
       e.Graphics.DrawImage(img, logo); 
       e.Graphics.DrawString("Header", printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); 
      } 
      using (SolidBrush blueBrush = new SolidBrush(Color.Black)) 
      { 
       Rectangle rect = new Rectangle(100, 100, 500, 120); 
       e.Graphics.FillRectangle(blueBrush, rect); 
      } 
     } 


     // 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); 
     } 

    } 
+0

Я уверен, что вы используете Windows Forms поэтому убедитесь, что помечать вопрос с 'winforms' тега. Для этой цели вы можете использовать [PrintPreviewControl] (http://msdn.microsoft.com/en-us/library/system.windows.forms.printpreviewcontrol.aspx). –

ответ

7

Вы можете использовать PrintPreviewDialog для этого:

private void printButton_Click(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); 

    PrintDialog printdlg = new PrintDialog(); 
    PrintPreviewDialog printPrvDlg = new PrintPreviewDialog(); 

    // preview the assigned document or you can create a different previewButton for it 
    printPrvDlg.Document = pd; 
    printPrvDlg.ShowDialog(); // this shows the preview and then show the Printer Dlg below 

    printdlg.Document = pd; 

    if (printdlg.ShowDialog() == DialogResult.OK) 
    { 
     pd.Print(); 
    } 
} 
+0

Примечание: PrintPreviewDialog является частью пространства имен System.Windows.Forms. – Jeff

0

Если хотите иметь отдельную кнопку для предварительного просмотра документа перед печатью, проверить это How do I trigger the PrintPreviewDialog Вы можете игнорировать аспект потока файлов ,

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