首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从TextBox打印多页

从TextBox打印多页
EN

Stack Overflow用户
提问于 2013-11-22 16:09:08
回答 1查看 1.2K关注 0票数 2
代码语言:javascript
复制
private void PrintTextBox(object sender, PrintPageEventArgs e)
{
    e.Graphics.DrawString(textBox1.Text, textBox1.Font, Brushes.Black, 50, 20);
}

private void printListButton_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += PrintTextBox;
    PrintPreviewDialog ppd = new PrintPreviewDialog();
    ppd.Document = pd;
    ppd.ShowDialog();
}

我尝试使用PrintTextBox方法使用e.HasMorePages == true,但随后它连续地开始添加页面。你知道怎么解决吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-22 16:21:16

这是一个经常出现的问题,e.hasmorepages没有一个常见的行为。e.hasmorepages会一次又一次地触发printtextbox,直到您说不(e.hasmorepages=false)为止。

您必须数行,然后计算空格,如果它不适合您的论文哟决定文件是否有更多的页。

我通常使用一个整数来计算我将要打印的行,如果没有足够的空间,则使用e.hasmorages=true;

检查这个简单的示例,您必须添加system.drawing.printing

代码语言:javascript
复制
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private string[] Lines = new string[10];
    private int CurrentRow = 0;

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 10; i++)
        {
            Lines[i] = i.ToString("N2");
        }
        PrintDocument pd=new PrintDocument();
        PrintDialog pdi = new PrintDialog();
        pdi.ShowDialog();
        pd.PrinterSettings = pdi.PrinterSettings;
        pd.PrintPage += PrintTextBox;
        pd.Print();
    }

    private void PrintTextBox(object sender, PrintPageEventArgs e)
    {
        int y = 0;

        do 
        {
            e.Graphics.DrawString(Lines[CurrentRow],new Font("Calibri",10),Brushes.Black,new PointF(0,y));
            CurrentRow += 1;
            y += 20;
            if (y > 20) // max px per page
            {
                e.HasMorePages = CurrentRow != Lines.Count(); // check if you need more pages
                break;
            }
        } while(CurrentRow < Lines.Count());
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20149377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档