You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WinForm首次打印正常后续Pagefeed失效 多页合并为单页

问题根因

  • 每次点击打印按钮重复注册PrintPage事件:每点击一次按钮就执行一次printDocument1.PrintPage += new PrintPageEventHandler(printit),后续打印时printit方法会被触发多次,状态逻辑完全混乱。
  • 打印状态未在每次独立任务前重置:你当前只在按钮点击流程的开头初始化了一次Y/N/PageNr三个状态变量,如果先走了打印预览,预览执行完成后这三个变量已经被修改为最终值,后面实际打印时用的是旧状态,自然只会输出1页。
  • 额外优化点:printit方法中每次新建的Font对象未释放,存在资源泄漏风险,原有逻辑还会出现相邻页首行和末行重复的小问题。

修复代码

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace PrintIt
{
    public partial class Form1 : Form
    {
        int N, Y, PageNr;
        public Form1()
        {
            InitializeComponent();
            // 事件只在构造函数注册一次即可,不要每次点击按钮重复注册
            printDocument1.PrintPage += new PrintPageEventHandler(printit);
        }

        // 新增:重置打印状态的方法,每次独立打印任务(预览/实际打印)启动前都调用
        private void ResetPrintState()
        {
            Y = 0;
            N = 0;
            PageNr = 1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            printDocument1.DocumentName = "Test Print";
            printDialog1.Document = printDocument1;

            pageSetupDialog1.PageSettings =
                new System.Drawing.Printing.PageSettings();

            DialogResult result = pageSetupDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                printDocument1.DefaultPageSettings.PaperSize = pageSetupDialog1.PageSettings.PaperSize;
                printDocument1.DefaultPageSettings.Landscape = pageSetupDialog1.PageSettings.Landscape;

                string message = "Would you like to view the print preview first?";
                string caption = "Print Preview";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;

                result = MessageBox.Show(message, caption, buttons);
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    // 预览前先重置状态
                    ResetPrintState();
                    printPreviewDialog1.Document = printDocument1;
                    ((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;
                    printPreviewDialog1.ShowDialog();
                }

                if (printDialog1.ShowDialog() == DialogResult.OK)
                {
                    // 实际打印前再次重置状态,避免预览修改了状态值
                    ResetPrintState();
                    printDocument1.Print();
                }
                
            }
        }

        private void printit(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            // 用using释放Font资源
            using (Font LFont = new Font("Arial", 12))
            {
                float fontHeight = LFont.GetHeight();
                int startY = 40;
                int pageHeight = printDocument1.DefaultPageSettings.PaperSize.Height;

                e.Graphics.DrawString("Page:" + PageNr, LFont, Brushes.Black, PageNr * 100, 10);
                for (int i = Y; i < 200; i++)
                {
                    e.Graphics.DrawString("Line: " + i, LFont, Brushes.Black, PageNr * 100, startY + N);
                    N += (int)fontHeight;

                    if (startY + N >= pageHeight - 100)
                    {
                        e.HasMorePages = true;
                        N = 0;
                        Y = i + 1; // 补+1避免上下页行重复
                        PageNr += 1;
                        return;
                    }
                }
                e.HasMorePages = false;
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Oscar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 19:48:01