我从数据库中获取一些值并设置为带标题的itextsharp PdfPCell。这是我的一些代码。
var a = _db.persons.SingleOrDefault(m => m.id == Person.Id);
var table = new PdfPTable(2) {WidthPercentage = 100};
table.DefaultCell.Border = Rectangle.NO_BORDER;
var columns = new[] { 50f, 50f };
table.SetWidths(columns);
var bt1a = new PdfPCell(new Phrase("Name, surname:", fontGeneralText));
var bt2a = new PdfPCell(new Phrase("Personal Id:", fontGeneralText));
var bt3a = new PdfPCell(new Phrase("Phone number:", fontGeneralText));
var bt4a = new PdfPCell(new Phrase("Department:", fontGeneralText));
var bt5a = new PdfPCell(new Phrase("University:", fontGeneralText));
var bt6a = new PdfPCell(new Phrase("Faculty:", fontGeneralText));
var bt1b = new PdfPCell(new Phrase(a.name_surname, fontBoldText));
var bt2b = new PdfPCell(new Phrase(a.personal_id, fontGeneralText));
var bt3b = new PdfPCell(new Phrase(a.phne_number, fontGeneralText));
var bt4b = new PdfPCell(new Phrase(a.department, fontGeneralText));
var bt5b = new PdfPCell(new Phrase(a.university, fontGeneralText));
var bt6b = new PdfPCell(new Phrase(a.faculty, fontGeneralText));
table.AddCell(bt1a);
table.AddCell(bt1b);
table.AddCell(bt2a);
table.AddCell(bt2b);
table.AddCell(bt3a);
table.AddCell(bt3b);
table.AddCell(bt4a);
table.AddCell(bt4b);
table.AddCell(bt5a);
table.AddCell(bt5b);
table.AddCell(bt6a);
table.AddCell(bt6b);
doc.Add(table);
但它是业余编码风格。因为我编写相同的结构并仅更改参数。 是否有任何方法可以使用foreach等自动化方法。
我认为可以通过这种方式完成,但在此之后我不知道:
List<string> titles = new List<string>() {"Name, surname:", "Personal Id:", "Phone number:", "Department:", "University:", "Faculty:"};
List<Phrase> datas = new List<Phrase>() { new Phrase(a.name_surname, fontGeneralText), new Phrase(a.personal_id, fontGeneralText), new Phrase(a.phone_number, fontGeneralText), new Phrase(a.department, fontGeneralText), new Phrase(a.university, fontGeneralText), new Phrase(a.faculty, fontGeneralText)};
foreach (var titles in title)
{
MessageBox.Show(title);
}
答案 0 :(得分:2)
从上面的代码示例中,您已经拥有titles
和datas
。因此,只需创建这两个集合所需的正确字体 - 如下所示:
List<string> titles = new List<string>()
{
"Name, surname:", "Personal Id:", "Phone number:",
"Department:", "University:", "Faculty:"
};
List<string> datas = new List<string>()
{
"0", "1", "2", "3", "4", "5"
};
Font timesBold = FontFactory.GetFont("Times-Roman", 8, Font.BOLD);
Font timesNormal = FontFactory.GetFont("Times-Roman", 8, Font.NORMAL);
然后将这些参数传递给类似于此的方法:
// you should also verify titles and datas have same 'Count'
public void FillTable(List<string> titles, List<string> datas,
Font titleFont, Font dataFont)
{
var table = new PdfPTable(2) { WidthPercentage = 100 };
table.DefaultCell.Border = Rectangle.NO_BORDER;
var columns = new[] { 50f, 50f };
table.SetWidths(columns);
using (FileStream stream = new FileStream(
OUT_FILE,
FileMode.Create,
FileAccess.Write))
{
using (var document = new Document())
{
PdfWriter.GetInstance(document, stream);
document.Open();
for (int i = 0; i < titles.Count; ++i)
{
table.AddCell(new PdfPCell(new Phrase(titles[i], titleFont)));
table.AddCell(new PdfPCell(new Phrase(datas[i], dataFont)));
}
document.Add(table);
}
}
}