我正在将我的数据集转换为pdf文档。我的数据集包含产品帐单详细信息。因此,在pdf的顶部我需要添加更多内容,例如“我的公司名称和地址客户名称,日期账单,账单号“
我正在使用以下代码转换为pdf。
public static void Exportdata(DataTable dataTable, HttpResponse Response, int val)
{
//String filename = String.Concat(name, "-", DateTime.Today.Day.ToString(), "/", DateTime.Today.Month.ToString(), "/", DateTime.Today.Year.ToString(), ".pdf");
Document pdfDoc = new Document(PageSize.A4, 30, 30, 40, 25);
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, mStream);
//int cols = 0;
//int rows = 0;
int cols = dataTable.Columns.Count;
int rows = dataTable.Rows.Count;
pdfDoc.Open();
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(cols, rows);
pdfTable.BorderWidth = 1;
pdfTable.Width = 100;
pdfTable.Padding = 1;
pdfTable.Spacing = 1;
//creating table headers
for (int i = 0; i < cols; i++)
{
Cell cellCols = new Cell();
Font ColFont = FontFactory.GetFont(FontFactory.HELVETICA, 8, Font.BOLD);
Chunk chunkCols = new Chunk(dataTable.Columns[i].ColumnName, ColFont);
cellCols.Add(chunkCols);
pdfTable.AddCell(cellCols);
}
//creating table data (actual result)
for (int k = 0; k < rows; k++)
{
for (int j = 0; j < cols; j++)
{
Cell cellRows = new Cell();
Font RowFont = FontFactory.GetFont(FontFactory.HELVETICA, 6);
Chunk chunkRows = new Chunk(dataTable.Rows[k][j].ToString(), RowFont);
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
pdfDoc.Add(pdfTable);
pdfDoc.Close();
Response.ContentType = "application/octet-stream";
if (val == 1)
{
Response.AddHeader("Content-Disposition", "attachment; filename=Users.pdf");
}
else if (val == 2)
{
Response.AddHeader("Content-Disposition", "attachment; filename=Customers.pdf");
}
else if (val == 3)
{
Response.AddHeader("Content-Disposition", "attachment; filename=Materials.pdf");
}
else
{
Response.AddHeader("Content-Disposition", "attachment; filename=Reports.pdf");
}
Response.Clear();
Response.BinaryWrite(mStream.ToArray());
//Response.Write(mStream.ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.End();
}