ali raza

C#, .Net , and my thoughts

I used the free dll which writes pdf files as simple as we us system IO to simple files . I write the the page with inline scripting text and placed the following code . I fetch data from database to write to pdf file. i  remove html tags from data because data save in database is comes form htm text editor. You can download iTextSharp dll form here


<%@ Page language=”c#” %>

<%@ Import Namespace=”System.IO” %>

<%@ Import Namespace=”iTextSharp.text” %>

<%@ Import Namespace=”iTextSharp.text.pdf” %>

<%@ Import Namespace=”System.Data” %>

<%@ Import Namespace=”iTextSharp.text.pdf” %>


// step 1

// need to write to memory first due to IE wanting

// to know the length of the pdf beforehand

MemoryStream m = new MemoryStream();

Document document = new Document();

try {

// step 2: we set the ContentType and create an instance of the Writer

Response.ContentType = “application/pdf”;

PdfWriter.GetInstance(document, m);

string _Details = “”;

// step 3


if (Request.QueryString[“Id”] !=null)


// Fetching Data from database based on Query string

DataTable _Dt = myAppplication.Documents.DocumentsById(Convert.ToInt64(Request.QueryString[“Id”]));

// Removing Html tags using Regular expressions on required filed

_Details = Regex.Replace(_Dt.Rows[0][“Details”].ToString(), @”<(.|\n)*?>”, string.Empty);



// step 4

document.Add(new Paragraph(_Details));

} catch (DocumentException ex) {




// step 5: Close document


// step 6: Write pdf bytes to outputstream

Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);






Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: