How to print more than one page in C #

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

}

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;
    }
+4
source share
2 answers

The PrintPage event is supposed to be called repeatedly until e.HasMorePages becomes false. It is before this event to print one page at a time. You have this only once and once feeds both pages in one loop. In other words, that for the cycle is killing you. Logically, you should keep track of which page you are on (outside of pd_PrintPage), and increment the counter as it continues. You can say that this is not true for you, because the counter is set to zero in pd_PrintPage, while the Print_Click button should be set to zero.

, "int-" pd_PrintPage -

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

FlowDocument DocumentocumentPaginator, :

public void PrintDocument(MemoryStream outputStream)
{
    FlowDocument fd = new FlowDocument();
    TextRange tr = new TextRange(fd.ContentStart, fd.ContentEnd);
    tr.Load(outputStream, System.Windows.DataFormats.Rtf);

    PrintDialog printDlg = new PrintDialog();
    fd.PageHeight = printDlg.PrintableAreaHeight;
    fd.PageWidth = printDlg.PrintableAreaWidth;
    fd.PagePadding = new Thickness(25);

    fd.ColumnGap = 0;
    fd.ColumnWidth = (fd.PageWidth -
                           fd.ColumnGap -
                           fd.PagePadding.Left -
                           fd.PagePadding.Right);

    if (printDlg.ShowDialog() == true)
    {              
        IDocumentPaginatorSource idpSource = fd;
        idpSource.DocumentPaginator.PageSize = new Size { Height = 600, Width = 600 };
        printDlg.PrintDocument(idpSource.DocumentPaginator, "Printing Document");
    }
}
0

All Articles