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

时间:2014-01-17 16:43:19

标签: c# .net

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

我一直在尝试在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;
        }
    }
}

}

更正的代码是:

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

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

尝试使用FlowDocument和IDocumentPaginator,如下所示:

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