ali raza

C#, .Net ,Asp.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 http://sourceforge.net/projects/itextsharp/

 

<%@ 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

document.Open();

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) {

Console.Error.WriteLine(ex.StackTrace);

Console.Error.WriteLine(ex.Message);

}

// step 5: Close document

document.Close();

// step 6: Write pdf bytes to outputstream

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

Response.OutputStream.Flush();

Response.OutputStream.Close();

%>

 

 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: