2016-05-24 20 views
0
public ActionResult PartTimeFacultyCourseLoadReport() 
    { 
     var teacherStatistics = (from t in db.Teachers 
           join c in db.Courses 
            on t.Id equals c.TeacherId into cGroup 
           where t.Status == "Part Time" 
           orderby t.Designation descending 
           select new 
           { 
            TeacherInfo = t, 
            CourseInfo = from cg in cGroup 
                orderby cg.Code ascending 
                select cg 
           }).ToList(); 

     List<TeacherStatistics> teacherStatisticses = new List<TeacherStatistics>(); 

     int count = 0; 
     foreach (var teacherStatistic in teacherStatistics) 
     { 
      TeacherStatistics aTeacherStatistics = new TeacherStatistics(); 
      aTeacherStatistics.Name = teacherStatistic.TeacherInfo.Name; 
      aTeacherStatistics.Designation = teacherStatistic.TeacherInfo.Designation; 
      aTeacherStatistics.NumberOfCourse = teacherStatistic.TeacherInfo.NumberOfCourse; 
      count = 0; 
      foreach (var courseInfo in teacherStatistic.CourseInfo) 
      { 
       if (count != 0) 
       { 
        aTeacherStatistics.Courses += ", "; 
       } 

       aTeacherStatistics.Courses += courseInfo.Code; 
       aTeacherStatistics.Courses += "("; 
       aTeacherStatistics.Courses += courseInfo.Section; 
       aTeacherStatistics.Courses += ")"; 
       count++; 
      } 
      teacherStatisticses.Add(aTeacherStatistics); 
     } 
     var document = new Document(PageSize.A4, 50, 50, 25, 25); 
     var output = new MemoryStream(); 
     var writer = PdfWriter.GetInstance(document, output); 
     document.Open(); 
     var data = teacherStatisticses.ToList(); 
     document.Add(data); 
     Response.ContentType = "application/pdf"; 
     Response.AddHeader("content-disposition", "attachment; filename=PartTimeFaculty.pdf"); 
     Response.BinaryWrite(output.ToArray()); 
     document.Close(); 
     return View(teacherStatisticses); 
    } 

我想通過一個名爲'teacherStatisticses'的列表通過文檔對象進行製作PDF。我的代碼不起作用。它告訴我以下錯誤 - 參數1:無法從'System.Collections.Generic.List'轉換爲'iTextSharp.text.IElement'無法在使用iTextSharp的文檔對象中添加列表

回答

0

我假設您正在使用某種版本的itextpdf生成PDF。

的錯誤是在該行:

document.Add(data); 

有沒有辦法爲純.NET對象添加到PDF文檔。我不能預測哪些you`d想實現PDF的結構,但上面提到的代碼可以寫成:

foreach(var teacher in teacherStatistics) 
    { 
     var paragraph = new Paragraph(teacher.ToString()); // instead of teacher.ToString() should be some code which translates teacherStatistics projection to the string representation 
     document.Add(paragraph); 
    } 
    //not tested 

了很多有益的樣品可以在 http://developers.itextpdf.com/examples

基礎教程中找到爲itextpdf: http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-1-introducing-basic-building-blocks

相關問題