Tuesday, August 16, 2011

aspnet c# generate .pdf

this article helped me, hope it helps you too!
http://www.4guysfromrolla.com/articles/030911-1.aspx

I need to parse HTML into the pdf, the implementation is:

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Transactions.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Document document = new Document(PageSize.A1, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(document, Response.OutputStream);

// Open the Document for writing
document.Open();

string documentContent = string.Empty;
document.NewPage();
documentContent = "Confirmation" + "
" + body;
StringReader sr = new StringReader(documentContent);
foreach (object item in HTMLWorker.ParseToList(sr, null).ToList())
{
document.Add((IElement)item);
}

document.Close();

Response.Write(document);
Response.End();

No comments: