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”

===============>>#1 票数:0 已采纳

我假设您正在使用itextpdf的某些版本来生成PDF。

错误在该行中:

 document.Add(data);

无法将纯.NET对象添加到PDF文档中。 我无法预测您想要实现的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

  ask by nil nae translate from so

未解决问题?本站智能推荐: