无法使用iTextSharp在文档对象中添加列表

时间:2016-05-24 18:01:19

标签: asp.net-mvc

 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 :(得分: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

可以找到很多有用的样本      https://msdn.microsoft.com/en-us/library/ms188029(v=sql.120).aspx

itextpdf的基础教程: http://developers.itextpdf.com/examples