2011-08-05 42 views
1

我這樣做是爲了在運行時生成pdf,當我點擊打印按鈕時 當我點擊打印按鈕時,pdf與文件夾一起生成,這個過程就像這樣。單擊打印按鈕時系統崩潰

對於我做這樣的:

private void btnPrint_Click(object sender, EventArgs e) 
{ 

    private const string PAYMENT_PATH = @"c:\xxxxx\xxxxx\paymentType_{0}"; 

    btnPrint.Visible = true; 
    btnPrint.Enabled = true; 

    string pay = cbpaymenttype.Text; 
    string dds = cbddprovider.Text; 

    string path = String.Format(PAYMENT_PATH, DateTime.Now.ToString("ddMMyyyyHHmm")); 

    //List<paymenttypeprint> paymenttype = new List<paymenttypeprint>(); 

    List<printfunctions> printfunction = new List<printfunctions>(); 
    foreach (ListViewItem item in lstviewcashmembers.Items) 
    { 

     printfunctions allpayments = new printfunctions(); 
     allpayments.member_Lastname = item.SubItems[1].Text; 
     allpayments.member_Firstname = item.SubItems[2].Text; 
     allpayments.Postcode = item.SubItems[3].Text; 
     allpayments.ddReference = item.SubItems[4].Text; 
     allpayments.ddprovider = item.SubItems[5].Text; 
     allpayments.Monthlyamount = Convert.ToDecimal(item.SubItems[6].Text); 
     allpayments.MembershipType = item.SubItems[7].Text; 
     allpayments.Status = item.SubItems[8].Text; 
     allpayments.Enddate = Convert.ToDateTime(item.SubItems[9].Text); 
     allpayments.paymentmethods = item.SubItems[10].Text; 
     printfunction.Add(allpayments); 
    } 

    new printFunction(printfunction, new Company(mf.gBaseUrl).GetSingleLineCompanyDetails(), path, pay, dds); 

    System.Diagnostics.Process.Start("explorer.exe", path); 
} 

我已經創建的類printfunctions這樣的:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 
using PdfSharp; 
using PdfSharp.Drawing; 
using PdfSharp.Pdf; 
using PdfSharp.Drawing.Layout; 
using MigraDoc.DocumentObjectModel; 
using MigraDoc.Rendering; 
using MigraDoc.DocumentObjectModel.Shapes; 
using MigraDoc.DocumentObjectModel.Tables; 
using MigraDoc.DocumentObjectModel.Fields; 
using System.IO; 

namespace classes 
{ 
    public class printfunctions 
    { 
     public string member_Lastname; 
     public string member_Firstname; 
     public string Postcode; 
     public string ddReference; 
     public string ddprovider; 
     public decimal Monthlyamount; 
     public string MembershipType; 
     public string Status; 
     public DateTime Enddate; 
     public string paymentmethods; 
    } 

    public class printFunction 
    { 
     private Document _document; 
     private Table _table; 
     private List<printfunctions> _array; 
     private string _address; 
     private string _paymethod; 
     private string _ddproviders; 

     public printFunction(List<printfunctions> array, string address, string exportpath, string paymentmethod, string ddproviders) 
     { 
      _array = array; 
      _address = address; 
      _paymethod = paymentmethod; 
      _ddproviders = ddproviders; 

      Document ddreportdoc = CreateDocument(); 
      PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true); 

      pdfRenderer.Document = ddreportdoc; 

      if (!Directory.Exists(exportpath)) 
       Directory.CreateDirectory(exportpath); 

      pdfRenderer.RenderDocument(); 

      if (paymentmethod != "All") 
      { 
       pdfRenderer.Save(String.Format(@"{0}\{1} MEMBERS REPORT.pdf", exportpath, _paymethod)); 
      } 
      else if (ddproviders != "") 
      { 
       pdfRenderer.Save(String.Format(@"{0}\{1} MEMBERS REPORT.pdf", exportpath, _ddproviders)); 
      } 
     } 

     public Document CreateDocument() 
     { 

      _document = new Document(); 

      if (_ddproviders != "") 
      { 
       _document.Info.Title = string.Format("{0}", _ddproviders); 
      } 
      else if (_paymethod != "All") 
      { 

       _document.Info.Title = string.Format("{0}", _paymethod); 
      } 
      _document.Info.Author = "xxxxxxx"; 

      DefineStyles(); 
      CreatePage(); 
      FillContent(); 

      return _document; 
     } 

     private void DefineStyles() 
     { 
      Style style = _document.Styles["Normal"]; 
      style.Font.Name = "Verdana"; 

      style = _document.Styles[StyleNames.Header]; 
      style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Right); 

      style = _document.Styles[StyleNames.Footer]; 
      style.ParagraphFormat.AddTabStop("7cm", TabAlignment.Center); 

      // Create a new style called Table based on style Normal 
      style = _document.Styles.AddStyle("Table", "Normal"); 
      style.Font.Name = "Verdana"; 
      style.Font.Size = 6; 

      // Create a new style called Reference based on style Normal 
      style = _document.Styles.AddStyle("Reference", "Normal"); 
      style.ParagraphFormat.SpaceBefore = "6mm"; 
      style.ParagraphFormat.SpaceAfter = "6mm"; 
      style.ParagraphFormat.TabStops.AddTabStop("6cm", TabAlignment.Right); 

     } 

     private void CreatePage() 
     { 
      Section section = _document.AddSection(); 

      // Create footer 
      Paragraph paragraph = section.Footers.Primary.AddParagraph(); 
      paragraph.AddText(_address); 
      paragraph.Format.Font.Size = 8; 
      paragraph.Format.Alignment = ParagraphAlignment.Center; 

      paragraph = section.AddParagraph(); 
      paragraph.Format.SpaceBefore = "1.3cm"; 
      paragraph.Style = "Reference"; 
      if (_paymethod == "All") 
      { 
       paragraph.AddFormattedText(string.Format("{0} MEMBERS MONTHLY REPORT", _paymethod), TextFormat.Bold); 
      } 
      else if (_ddproviders == "All providers") 
      { 

       paragraph.AddFormattedText(string.Format("{0} MEMBERS MONTHLY REPORT", _ddproviders), TextFormat.Bold); 
      } 
      paragraph.AddTab(); 
      paragraph.AddDateField("dd MMM yyyy"); 

      _table = section.AddTable(); 
      _table.Style = "Table"; 
      _table.Borders.Color = new Color(0, 0, 0); 

      //Defining the columns 

      Column column = _table.AddColumn("1.7cm"); //Lastname 
      column.Format.Alignment = ParagraphAlignment.Center; 

      column = _table.AddColumn("1.7cm"); // first name 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // postcode 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // DD ref 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); //DD provider Name 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // Monthly Amount 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // mship type 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // status 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // Expiry date 
      column.Format.Alignment = ParagraphAlignment.Left; 

      column = _table.AddColumn("1.7cm"); // payment type 
      column.Format.Alignment = ParagraphAlignment.Right; 

      // Create the header of the _table 
      Row row = _table.AddRow(); 
      row.HeadingFormat = row.Format.Font.Bold = true; 
      row.Format.Alignment = ParagraphAlignment.Center; 

      row.Cells[0].AddParagraph("Last Name"); 
      row.Cells[0].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[1].AddParagraph("First Name"); 
      row.Cells[1].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[2].AddParagraph("Post Code"); 
      row.Cells[2].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[3].AddParagraph("DD Reference"); 
      row.Cells[3].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[4].AddParagraph("DDProvider Name"); 
      row.Cells[4].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[5].AddParagraph("Monthly Amount"); 
      row.Cells[5].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[6].AddParagraph("Memebrship Type"); 
      row.Cells[6].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[7].AddParagraph("Status"); 
      row.Cells[7].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[8].AddParagraph("ExpiryDate"); 
      row.Cells[8].Format.Alignment = ParagraphAlignment.Left; 

      row.Cells[9].AddParagraph("payment type"); 
      row.Cells[9].Format.Alignment = ParagraphAlignment.Left; 
     } 

     private void FillContent() 
     { 
      foreach (var item in _array) 
      { 
       // Each item fills two rows 
       Row row1 = _table.AddRow(); 
       row1.TopPadding = 1.0; 
       Paragraph paragraph; 

       row1.Cells[0].VerticalAlignment = VerticalAlignment.Center; 
       row1.Cells[0].Format.Alignment = ParagraphAlignment.Left; 
       row1.Cells[0].AddParagraph(item.member_Lastname); 

       row1.Cells[1].Format.Alignment = ParagraphAlignment.Right; 
       paragraph = row1.Cells[1].AddParagraph(item.member_Firstname); 

       row1.Cells[2].Format.Alignment = ParagraphAlignment.Left; 
       paragraph = row1.Cells[2].AddParagraph(item.Postcode); 

       row1.Cells[3].Format.Alignment = ParagraphAlignment.Right; 
       paragraph = row1.Cells[3].AddParagraph(item.ddReference); 

       row1.Cells[4].Format.Alignment = ParagraphAlignment.Left; 
       paragraph = row1.Cells[4].AddParagraph(item.ddprovider); 

       row1.Cells[5].Format.Alignment = ParagraphAlignment.Right; 
       paragraph = row1.Cells[5].AddParagraph(item.Monthlyamount.ToString("F2")); 

       row1.Cells[6].Format.Alignment = ParagraphAlignment.Left; 
       paragraph = row1.Cells[6].AddParagraph(item.MembershipType); 

       row1.Cells[7].Format.Alignment = ParagraphAlignment.Right; 
       paragraph = row1.Cells[7].AddParagraph(item.Status); 

       row1.Cells[8].Format.Alignment = ParagraphAlignment.Left; 
       paragraph = row1.Cells[8].AddParagraph(item.Enddate.ToString("dd MMM yyyy")); 

       row1.Cells[9].Format.Alignment = ParagraphAlignment.Right; 
       paragraph = row1.Cells[9].AddParagraph(item.paymentmethods); 
      } 
     } 
    } 
} 

其工作正常時,ListView lstviewcashmembers具有高達100或300行,但當lstviewcash成員有1600行時,它不工作。

系統崩潰,在這條線:

pdfRenderer.RenderDocument(); 

執行不進入下一行,並沒有表現出任何異常還當我點擊打印按鈕,系統剛剛崩潰,如果ListView有1600行。

在運行時使用MigraDoc和PDFsharp庫生成PDF的代碼中是否有任何問題?

有沒有人請在此幫助.....

非常感謝........

MODIFIED : It will take four minutes for generating pdf so how should I reduce this time 

將在此

非常感謝任何一項幫助

+1

轉到Debug \ Exceptions併爲引發和用戶未處理啓用公共語言運行時異常。再次運行,看看你是否能得到關於你的異常的更多信息。 – Adi

+0

@Adi謝謝它顯示了所有不必要的異常,當我想要捕捉此行的異常時,我必須執行的操作「pdfRenderer.RenderDocument();」你會幫我 – user682417

+0

它可能是一個內存問題? 1600行顯着高於100-300。我不知道你的'printfunctions'類佔用了多少空間,但這可能是一個看起來很不錯的地方。 – Tim

回答

0

在VS中啓用異常處理,如here,您將得到確切的異常說明。看看你的問題描述聽起來像這可能是行數量限制。順便檢查一下鏈接。

問候。

+0

嗨它顯示了所有不必要的例外,我什麼必須要做的時候,我想趕上這條線的例外「pdfRenderer.RenderDocument();」你會幫我嗎 – user682417

+2

我不認爲你有任何例外情況,這些都很重要。你需要的是通過他們,讓你的PRINT點擊,以獲得你要搜索的那個。忽略其他例外情況,立即着手解決您的CURRENT問題。 – Tigran

+0

我檢查了所有這些,但我沒有拋出任何異常... – user682417

0

摘自http://pdfsharp.codeplex.com/releases/view/37054

如果你偏愛速度:留在PDFsharp 1.30。如果您喜歡小型PDF 文件:請使用PDFsharp 1.31。未來版本的PDFsharp將支持 兩種模式(有利於速度和偏好大小)。

也許這會有所幫助。

+0

此聲明涉及圖像處理。關於表格渲染,在1.30,1.31和1.32之間應該沒有速度差異。 –