0

我在打印文档时遇到问题。我绝不是编程专家,但我已经自学了大约一年,所以我了解了基础知识。我想我知道问题出在哪里,但我不知道如何解决它,我已经尽职尽责地找到了答案,但没有任何运气。

我正在尝试创建一个报告列表,用户可以选择要打印的特定报告,然后点击打印按钮或打印预览按钮,并根据需要打印或显示报告。当我将打印或打印预览按钮连接到一个特定的报告时,它工作正常。一旦我将其更改为从列表中打印,它就会开始给我带来问题,并且仅当报告长度为两页或更多页并且它随机发生而我没有进行任何更改时才会出现问题。(有时它可以正常工作,有时不能)问题是它会尝试将所有页面打印到一张纸上,就像一张双重曝光的照片一样。请参阅下面的屏幕截图。

问题示例

这是我认为很重要的代码。

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.
    }
}
4

1 回答 1

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(); //<-------------
    }
于 2013-03-18T00:09:08.087 回答