Click here to Skip to main content
15,891,248 members

Comments by Mausam Bharati (Top 6 by date)

Mausam Bharati 9-Jul-13 7:57am View    
There is no error.A blank pdf page is generated.


protected void txtPDF_Click(object sender, EventArgs e)
{

PDFReport pdfReport = new PDFReport();
pdfReport.ReportProperty.ReportTable = "Bill";

pdfReport.ReportProperty.Columns = new System.Collections.Generic.Dictionary<string, string="">();

pdfReport.ReportProperty.Columns.Add("Emp Code", "Employee Code");
pdfReport.ReportProperty.Columns.Add("Status", "Employee Status");
pdfReport.ReportProperty.Columns.Add("Bill Type", "Employee BillType");
pdfReport.ReportProperty.Columns.Add("Financial Yr", "Financial Year");
pdfReport.ReportProperty.Columns.Add("Bill Date", "Bill Date From");
pdfReport.ReportProperty.Columns.Add("Approve Date", "Bill Date To");
Literal1.Text = pdfReport.HTMLToPdf(pdfReport.ReportProperty, "C:\\temp.pdf",Response.OutputStream);


}


this is my on button click event.
Mausam Bharati 9-Jul-13 7:51am View    
Excuse any error..I am a beginner. :(
Mausam Bharati 9-Jul-13 7:50am View    
This code is a function of a dll.The string htmlread2 is called from the other function.
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;




namespace ReportLib
{
public class PDFReport
{
public struct _TableProperty
{
public string ReportTable { get; set; }
public Dictionary<string, string=""> Columns;
public string ReportCondition { get; set; }
}

public _TableProperty ReportProperty = new _TableProperty();



public string getReportHTML(_TableProperty tableProperty, Stream stream)
{
string sql = "select ";
string columnAdd="<table><tr>";

foreach (var column in tableProperty.Columns)//emp-Employee,b-2,c-3
{

sql += "["+column.Key+"]" + " as [" + column.Value + "],";
columnAdd += "<th> "+column.Value+" </th>";

}
columnAdd += "</tr>";
sql=sql.TrimEnd(',');
sql += " from " + tableProperty.ReportTable;
sql = sql + " where 1=1 " + (string.IsNullOrEmpty(tableProperty.ReportCondition) ? "" : "and " + tableProperty.ReportCondition);

SqlConnection _con = new SqlConnection(@"server=mausam-pc\sqlexpress;uid=sa;pwd=ngc;initial catalog=HRMSN");
SqlCommand _cmd = new SqlCommand(sql, _con);
_con.Open();
SqlDataReader _reader = _cmd.ExecuteReader();


while (_reader.Read())
{
columnAdd += "<tr>";
foreach (var column in tableProperty.Columns)
{
columnAdd += "<td>" + _reader[_reader.GetOrdinal(column.Value)] + "</td>";
//columnAdd += _reader.GetOrdinal(column.Value);
}
columnAdd += "</tr>";
}
columnAdd += "</table></tr>";
string htmlread = "<html><title>General</title><body>"+ columnAdd +"</body></html>";

if (_reader != null)
{
_reader.Close();
}
_con.Close();
return htmlread;

}

public string HTMLToPdf(_TableProperty ReportProperty, string FilePath,Stream stream)
{
string htmlread2 = getReportHTML(ReportProperty, stream);
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\\myPDF.pdf", FileMode.Create));
document.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath("pdfic.jpg"));

pdfImage.ScaleToFit(100, 50);

pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);

document.Add(pdfImage);
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.XHTMLWorker hw = new iTextSharp.text.html.simpleparser.XHTMLWorker(document);
hw.Parse(new StringReader(htmlread2));
document.Close();
ShowPdf("myPDF.pdf");
return htmlread2;
}

private void ShowPdf(string s)
{
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + s);
Mausam Bharati 9-Jul-13 7:48am View    
What will be Chap0101.pdf in case of table from database generated through a dll?
Mausam Bharati 9-Jul-13 7:47am View    
http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp

I have taken this code as reference.