2013-03-17 3 views
0

У меня возникла проблема с печатью документа. Я никоим образом не специалист по программированию, но самостоятельно учился около года, поэтому я понимаю основы. Я думаю, что знаю, где проблема, но я не знаю, как ее исправить, и я приложил все усилия, чтобы найти ответ без везения.Печать нескольких страниц на одном листе

Я пытаюсь создать список отчетов, в которых пользователь может выбрать конкретные отчеты для печати, а затем нажать кнопку печати или кнопку предварительного просмотра печати и распечатать или отобразить отчеты соответствующим образом. Когда у меня есть кнопки печати или печати, связанные с одним конкретным отчетом, он работает нормально. После того, как я изменил его для печати из списка, он начинает давать мне проблемы, и проблема возникает только тогда, когда отчет состоит из двух или более страниц, и он случайным образом происходит без внесения каких-либо изменений. (иногда это работает отлично, а иногда и нет). Проблема заключается в том, что он попытается распечатать все страницы на одном листе, как фотография, которая была дважды открыта. См. Снимок экрана ниже.

example of problem

Вот код, я считаю важным.

public class Report //Creates a new class called Report. 
{ 
    public PrintDocument Document = new PrintDocument(); 

    private int LineNumber; 
    private int PageNumber; 
    private int TotalNumberOfPages; 

    private PrintDocument Set() //Sets up the document. 
    { 
     Body.Add(new NewLine()); 
     LineNumber = 0; 
     PageNumber = 1; 
     Document.DocumentName = Title[0].Text; 
     Document.PrintPage += new PrintPageEventHandler(OnPrintPage); 
     return Document; 
    } 
    private void OnPrintPage(object sender, PrintPageEventArgs e) //Sets up the lines to be printed. 
    { 
     int Offset = e.MarginBounds.Top; 
     int PageEnd = e.MarginBounds.Top + e.MarginBounds.Height; 

     //Instructions on how to build the page. Trust me, it's right. 

     if (AllLines.Count > LineNumber) 
     { 
      e.HasMorePages = true; 
      /* ^This is where the problem is problem is. When this is made true, it sometime changes back to 
       false (as it should) when it gets to the end, but sometime it does not. The times that it randomly 
       does not, it still starts the page over but does it right on top of the old page like I printed 
       the first page out, then put the paper back into the printer and printed the second page over it. 
      */ 
      PageNumber++; 
     } 
     else 
     { 
      e.HasMorePages = false; 
      LineNumber = 0; 
      PageNumber = 1; 
     } 
    } 

    public void Print() //Sets the command to send the document to the printer. 
    { 
     Set().Print(); 
    } 
    public void PrintPreview() //Set the command to display the document. 
    { 
     PrintPreviewDialog PP = new PrintPreviewDialog(); 
     PP.Document = Set(); 
     PP.ShowDialog(); 
    } 
} 

public partial class frmReports : Form 
{ 
#region PRINT DOCUMENTS //Creates the reports. 
    public Report Report1() 
    { 
     Report Page = new Report();  
     //Instructions on how to build the page. Trust me, it's right. 
     return Page; 
    } 
    public Report Report2() 
    { 
     Report Page = new Report();  
     //Instructions on how to build the page. Trust me, it's right. 
     return Page; 
    } 
    public Report Report3() 
    { 
     Report Page = new Report();  
     //Instructions on how to build the page. Trust me, it's right. 
     return Page; 
    } 
    public Report Report4() 
    { 
     Report Page = new Report();  
     //Instructions on how to build the page. Trust me, it's right. 
     return Page; 
    } 
    public Report Report5() 
    { 
     Report Page = new Report();  
     //Instructions on how to build the page. Trust me, it's right. 
     return Page; 
    } 
    #endregion 

    List<Report> Reports = new List<Report>(); //Creates a Reports List 

    public frmReports() 
    { 
     InitializeComponent();  

     //Adds the reports to the Reports List 
     Reports.Add(Report1()); 
     Reports.Add(Report2()); 
     Reports.Add(Report3()); 
     Reports.Add(Report4()); 
     Reports.Add(Report5()); 

     foreach (Report r in Reports) //Adds the reports to the List Box already added to the form. 
      lstReportList.Items.Add(r); 
    } 

    private void OnClick_btnPrint(object sender, EventArgs e) //When clicking the Print button. 
    { 
     if (lstReportList.SelectedItem != null) 
      foreach(Report r in Reports.FindAll(r => r == lstReportList.SelectedItem) r.Print(); //Determains all of the items in the List Box and prints them out. 
    } 

    private void OnClick_btnPrintPreview(object sender, EventArgs e) //When clicking the Print Preview button. 
    { 
     if (lstReportList.SelectedItem != null) 
      foreach(Report r in Reports.FindAll(r => r == lstReportList.SelectedItem) r.PrintPreview(); //Determains all of the items in the List Box and displays them. 
    } 
} 

ответ

0

Я понял. Мне нужно было сбросить документ.

public void Print() 
    { 
     Set().Print(); 
     Document = new PrintDocument(); //<------------- 
    } 
    public void PrintPreview() 
    { 
     PrintPreviewDialog PP = new PrintPreviewDialog(); 
     PP.Document = Set(); 
     Form frmPP = (Form)PP; 
     frmPP.WindowState = FormWindowState.Maximized; 
     PP.Name = PP.Document.DocumentName; 
     frmPP.Text = PP.Name; 
     frmPP.Icon = ((Icon)(Midas_Point_of_Sales_System.Properties.Resources.yellow)); 
     PP.ShowDialog(); 
     Document = new PrintDocument(); //<------------- 
    } 
Смежные вопросы