跨多个页面打印表单

本文关键字:打印 表单 | 更新日期: 2023-09-27 18:02:55

我有3个打印方法:printa, printb和printc(每个打印一个表单)。所有这3个表格都需要在多个页面上打印。我如何打印3页?

我知道,我们需要使用事件处理程序,并使用currentpage和.hasmorepages,但我不知道如何使用它

跨多个页面打印表单

只需跟踪页码。在BeginPrint事件中将其设置为0,并在每次调用PrintPage时增加它。这样的:

    int pageNumber;
    private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
        pageNumber = 0;
    }
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
        ++pageNumber;
        e.HasMorePages = true;
        switch (pageNumber) {
            case 1: printa(e); break;
            case 2: printb(e); break;
            case 3: printc(e); e.HasMorePages = false; break;
        }
    }

是的,它可能相当模糊。下面是我打印Reporting Services报告的方法(下面代码中的MetaFile图像数组)。

public void Print()
{
    if (emfImage == null || emfImage.Count <= 0)
    {
        throw new ArgumentException("An image is required to print.");
    }
    printer = printer.Trim();
    if (string.IsNullOrEmpty(printer))
    {
        throw new ArgumentException("A printer is required.");
    }
    printingPage = 0;
    PrintDocument pd = new PrintDocument();
    pd.PrinterSettings.PrinterName = printer;
    pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
    pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    Metafile page = emfImage[printingPage];
    e.Graphics.DrawImage(page, 0, 0, page.Width, page.Height);
    e.HasMorePages = ++printingPage < emfImage.Count;
}