打印连接的字符串

时间:2011-08-03 15:23:22

标签: c#

我在使用printdocument方法打印字符串时遇到了很多麻烦。

我有一个字符串形式的无效条目报告。我通过在无效条目的forloop内部连接条目来构建此字符串。看起来像这样

foreach(Error entry in ErrorEntries)
   reportString += entry.ToString();

现在我从printdocumen t方法访问此字符串(它是一个单例)。问题是该字符串有大约300个条目,因此不适合一个页面。

假设它打印前30条记录。根据我的理解,我遇到了e.HasMorePages = true命令的问题,它重新运行了rpintdocument1方法。如果是这种情况,那么该方法将仅从上到下再次打印报告字符串,并在第30条记录处停止。

有没有办法删除我刚刚从reportSummary字符串打印的行,所以下次printdocument方法运行时,它不会打印相同的字符串内容(起始30条记录)?

2 个答案:

答案 0 :(得分:0)

如果没有连接单个字符串而是将错误列表传递给print方法并保留需要打印的第一条记录的索引...那么下次调用打印页面方法时,从您上次打印的点开始(+1)

答案 1 :(得分:0)

您可以使用printdocument方法所在的类中的字段来存储到目前为止您已设置的打印记录数:

编辑:更新了符合您情况的示例代码

class YourClass {
  private int charactersPrinted = 0;

  ...

  private void printdocument(object sender, PrintPageEventArgs ev) {
    var charactersDoneThisPage = 
      this.PrintReportStringPart(reportString.Substring(charactersPrinted));

    charactersPrinted += charactersDoneThisPage; // Number of characters you managed to print this page

    if (charactersPrinted < reportString.Length) {
      ev.HasMorePages = true;
    } else {
      ev.HasMorePages = false;
    }
  }

  // Prints the string to the page
  // Returns the number of characters it was actually able to print
  private int PrintReportStringPart(string reportStringPart) {

    // Print the reportStringPart however you're already doing it
    //  filling the page
    return charactersDoneThisPage;
  }
}