Hi all,
i have developed a webpage which allow the user to enter the values just like a registration form.while saving the data i need to give an option for downloading the page as pdf format(which i have entered values).
what i did?
* i tried using itextsharp dll. the problem is i don't get all the content in the pdf files. example images and styles are not converted properly.
* i tried some third party tools like( SautinSoft's PdfVision,ConvertApi,pdfcrowd)assemblies. all the dll are getting the url as a parameter and converting to pdf. in that way the server side html code is converted into pdf.the values i have entered is not in the pdf.
sample code i got from net for (sautinsoft).
SautinSoft.PdfVision v = new SautinSoft.PdfVision();
v. PageStyle.PageSize.Letter();
byte[] pdf = v.ConvertHtmlFileToPDFStream(@"http://en.wikipedia.org/wiki/Main_Page");
if (pdf != null)
{
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/PDF";
Response.AddHeader("Content-Disposition:", "attachment; filename=Result.pdf");
Response.BinaryWrite(pdf);
Response.Flush();
Response.End();
}
for (itextsharp)
protected override void Render(HtmlTextWriter writer)
{
MemoryStream mem = new MemoryStream();
StreamWriter twr = new StreamWriter(mem);
HtmlTextWriter myWriter = new HtmlTextWriter(twr);
base.Render(myWriter);
myWriter.Flush();
myWriter.Dispose();
StreamReader strmRdr = new StreamReader(mem);
strmRdr.BaseStream.Position = 0;
string pageContent = strmRdr.ReadToEnd();
strmRdr.Dispose();
mem.Dispose();
writer.Write(pageContent);
CreatePDFDocument(pageContent);
}
public void CreatePDFDocument(string strHtml)
{
string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
// step 1: creation of a document-object
Document document = new Document();
// step 2:
// we create a writer that listens to the document
PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));
StringReader se = new StringReader(strHtml);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
document.Close();
ShowPdf(strFileName);
}
public void ShowPdf(string strFileName)
{
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
Response.ContentType = "application/pdf";
Response.WriteFile(strFileName);
Response.Flush();
Response.Clear();
}
note: i have taken code from several forums.
please let me know if u have better solution.
thanks.
dev