虽然我不熟悉您的数据库模式或PDF所需的格式,但我相信重构您的
Main
方法将为每个预订id生成一个离散的PDF。
static void Main(string[] args)
{
string strQuery = "select * from emp_booking where emp_booking_id in (select e.emp_booking_id from emp_booking e, emp_booking_file ef where ef.booking_date>=getdate() and e.emp_booking_id = ef.emp_booking_id)";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
Transpose(dt);
foreach (DataRow r in dt)
{
Document document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("<Unique filename for each booking ID>", FileMode.Create));
document.Open();
iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);
PdfPTable table = new PdfPTable(dt.Columns.Count);
float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f };
table.SetWidths(widths);
table.WidthPercentage = 100;
foreach (DataColumn c in dt.Columns)
{
table.AddCell(new Phrase(c.ColumnName, font5));
}
table.AddCell(new Phrase(r[0].ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
table.AddCell(new Phrase(r[3].ToString(), font5));
table.AddCell(new Phrase(r[4].ToString(), font5));
document.Add(table);
document.Close();
ZipFile.CreateFromDirectory(@"<parent directory of pdf>", @"M://BookingSheetUpload/Zip/<unique zip name>.zip");
}
}
这将遍历每个
DataRow
由查询返回,并为每行生成一个PDF。请注意,每个PDF和.zip文件都应该指定一个唯一的名称。
我也可能建议将这些代码中的一些移到其他类中,或者至少是类程序的静态方法中。再说一次,我对你的申请不熟悉,但看起来
主要
方法是在这里做很多繁重的工作。