在 WinForms 中打印多页不起作用

发布于 2025-01-07 00:55:54 字数 3642 浏览 1 评论 0原文

来自 问题

很快就得到了回答,我绊倒了升级后出现问题。

我已经更改了程序以从数据库填充一些数据集。

我在 printDocument 上调用 Print(),一切正常,它只是不想注册我的 e.HasMorePages = true;

这是代码:

   public static void printDokument()
   {
       if (result == DialogResult.OK)
       {

           DbDataPostavke = checkDB("SELECT * FROM " + tipDokumenta + "_postavke WHERE ID_" + tipDokumenta + " = " + stDokumenta);

           list = DbDataPostavke.Tables[0].AsEnumerable().ToList();                             
           printDocument.Print();
       }       
   }

   static void printDocument_PrintPage(object sender, PrintPageEventArgs e)
   {
       graphic = e.Graphics;

       e.PageSettings.PaperSize = ps;

       stranSirina = e.PageSettings.PrintableArea.Width;
       stranVisina = e.PageSettings.PrintableArea.Height;

       fontHeight = font.GetHeight();

       //this works/prints
       printDocument_PrintHeader();

       //this works/prints
       printDocument_PrintDocumentInfo();

       if (firstPage) printDocument_PrintSupplierInfo();    

       //Lines that I take from DB, amount of this lines is variable //it only prints one page, then it stops printing
       printDocument_PrintProductLines(e);

       //Sum of lines
       if(zadnjaStran) printDocument_printSum();

       //prints comment on document
       if (zadnjaStran) printDocument_PrintComment();

       //footer
       printDocument_PrintFooter();
   }

   static void printDocument_PrintProductLines(PrintPageEventArgs e)
   {
       //I print some stuff here (header, etc..) 

       String stranArtikliVrstica = String.Empty; // string for one line of data
       DataRow dataRow1 = null;
       DataRow dr = null;

       for(int i = 0; i < list.Count(); i++)
       {
           dr = list[i];
           dataRow1 = poglejBazo("SELECT ime, EM, opis FROM Sifrant WHERE ID = " + dr[2].ToString()).Tables[0].Rows[0];

           stranArtikliVrstica = String.Format("{0,-38}  {1,10}  {2,5}  {3,9:C}  {4,9:C}", dataRow1[0].ToString() + " - " + dataRow1[2].ToString(), dr[3].ToString(), dataRow1[1].ToString(), dr[4], Convert.ToInt16(dr[3]) * Convert.ToInt16(dr[4]));

           list.Remove(dr);

           graphic.DrawString(stranArtikliVrstica, font, brush, startX + offsetX, startY + offsetY);
           offsetY += (int)font.GetHeight();

           //if there is less then 35 "lines" remaining, we have enough space for printing some other stuff, otherwise, that stuff doesn't print..
           if (list.Count() < 35) zadnjaStran = true;
           else zadnjaStran = false;

           if (offsetY > stranVisina - 50)
           {
               prvaStran = false;
               stevecStrani++;
               offsetY = 0;
               e.HasMorePages = true;
               return;
           }
       }

   }

因此,当我尝试打印单页文档时,一切正常,但如果我尝试打印多页文档,则仅打印第一页(标题、文档信息、供应商信息、产品线(80 行中大约有 38 行) ),页脚),然后就没有更多页面了(我正在测试打印成 PDF 文件..)

我做错了什么?

PrintProductLines 中的 e 参数是否有问题?我如何告诉函数 PrintProductLines 我想从原始函数触发 e 上的 HasMorePages ?我知道我可以通过引用传递它,但 ref 关键字在我的情况下不起作用:S

编辑:

更改 static void printDocument_PrintProductLines(ref PrintPageEventArgs e)printDocument_PrintProductLines(ref e);< /code> 抛出错误:

错误 2 参数 1 必须使用“ref”关键字传递
错误 1 ​​最佳重载方法匹配 'GZIG.globalClass.printDocument_PrintPostavke(参考 System.Drawing.Printing.PrintPageEventArgs)' 有一些无效 论点

Coming from Question

which was answered really quick, I have stumbled upon upgraded problem.

I have changed my program to fill some DataSet from DB.

I call Print() on printDocument, everything works, it just doesn't want to register my e.HasMorePages = true;

Here is code:

   public static void printDokument()
   {
       if (result == DialogResult.OK)
       {

           DbDataPostavke = checkDB("SELECT * FROM " + tipDokumenta + "_postavke WHERE ID_" + tipDokumenta + " = " + stDokumenta);

           list = DbDataPostavke.Tables[0].AsEnumerable().ToList();                             
           printDocument.Print();
       }       
   }

   static void printDocument_PrintPage(object sender, PrintPageEventArgs e)
   {
       graphic = e.Graphics;

       e.PageSettings.PaperSize = ps;

       stranSirina = e.PageSettings.PrintableArea.Width;
       stranVisina = e.PageSettings.PrintableArea.Height;

       fontHeight = font.GetHeight();

       //this works/prints
       printDocument_PrintHeader();

       //this works/prints
       printDocument_PrintDocumentInfo();

       if (firstPage) printDocument_PrintSupplierInfo();    

       //Lines that I take from DB, amount of this lines is variable //it only prints one page, then it stops printing
       printDocument_PrintProductLines(e);

       //Sum of lines
       if(zadnjaStran) printDocument_printSum();

       //prints comment on document
       if (zadnjaStran) printDocument_PrintComment();

       //footer
       printDocument_PrintFooter();
   }

   static void printDocument_PrintProductLines(PrintPageEventArgs e)
   {
       //I print some stuff here (header, etc..) 

       String stranArtikliVrstica = String.Empty; // string for one line of data
       DataRow dataRow1 = null;
       DataRow dr = null;

       for(int i = 0; i < list.Count(); i++)
       {
           dr = list[i];
           dataRow1 = poglejBazo("SELECT ime, EM, opis FROM Sifrant WHERE ID = " + dr[2].ToString()).Tables[0].Rows[0];

           stranArtikliVrstica = String.Format("{0,-38}  {1,10}  {2,5}  {3,9:C}  {4,9:C}", dataRow1[0].ToString() + " - " + dataRow1[2].ToString(), dr[3].ToString(), dataRow1[1].ToString(), dr[4], Convert.ToInt16(dr[3]) * Convert.ToInt16(dr[4]));

           list.Remove(dr);

           graphic.DrawString(stranArtikliVrstica, font, brush, startX + offsetX, startY + offsetY);
           offsetY += (int)font.GetHeight();

           //if there is less then 35 "lines" remaining, we have enough space for printing some other stuff, otherwise, that stuff doesn't print..
           if (list.Count() < 35) zadnjaStran = true;
           else zadnjaStran = false;

           if (offsetY > stranVisina - 50)
           {
               prvaStran = false;
               stevecStrani++;
               offsetY = 0;
               e.HasMorePages = true;
               return;
           }
       }

   }

So, when I try to print a document with a single page, everything works, but if I try to print a document with multiple pages, only the first page prints (Header, DocumentInfo, SupplierInfo, ProductLines (around 38 lines out of 80), Footer) and then there is no more pages (I'm testing with printing into PDF file..)

What am I doing wrong?

Is there a problem with e parameter in PrintProductLines? How can I tell function PrintProductLines that I want to trigger HasMorePages on e from original function? I know I can pass it by reference, but ref keyword doesn't work in my case :S

EDIT:

Changing static void printDocument_PrintProductLines(ref PrintPageEventArgs e) and printDocument_PrintProductLines(ref e); throws an error:

Error 2 Argument 1 must be passed with the 'ref' keyword
Error 1 The best overloaded method match for
'GZIG.globalClass.printDocument_PrintPostavke(ref
System.Drawing.Printing.PrintPageEventArgs)' has some invalid
arguments

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞取 2025-01-14 00:55:54

您不应该将这样的打印代码放入静态全局类中。

该例程属于将使用 Graphics 对象的类的实际实例。

private const int PAD = 4;
private int m_Line, m_LinesToPrint;
private Font m_Font;
private PrintDocument m_Doc;

private void print_Click(object sender, EventArgs e) {
  using (var dlg = new PrintPreviewDialog()) {
    if (m_Doc == null) {
      throw new NullReferenceException("Create the document before trying to print it.");
    }
    dlg.Document = m_Doc;
    m_Line = 0;
    m_LinesToPrint = list.Count;
    m_Font = new Font("Courier New", 14, FontStyle.Underline, GraphicsUnit.Point);
    dlg.ShowDialog();
  }
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
  float lineHeight = m_Font.GetHeight(e.Graphics) + PAD;
  float yLineTop = e.MarginBounds.Top;
  for ( ; m_Line < m_LinesToPrint; m_Line++) {
    if (e.MarginBounds.Bottom < (yLineTop + lineHeight)) {
      e.HasMorePages = true;
      return;
    }
    DataRow dr = list[m_Line];
    DataRow row1 = poglejBazo("SELECT ime, EM, opis FROM Sifrant WHERE ID = " + dr[2].ToString()).Tables[0].Rows[0];
    string strText = String.Format("{0,-38}  {1,10}  {2,5}  {3,9:C}  {4,9:C}", dataRow1[0].ToString() + " - " + dataRow1[2].ToString(), dr[3].ToString(), dataRow1[1].ToString(), dr[4], Convert.ToInt16(dr[3]) * Convert.ToInt16(dr[4]));
    // list.Remove(list[m_Line]) <= DO NOT DO THAT!
    e.Graphics.DrawString(strText, m_Font, Brushes.Black, new PointF(e.MarginBounds.Left, yLineTop));
    yLineTop += lineHeight;
  }
  e.HasMorePages = false;
}

You should not be placing printing code like this into a static global class.

This routine belongs in the actual instance of the class that will be using the Graphics object.

private const int PAD = 4;
private int m_Line, m_LinesToPrint;
private Font m_Font;
private PrintDocument m_Doc;

private void print_Click(object sender, EventArgs e) {
  using (var dlg = new PrintPreviewDialog()) {
    if (m_Doc == null) {
      throw new NullReferenceException("Create the document before trying to print it.");
    }
    dlg.Document = m_Doc;
    m_Line = 0;
    m_LinesToPrint = list.Count;
    m_Font = new Font("Courier New", 14, FontStyle.Underline, GraphicsUnit.Point);
    dlg.ShowDialog();
  }
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) {
  float lineHeight = m_Font.GetHeight(e.Graphics) + PAD;
  float yLineTop = e.MarginBounds.Top;
  for ( ; m_Line < m_LinesToPrint; m_Line++) {
    if (e.MarginBounds.Bottom < (yLineTop + lineHeight)) {
      e.HasMorePages = true;
      return;
    }
    DataRow dr = list[m_Line];
    DataRow row1 = poglejBazo("SELECT ime, EM, opis FROM Sifrant WHERE ID = " + dr[2].ToString()).Tables[0].Rows[0];
    string strText = String.Format("{0,-38}  {1,10}  {2,5}  {3,9:C}  {4,9:C}", dataRow1[0].ToString() + " - " + dataRow1[2].ToString(), dr[3].ToString(), dataRow1[1].ToString(), dr[4], Convert.ToInt16(dr[3]) * Convert.ToInt16(dr[4]));
    // list.Remove(list[m_Line]) <= DO NOT DO THAT!
    e.Graphics.DrawString(strText, m_Font, Brushes.Black, new PointF(e.MarginBounds.Left, yLineTop));
    yLineTop += lineHeight;
  }
  e.HasMorePages = false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文