WCF/WF - PDF format - Asked By hema latha on 03-Jan-12 12:16 AM

Hi to all....
        I uploaded my gridview data to pdf in asp.net. but i want to format the pdf with colors. Anybody have an idea about this.
    
smr replied to hema latha on 03-Jan-12 12:24 AM
hi

use this

<asp:GridView ID="GridView1" runat="server"

    AutoGenerateColumns = "false" Font-Names = "Arial"

    Font-Size = "11pt" AlternatingRowStyle-BackColor = "#C2D69B" 

    HeaderStyle-BackColor = "green" AllowPaging ="true"  

    OnPageIndexChanging = "OnPaging" >

   <Columns>

    <asp:BoundField ItemStyle-Width = "150px" DataField = "CustomerID"

    HeaderText = "CustomerID" />

    <asp:BoundField ItemStyle-Width = "150px" DataField = "City"

    HeaderText = "City"/>

    <asp:BoundField ItemStyle-Width = "150px" DataField = "Country"

    HeaderText = "Country"/>

    <asp:BoundField ItemStyle-Width = "150px" DataField = "PostalCode"

    HeaderText = "PostalCode"/>

   </Columns>

</asp:GridView>



it applies to pdf also
hema latha replied to smr on 03-Jan-12 01:38 AM
I tried it. It applied to gridview but it didnt applied to pdf document
dipa ahuja replied to hema latha on 03-Jan-12 03:01 AM
This is the simple example
 
Try this way:
 
Download the dll of itextSharp.dll
 
http://sourceforge.net/projects/itextsharp/
 
and try this code
 
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
 
private void btnExport_Click(object sender, EventArgs e)
{
   Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
   PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
 
   doc.Open();//Open Document to write
 
 
   Paragraph paragraph = new Paragraph("data Exported From DataGridview!");
 
   doc.Add(paragraph);
   doc.Add(t1);
   doc.Close(); //Close document
   //
   MessageBox.Show("PDF Created!");
}
 
 
 
 
 
hema latha replied to dipa ahuja on 03-Jan-12 03:24 AM
It creates pdf file. its working fine. but i want colored pdf
dipa ahuja replied to hema latha on 03-Jan-12 03:27 AM
Refer the itextsharp and its different methods and formats.. There are lots of methods and properties to format the pdf

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

Font times new Font(bfTimes, 12, Font.ITALIC, Color.RED);

string path = Server.MapPath("PDFs");

Document doc = new Document();

PdfWriter.GetInstance(doc, new FileStream(path + "/Font.pdf"FileMode.Create));

doc.Open();

doc.Add(new Paragraph("This is a Red Font Test using Times Roman", times));

doc.Close();


hema latha replied to dipa ahuja on 03-Jan-12 03:34 AM
Thanks for ur reply.
    I tried that code. it gives the total pdf in that font. but i want the first row of the table in one color and the remaining in another color.
dipa ahuja replied to hema latha on 03-Jan-12 03:39 AM
kalpana aparnathi replied to hema latha on 03-Jan-12 04:06 AM
hi,
Try this code:
public
void ExportToPdf(DataTable ExDataTable)
  {
    //Here set page size as A4
 
    Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);
 
    try
    {
      PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
      pdfDoc.Open();
 
       //Set Font Properties for PDF File
      Font fnt = FontFactory.GetFont("Times New Roman", 12);
      DataTable dt = ExDataTable;
 
      if (dt != null)
      {
 
        PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
        PdfPCell PdfPCell = null;
 
        //Here we create PDF file tables
 
        for (int rows = 0; rows < dt.Rows.Count; rows++)
        {
          if (rows == 0)
          {
            for (int column = 0; column < dt.Columns.Count; column++)
            {
              PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Columns[column].ColumnName.ToString(), fnt)));
              PdfTable.AddCell(PdfPCell);
            }
          }
          for (int column = 0; column < dt.Columns.Count; column++)
          {
            PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), fnt)));
            PdfTable.AddCell(PdfPCell);
          }
        }
 
        // Finally Add pdf table to the document
        pdfDoc.Add(PdfTable);
      }
 
      pdfDoc.Close();
 
      Response.ContentType = "application/pdf";
 
      //Set default file Name as current datetime
      Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
 
      System.Web.HttpContext.Current.Response.Write(pdfDoc);
 
      Response.Flush();
      Response.End();
 
    }
    catch (Exception ex)
    {
      Response.Write(ex.ToString());
    }
  }