2012-02-19 187 views
0

Question打印多个页面不起作用

这是回答真的很快到来,我已经在升级问题绊倒了。

我已经改变了我的程序来从数据库中填充一些DataSet。

我打电话Print()printDocument,一切正常,它只是不希望我的注册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; 
      } 
     } 

    } 

所以,当我试图打印文档用单页面,一切正常,但如果我尝试打印多页文档,只打印第一页(Header,DocumentInfo,SupplierInfo,ProductLines(80条左右的38行),Footer),然后再没有更多页面正在打印成PDF文件..)

我在做什么错?

PrintProductLines中的e参数有问题吗?我怎样才能告诉功能PrintProductLines,我想从原来的功能触发HasMorePages?我知道我可以通过引用传递,但裁判关键字不会在我的情况下工作:S

编辑:

更改static void printDocument_PrintProductLines(ref PrintPageEventArgs e)printDocument_PrintProductLines(ref e);抛出一个错误:

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

+0

您必须在PrintPage事件处理程序中将e.HasMorePages设置为true才能获取多个页面。 – 2012-02-19 16:24:24

+0

我已将整个PrintArticleLines(e)过程移回到PrintPage函数中,并且仍然不起作用。我现在正在设置HasMorePages,它仍然不起作用:( – SubjectX 2012-02-19 19:33:46

回答

2

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

此例程属于将使用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; 
} 
+0

谢谢jp2code。我一直在努力打印多个页面,我一直在盯着你的代码,想知道为什么for循环没有重置为“0 “在e.HasMorePages = true;&”return“之后,我终于看到你做了什么 - 你从for循环中除去了int m_Line = 0,即为(; m_Line 2014-10-21 15:29:17

+0

我希望我没有给你带来太多的悲伤,鲍勃,我把'm_Line'全局化,以便在调用print方法之后,我可以读取我的变量并确保打印所有页面。 – jp2code 2014-10-21 19:17:19