Monday, May 10, 2010

Create pdf using Open source in c#

Used the iTextSharp.dll for createing the pdf.

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections;
using System.Text;
using iTextSharp.text.xml;
using iTextSharp.text.html;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//create document
Document document = new Document();
try
{
//writer - have our own path!!! and see you have write permissions...

PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~") + "/WordDoc/" + "parsetest.pdf", FileMode.Create));
document.Open();


String htmlText = "" color=\"#0000FF\">Title One
" color=\"black\">

Some text here


" color=\"#0000FF\">Another title here " +
"
" color=\"black\">

Text1
Text2
  1. hi
  2. how are u
";



ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

//add the collection to the document
for (int k = 0; k <>
{
document.Add((IElement)htmlarraylist[k]);
}

document.Add(new Paragraph("And the same with indentation...."));


// or add the collection to an paragraph
// if you add it to an existing non emtpy paragraph it will insert it from
//the point youwrite -
Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
mypara.IndentationLeft = 36;
mypara.InsertRange(0, htmlarraylist);
document.Add(mypara);
document.Close();




}
catch (Exception exx)
{
Response.Write(exx.StackTrace);
Response.Write(exx.Message);
}
}
}

Use the rest Webservice

using System.Text;
using System.Net;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSend_Click(object sender, EventArgs e)
{
string parameter="a/b";
Uri address = new Uri("website Address" + parameter);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
//Response.Write(reader.ReadLine());
lblMessage.Text = reader.ReadLine();
lblMessage.Visible = true;
}
}
}