Click here to Skip to main content
15,894,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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 + "C:\\HRMS OSN\\latest\\HRMS\\admpanel\\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("C:\\HRMS OSN\\latest\\HRMS\\admpanel\\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);
           System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
           System.Web.HttpContext.Current.Response.WriteFile(s);
           System.Web.HttpContext.Current.Response.Flush();
           System.Web.HttpContext.Current.Response.Clear();
       }
Posted

1 solution

try this...:)


Generate PDF Using C#[^]
 
Share this answer
 
Comments
Mausam Bharati 9-Jul-13 7:45am    
I am sorry but this ain't my problem. The issue is that the pdf page gets generated but no data gets displayed over it. Perhaps it is not reading the table from the database.
Mausam Bharati 9-Jul-13 7:47am    
http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp

I have taken this code as reference.
Nirav Prabtani 9-Jul-13 7:49am    
where did you provide data to make PDF????
Mausam Bharati 9-Jul-13 7:50am    
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:51am    
Excuse any error..I am a beginner. :(

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