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);
}
}
}

No comments:

Post a Comment