Printing 在一张纸上打印多页

Printing 在一张纸上打印多页,printing,Printing,我在打印文档时遇到问题。我绝对不是编程方面的专家,但我已经自学了大约一年了,所以我了解了基础知识。我想我知道问题出在哪里,但我不知道如何解决它,我已经尽了我应有的努力,毫无运气地找到了答案 我正在尝试创建一个报告列表,用户可以选择要打印的特定报告,然后点击打印按钮或打印预览按钮,并根据需要打印或显示报告。当我将“打印”或“打印预览”按钮连接到一个特定的报告时,效果很好。一旦我将它从列表中更改为打印,它就开始给我带来问题,只有当报告有两页或两页以上长时,问题才会出现,而且在我不做任何更改的情况下,

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

我正在尝试创建一个报告列表,用户可以选择要打印的特定报告,然后点击打印按钮或打印预览按钮,并根据需要打印或显示报告。当我将“打印”或“打印预览”按钮连接到一个特定的报告时,效果很好。一旦我将它从列表中更改为打印,它就开始给我带来问题,只有当报告有两页或两页以上长时,问题才会出现,而且在我不做任何更改的情况下,它会随机发生。有时它可以很好地工作,而另一些时候问题不在于它会试图将所有页面打印到一张纸上,就像一张双重曝光的照片。请参见下面的屏幕截图

以下是我认为很重要的代码

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

我想出来了。我需要重新设置文档

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