views:

67

answers:

1

I am converting my data set into a pdf document.My data set contains the product bill details.So,at the top of the pdf i need to added some more content like "my company name & address customer name, date of bill,bill no"

Below code i am using to convert into 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();
}
+1  A: 

Do you want to add a header using iTextSharp?

In this case extend PdfPageEventHelper and use the OnStartPage event.

Here's an example .

Jay Riggs