weixin_44151228 2023-01-09 14:39 采纳率: 58.3%
浏览 31
已结题

c#中怎样实现多页打印?

用C#的printdocument写数据,要求每页只写入31排,写满一页后,将多的内容写到第二页,以此类推,该如何写?
请根据我写的代码进行修改,怎样使我的长字符串str显示在多页上?

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            string str = "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example," +
                "Hello,World!,This,is,a,multi - page,print,example,";
            string[] arr = str.Split(',');
 
            // 定义变量表示当前页的编号、每页的行数和当前已经打印的行数
            int page = 1;
            int rowsPerPage = 31;
            int rowsPrinted = 0;
            int totalPages = (arr.Length + 30) / 31;
 
            // 创建用于绘制文本的字体
            Font font = new Font("Arial", 10);
 
            // 循环打印每一页
            while (page <= totalPages)
            {
                //循环结束后显示的行数清零
                int row = 0;
 
                // 绘制当前页的内容
                for (int i = rowsPrinted; i < arr.Length; i++)
                {
                    e.Graphics.DrawString(arr[i], font, Brushes.Black, new PointF(615, 215 + row * 18));
                    rowsPrinted++;
                    row++;
 
                    // 如果已经绘制的行数达到了每页的行数,跳出循环
                    if (rowsPrinted >= rowsPerPage * page)
                    {
                        break;
                    }
                }
                page++;
 
                //显示页码
                e.Graphics.DrawString(page.ToString(), font, Brushes.Black, new PointF(615, 215 + 32 * 18));
 
                // 如果还有更多的页面要打印,设置HasMorePages为true
                if (page <= totalPages)
                {
                    e.HasMorePages = true;
 
                }
                else
                {
                    e.HasMorePages = false;
 
                }
            }
 
        }
 
private void 打印预览_Click(object sender, EventArgs e)
        {
 
 
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = printDocument1;
            printPreviewDialog.ShowDialog();
 
 
        }
 

  • 写回答

2条回答 默认 最新

  • 於黾 2023-01-09 14:43
    关注

    不是问过了吗,怎么又问
    page++;放到if (page <= totalPages)后面去

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月9日
  • 赞助了问题酬金15元 1月9日
  • 创建了问题 1月9日

悬赏问题

  • ¥15 kali linux用wget archive.kali.org/archive-key.asc指令下载签名无效(失败)
  • ¥15 openHarmony 利用c++程序在dayu210开发板上实现拉取RTSP视频流并且在屏幕上显示
  • ¥15 GD32H757的can通信配置
  • ¥20 nist随机数测试的问题
  • ¥20 帮我解决这个项目,thank you各位程序员
  • ¥15 哪位能用ea软件做一下,有偿
  • ¥15 按要求编写汇编语言,并设计仿真电路图
  • ¥15 电脑软件运行次数如何统计
  • ¥15 同一个浏览器打开两个窗口怎么区分会话
  • ¥100 如何编写自己的emmc镜像