我在打印文档时遇到问题。我不是编程方面的专家,但是我已经学习了大约一年了,所以我理解了基础知识。我想我知道问题出在哪里,但我不知道如何解决这个问题,而且我已经尽职尽责地找到答案而没有任何运气。
我正在尝试创建一个报告列表,用户可以选择要打印的特定报告,然后点击打印按钮或打印预览按钮,打印或显示报告。当我将打印或打印预览按钮连接到一个特定的报告时,它工作正常。一旦我将其更改为从列表中打印,它就会开始给我带来问题,只有在报告长度超过两页时才会出现问题,并且在我没有做任何更改的情况下随机发生。 (有时它工作正常,其他时候没有)问题是它会尝试将所有页面打印到一张纸上,就像已经双重曝光的照片一样。见下面的屏幕截图。
以下是我认为重要的代码。
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 :(得分: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(); //<-------------
}