Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have converted pdf using Itextsharp.class successfully but i have one problem that
my converted pdf missing the stylesheet is there any way that i can call or apply css in pdf converstion.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 3:08am    
I did not get it. There is no such thing as PDF stylesheet, or... is it there? :-)
--SA

1 solution

Hi,

See this code if could help...

C#
 public enum ExportTypes
 {
   Excel, PDF, TextFile
 }

 public class ExportManager
 {
   public static bool Downloader(GridViewReport grdReport, ExportTypes type)
   {
      HttpContext.Current.Response.ClearContent();
      StringWriter sw = new StringWriter();
      HtmlTextWriter htw = new HtmlTextWriter(sw);
      string selectedType = string.Empty;
      switch (type)
      {
        case ExportTypes.Excel:
          selectedType = "xls";
          grdReport.ExportType = ExportTypes.Excel;
          grdReport.FileName = grdReport.FileName + ".xls";
          grdReport.RenderControl(htw);
          HttpContext.Current.Response.Write(grdReport.ReportHeader + 
          sw.ToString());
          break;
        case ExportTypes.PDF:
          selectedType = "pdf";
          grdReport.RenderControl(htw);
          HttpContext.Current.Response.ContentType = "application/pdf";
          string fileName2Save = grdReport.FileName.Replace(" ", "").
          Replace("/", "-") + ".pdf";
          HttpContext.Current.Response.AddHeader("content-disposition", 
          "attachment; filename=" + fileName2Save);
          HttpContext.Current.Response.Cache.
          SetCacheability(HttpCacheability.NoCache);
          break;              
      }
      if (selectedType == "pdf")
      {
         HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.
         NoCache);
         StringReader sr = new StringReader(grdReport.
         ReportHeader + "\r\n" + sw.ToString());
         Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 4f);
         HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
         PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream);
         pdfDoc.Open();
         //pdfDoc.HtmlStyleClass = BaseFont.TIMES_ROMAN;  // "clsReportStyle";
         htmlparser.Parse(sr);
         pdfDoc.Close();
         HttpContext.Current.Response.Write(pdfDoc);
         HttpContext.Current.Response.Flush();
         HttpContext.Current.Response.End();
      }
      else
      {
         HttpContext.Current.Response.ContentType = 
         grdReport.ExportType.ToString();
         HttpContext.Current.Response.AddHeader("content-disposition", 
         "attachment;filename=" + grdReport.FileName);
         grdReport.Dispose();
         HttpContext.Current.Response.End();
      }
      return true;
  }
}

public class GridViewReport : GridView
{
   public string ReportHeader { get; set; }
   public string FileName { get; set; }
   public ExportTypes ExportType { get; set; }

   public GridViewReport()
   {
       RowStyle.VerticalAlign = VerticalAlign.Top;
       CssClass = "clsReportStyle";  // Here is the Css were defined...
   }      
}


Please remember to vote if could help so that others may consider as an answer...

Regards,
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900