如何在C#中打印多个页面

Bob*_*b T 5 .net c#

在我致力于疯人院之前,我想我会尝试一下:你如何编写代码来打印多个页面?

我一直在尝试我在stackoverflow(和其他地方)找到的所有例子,但我没有取得任何成功!这让我比以前更疯狂!我发现的所有其他例子都是处理与我想要做的事情无关的问题.我试图修复的示例将在两个页面上打印0-100个整数,即第1页的0-80和第2页的81-100.尽管所有我建议的技术都是一页被页面覆盖的页面2的数据在顶部.

e.HasMorePages = true; 应该开始下一页但不起作用.

我为此创建了一个非常简单的Winform程序,这是代码.任何想法将不胜感激:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

更正的代码是:

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;
    }
Run Code Online (Sandbox Code Playgroud)

Jac*_*itt 5

应该重复调用PrintPage事件,直到e.HasMorePages变为false.这个事件取决于一次打印一页.你只需要调用一次,然后在一个for循环中将它们送到两个页面.换句话说,for循环正在杀死你.从逻辑上讲,您应该跟踪当前所在的页面(pd_PrintPage之外)并在继续时递增计数器.你可以告诉你有这个错误,因为 pd_PrintPage中计数器被设置为零,而在buttonPrint_Click中应该设置为零.

所以从pd_PrintPage中拉出"int page"并使循环类似于

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.
Run Code Online (Sandbox Code Playgroud)