How to Print more than one page in C#

Bob T

before I am committed to the insane asylum I thought I'd give this a try: How do you write the code to print more than one page?

I have been trying all the examples I could find on stackoverflow (and other places) but am not having any success! It really is making me crazier than I already am! All the other examples I found were dealing with issues that didn't relate to what I am trying to do. The example I am trying to fix would print 0 - 100 integers on two pages, i.e., 0-80 on page 1 and 81-100 on page 2. Despite all the techniques suggested all I can get is one page that is overwritten with page 2's data on top.

e.HasMorePages = true; is supposed to start the next page but is 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; // The amount of lines per page


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

}

The corrected code is:

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;
    }
Jacob Proffitt

The PrintPage event is supposed to be called repeatedly until e.HasMorePages becomes false. It's up to that event to print one page at a time. You have it only being called once and are feeding it both pages in a single for loop. In other words, that for loop is killing you. Logically, you should be tracking which page you are currently on (outside of pd_PrintPage) and incrementing the counter as it continues. You can tell you have this wrong because counter is being set to zero in pd_PrintPage whereas is should be set to zero in buttonPrint_Click.

So pull "int page" out of pd_PrintPage and have the loop be something like

int stop = counter + amtperpage;
if (stop >= al.Count)
    stop = al.Count - 1; // - 1 to prevent index out of range error.

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 - 1; // pesky zero-based array issue again.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Print more than one page with Printable on Java

From Dev

How to print more than one QStrings on QtextEdit

From Dev

how to use more than one validation in one page

From Dev

How to print more than one div at the same time in same pdf?

From Dev

How to cat and print formatted more than one file?

From Dev

How to create more than one index page in Ruby on Rails

From Dev

how to put isActive for More than one page in angularjs

From Dev

How to include a PHP page more than once one with a curl

From Dev

How to use more than one date picker on a page

From Dev

how to put isActive for More than one page in angularjs

From Dev

configuring more than one {{> uploader}} in one page

From Dev

sed print more than one matches in a line

From Dev

Print more than one row in Crystal Report

From Dev

Print sums of more than one column in Linux

From Dev

Django more than one ListView to an html page

From Dev

more than one ViewModel on same page in MVC

From Dev

Multiple User Section [more than one page]

From Dev

Parsing more than one page from API

From Dev

How to have a C++ stack with more than one data type?

From Dev

How to redirect more than one text file in c programm

From Dev

how to trap more than one signal in c++?

From Dev

How to make string variables more than one word c++?

From Dev

How to have a C++ stack with more than one data type?

From Dev

How to redirect more than one text file in c programm

From Dev

How do you start more than one thread in C++

From Dev

How to create a c# enumeration with more than one property

From Dev

How do I get a long (more than 1 page) bibliography to print in an R markdown beamer?

From Dev

comet.c cannot work with more than one page opened in browser

From Dev

How to print in C# html document in ONE page

Related Related

  1. 1

    Print more than one page with Printable on Java

  2. 2

    How to print more than one QStrings on QtextEdit

  3. 3

    how to use more than one validation in one page

  4. 4

    How to print more than one div at the same time in same pdf?

  5. 5

    How to cat and print formatted more than one file?

  6. 6

    How to create more than one index page in Ruby on Rails

  7. 7

    how to put isActive for More than one page in angularjs

  8. 8

    How to include a PHP page more than once one with a curl

  9. 9

    How to use more than one date picker on a page

  10. 10

    how to put isActive for More than one page in angularjs

  11. 11

    configuring more than one {{> uploader}} in one page

  12. 12

    sed print more than one matches in a line

  13. 13

    Print more than one row in Crystal Report

  14. 14

    Print sums of more than one column in Linux

  15. 15

    Django more than one ListView to an html page

  16. 16

    more than one ViewModel on same page in MVC

  17. 17

    Multiple User Section [more than one page]

  18. 18

    Parsing more than one page from API

  19. 19

    How to have a C++ stack with more than one data type?

  20. 20

    How to redirect more than one text file in c programm

  21. 21

    how to trap more than one signal in c++?

  22. 22

    How to make string variables more than one word c++?

  23. 23

    How to have a C++ stack with more than one data type?

  24. 24

    How to redirect more than one text file in c programm

  25. 25

    How do you start more than one thread in C++

  26. 26

    How to create a c# enumeration with more than one property

  27. 27

    How do I get a long (more than 1 page) bibliography to print in an R markdown beamer?

  28. 28

    comet.c cannot work with more than one page opened in browser

  29. 29

    How to print in C# html document in ONE page

HotTag

Archive