有什么简单的方法可以在 itextsharp 中创建 table 元素
Is there any easy way to create table elements in itextsharp
我从数据库中获取一些值并设置到带有标题的 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);
}
从上面的代码示例中,您已经有 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);
}
}
}
我从数据库中获取一些值并设置到带有标题的 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);
}
从上面的代码示例中,您已经有 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);
}
}
}