I got itextsharp version 4.1.6.0 and got it to work like this
for parsing html not in a file...:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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 itexttest : 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();
//html -text - kan be from database or editor too
String htmlText="<font " +
" color=\"#0000FF\"><b><i>Title One</i></b></font><font " +
" color=\"black\"><br><br>Some text here<br><br><br><font " +
" color=\"#0000FF\"><b><i>Another title here " +
" </i></b></font><font " +
" color=\"black\"><br><br>Text1<br>Text2<br><OL><LI>hi</LI><LI>how are u</LI></OL>";
//make an arraylist ....with STRINGREADER since its no IO reading file...
ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; 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();
.netdevelop
Member
20 Points
10 Posts
Re: Convert html to pdf using iTextSharp
Apr 29, 2010 12:15 PM|LINK
I got itextsharp version 4.1.6.0 and got it to work like this
for parsing html not in a file...:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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 itexttest : 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();
//html -text - kan be from database or editor too
String htmlText="<font " +
" color=\"#0000FF\"><b><i>Title One</i></b></font><font " +
" color=\"black\"><br><br>Some text here<br><br><br><font " +
" color=\"#0000FF\"><b><i>Another title here " +
" </i></b></font><font " +
" color=\"black\"><br><br>Text1<br>Text2<br><OL><LI>hi</LI><LI>how are u</LI></OL>";
//make an arraylist ....with STRINGREADER since its no IO reading file...
ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; 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) {
Console.Error.WriteLine(exx.StackTrace);
Console.Error.WriteLine(exx.Message);
}
}
}
good luck....