before I end the crazy hideaway, I thought I'd try: how do you write code to print more than one page?
I am trying to use all the examples that I could find in stackoverflow (and other places), but have not succeeded! It really makes me more crazy than me! All the other examples that I found related to issues that were not related to what I'm trying to do. The example I'm trying to fix will print 0-100 integers on two pages, that is, 0-80 on page 1 and 81-100 on page 2. Despite all the suggested methods, all I can get is one page that is overwritten by page 2 on top.
e.HasMorePages = true; should start the next page, but not working.
I created a very simple Winform program for this, and here is the code. Any ideas would be greatly appreciated:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace PrintMultiplePages_v2
{
public partial class Form1 : Form
{
ArrayList al = new ArrayList();
private int fontcount;
private int fontposition = 1;
private float ypos;
private string textToPrint;
private PrintPreviewDialog previewDlg = null;
private PrintDocument pd = null;
private int counter = 0;
private int amtperpage = 80;
public Form1()
{
InitializeComponent();
for (int i = 0; i < 100; i++)
al.Add(i.ToString());
}
private void buttonPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float leftMargin = 70.0f;
float topMargin = 20.0f;
float lineInc = 20.0f;
Font printFontArial10 = new Font("Arial", 10, FontStyle.Regular);
Graphics g = e.Graphics;
double pageCount = (double)al.Count / (double)amtperpage;
int pageRequired = Convert.ToInt32(Math.Ceiling(pageCount));
counter = 0;
for (int page = 1; page <= pageRequired; page++)
{
int counterMax = amtperpage * page;
if (counterMax > al.Count)
counterMax = al.Count;
for (int x = counter; x < counterMax; x++)
{
textToPrint = al[x].ToString() + " - test";
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin, topMargin + lineInc);
lineInc += 12;
counter++;
}
if (counter == counterMax)
{
if (counter != al.Count)
{
e.HasMorePages = true;
counter++;
lineInc = 20.0f;
}
}
else
e.HasMorePages = false;
}
}
}
}
Corrected Code:
private int page = 0;
private void buttonPrint_Click(object sender, EventArgs e)
{
page = 0;
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float leftMargin = 70.0f;
float topMargin = 20.0f;
float lineInc = 20.0f;
Font printFontArial10 = new Font("Arial", 10, FontStyle.Regular);
Graphics g = e.Graphics;
int stop = counter + amtperpage;
if (stop > al.Count)
stop = al.Count;
while (counter < stop)
{
textToPrint = al[counter].ToString() + " - test";
e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin, topMargin + lineInc);
lineInc += 12;
counter++;
}
page++;
e.HasMorePages = counter < al.Count;
}
Bob t source
share