Itext Ilist 转 pdf

发布于 2024-12-21 17:05:15 字数 899 浏览 1 评论 0原文

我在c#中有一个IList,我想通过IText将其放入PDF中。有什么办法可以做到这一点吗?我已经找了一段时间了。

我试图做的是:

s = BLLstudent.selectStudentById(Convert.ToInt16(Request.QueryString["s"]));
var data = BLLevk.selectEvkDetailsVanStudent(s.pk_studentID);

Document mySavedPDF = new Document();

FileStream fs = new FileStream(@"C:\Users\Toon\Documents\Visual Studio 2010\WebSites\LilyNoone-LessLes-503729a\prints\" + s.studentNaam + "_" + s.studentVoornaam + ".pdf", FileMode.Create);
PdfWriter.GetInstance(mySavedPDF, fs);
mySavedPDF.Open();
mySavedPDF.Add(data);
mySavedPDF.CloseDocument();

但这说

错误 2 参数 1:无法从“System.Collections.Generic.IList”转换为“System.IO.TextReader”C:\Users\Toon\Documents\Visual Studio 2010\WebSites\evk-applicatie-181211\web \admin\a_overzicht_student.aspx.cs 95 77 C:...\evk-applicatie-181211\

有没有办法直接插入列表?

提前谢谢

I have an IList in c#, and I want to put it in PDF through IText. Is there any way to do this? I have been searching for it for a while now.

What i tried to do was:

s = BLLstudent.selectStudentById(Convert.ToInt16(Request.QueryString["s"]));
var data = BLLevk.selectEvkDetailsVanStudent(s.pk_studentID);

Document mySavedPDF = new Document();

FileStream fs = new FileStream(@"C:\Users\Toon\Documents\Visual Studio 2010\WebSites\LilyNoone-LessLes-503729a\prints\" + s.studentNaam + "_" + s.studentVoornaam + ".pdf", FileMode.Create);
PdfWriter.GetInstance(mySavedPDF, fs);
mySavedPDF.Open();
mySavedPDF.Add(data);
mySavedPDF.CloseDocument();

But this said

Error 2 Argument 1: cannot convert from 'System.Collections.Generic.IList' to 'System.IO.TextReader' C:\Users\Toon\Documents\Visual Studio 2010\WebSites\evk-applicatie-181211\web\admin\a_overzicht_student.aspx.cs 95 77 C:...\evk-applicatie-181211\

Is there any way to insert the list directly?

Thx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨光如昨 2024-12-28 17:05:15

不,无法直接将通用IList直接添加到Document对象。如果您查看 Document.Add 方法,唯一有效的参数是 元素object - 这就是抛出 Exception 的原因。如果您考虑一下,尝试将通用 IList 添加到 PDF 将非常困难 - 至少您必须考虑 IList 元素 type 以及如何格式化每个成员PDF 中的属性(在使用 Reflection 确定类型和成员之后)。

所以你有几个选择。

  • 将 IList 转换为简单的 XML 表示形式,然后将其发送到 XMLWorker 或 < a href="http://api.itextpdf.com/itext/com/itextpdf/text/html/simpleparser/HTMLWorker.html" rel="nofollow">HTMLWorker。
  • 编写您自己的包装器或代码来显示您的 IList。

第二个选择还不错,您可以完全控制如何展示您的收藏。这是一个简单的例子:

Response.ContentType = "application/pdf";
IList<Student> students = Student.GetStudents();
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(
    document, Response.OutputStream
  );
  document.Open();
  foreach (Student s in students) {
    document.Add(new Paragraph(string.Format(
      "[{0:D8}] - {1}, {2}. MAJOR: {3}",
      s.Id, s.NameLast, s.NameFirst, s.Major
    )));
    List list = new List(List.ORDERED);
    foreach (string c in s.Classes) {
      list.Add(new ListItem(c));
    }
    document.Add(list);
  }
}

有一个像这样的简单类:

public class Student {
  public string NameLast, NameFirst, Major;
  public int Id;
  public string[] Classes;
  public static IList<Student> GetStudents() {
    string[] majors = {"Math", "Engineering", "CS"};
    List<Student> l = new List<Student>();
    for (int i = 0; i < majors.Length;) {
      l.Add(new Student() {
        Major = majors[i], 
        Id = ++i, NameLast = string.Format("LastNameStudent{0}", i),
        NameFirst = string.Format("FirstnameStudent{0}", i),
        Classes = new string[] {"Calc I", "Physics II", "Databases"}
      });
    }
    return l;
  }
}

No, there's no way to directly add a generic IList to the Document object directly. If you take a look at the Document.Add method, the only valid parameter is an Element object - that's why the Exception is thrown. If you think about it, trying to add a generic IList to a PDF would be very difficult - at the minimum you would have to take into consideration both the IList elements type, and also how to format each member property (after you determine both type and members using Reflection) in the PDF.

So you have a couple of choices.

  • Convert your IList to a simple XML representation, then send it to a XMLWorker or HTMLWorker.
  • Write your own wrapper or code to display your IList.

The second choice isn't so bad, and you have complete control of how to display your collection. Here's a simple example:

Response.ContentType = "application/pdf";
IList<Student> students = Student.GetStudents();
using (Document document = new Document()) {
  PdfWriter writer = PdfWriter.GetInstance(
    document, Response.OutputStream
  );
  document.Open();
  foreach (Student s in students) {
    document.Add(new Paragraph(string.Format(
      "[{0:D8}] - {1}, {2}. MAJOR: {3}",
      s.Id, s.NameLast, s.NameFirst, s.Major
    )));
    List list = new List(List.ORDERED);
    foreach (string c in s.Classes) {
      list.Add(new ListItem(c));
    }
    document.Add(list);
  }
}

With a simple class like this:

public class Student {
  public string NameLast, NameFirst, Major;
  public int Id;
  public string[] Classes;
  public static IList<Student> GetStudents() {
    string[] majors = {"Math", "Engineering", "CS"};
    List<Student> l = new List<Student>();
    for (int i = 0; i < majors.Length;) {
      l.Add(new Student() {
        Major = majors[i], 
        Id = ++i, NameLast = string.Format("LastNameStudent{0}", i),
        NameFirst = string.Format("FirstnameStudent{0}", i),
        Classes = new string[] {"Calc I", "Physics II", "Databases"}
      });
    }
    return l;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文