Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Winform应用程序中打印多页_C#_Printdocument - Fatal编程技术网

C# 在Winform应用程序中打印多页

C# 在Winform应用程序中打印多页,c#,printdocument,C#,Printdocument,我正在尝试使用C#Winforms应用程序打印某些内容。我似乎无法理解多页是如何工作的。假设我的构造函数中有以下代码: private string _stringToPrint; _stringToPrint = ""; for (int i = 0; i < 120; i++) { _stringToPrint = _stringToPrint + "Line " + i.ToString() + Environment.NewLine; } 现在,当我运行这个代码时,它给了

我正在尝试使用C#Winforms应用程序打印某些内容。我似乎无法理解多页是如何工作的。假设我的构造函数中有以下代码:

private string _stringToPrint;
_stringToPrint = "";
for (int i = 0; i < 120; i++)
{
    _stringToPrint = _stringToPrint + "Line " + i.ToString() + Environment.NewLine;
}

现在,当我运行这个代码时,它给了我一页,大约70行之后,它就从纸上跑了出来。如何打印此字符串,使其打印足够一页,然后转到第二页,等等?

您可以使用计数器设置每页所需的行数,如下所示:

private string[] _stringToPrint = new string[100]; // 100 is the amount of lines
private int counter = 0;
private int amtleft = _stringToPrint.Length;
private int amtperpage = 40; // The amount of lines per page
for (int i = 0; i < 120; i++)
{
    _stringToPrint[i] ="Line " + i.ToString();
}
我在
e.HasMorePages
方面有过不好的经历,所以这可能不起作用


让我知道这是否有效,我希望它有帮助

这个问题以前肯定被问过几千次。当然,它不合适。这是您从Graphics.MeasureString()重载中发现的,该重载返回charactersFitted。这样您就可以将e.HasMorePages设置为true,并在下一页打印其余内容。当然,你还需要BeginPrint来重置索引。@HansPassant-我真的在请求之前用谷歌搜索了一下。如果Microsoft在
PrintPageEventArgs
上附加了一个
Page
属性,该属性将被设置为当前呈现的任何页面,那么它看起来会容易得多。这段代码可以工作,但这是最简单的方法吗?似乎有很多数学只是为了打印一条长线,实际上没有更好的方法,大多数数学是为了计算当前页面上要打印多少,要打印多少以及当前行。对我来说,这是最简单的方法,但如果你不完全理解,我可以帮你聊天。
private string[] _stringToPrint = new string[100]; // 100 is the amount of lines
private int counter = 0;
private int amtleft = _stringToPrint.Length;
private int amtperpage = 40; // The amount of lines per page
for (int i = 0; i < 120; i++)
{
    _stringToPrint[i] ="Line " + i.ToString();
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    int currentamt = (amtleft > 40)?40:amtleft;
    Graphics g = e.Graphics;
    var font = new Font("Arial", 10f, FontStyle.Regular);
    for(int x = counter; x < (currentamt+counter); x++)
    {
        g.DrawString(_stringToPrint[x], font, Brushes.Black, new PointF(10f, (float)x*10));
        // x*10 is just so the lines are printed downwards and not on top of each other
        // For example Line 2 would be printed below Line 1 etc
    }
    counter+=currentamt;
    amtleft-=currentamt;
    if(amtleft<0)
        e.HasMorePages = true;
    else
        e.HasMorePages = false;
    // If e.HasMorePages is set to true and the 'PrintPage' has finished, it will print another page, else it wont
}