I have to get an ordered list into pdf.The data stored is in html format.When exporting to pdf using itextsharp,the ol-li tags should be replaced by an ordered list.
Asked
Active
Viewed 3,217 times
0
-
Deleted my answer as I seem to have misunderstood the question. Could you add som more details on what you are doing here? – alun Aug 02 '11 at 06:29
-
Data from the editor is stored in html format in the database.The same editor format must be retained while exporting to pdf rather than just stripping the html tags.I cant get the ordered list into my pdf from the html. – asdam Aug 02 '11 at 06:39
1 Answers
1
You'll want to use iTextSharp's iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList()
method. Below is a full working sample WinForms app targeting iTextSharp 5.1.1.0 that does what you're looking for. See the inline comments for what's going on.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//File to export to
string exportFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "HTML.pdf");
//Create our PDF document
using (Document doc = new Document(PageSize.LETTER)){
using (FileStream fs = new FileStream(exportFile, FileMode.Create, FileAccess.Write, FileShare.Read)){
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)){
//Open the doc for writing
doc.Open();
//Insert a page
doc.NewPage();
//This is our sample HTML
String HTML = "<ol><li>Row 1</li><li>Row 2</li></ol>";
//Create a StringReader to parse our text
using (StringReader sr = new StringReader(HTML))
{
//Pass our StringReader into iTextSharp's HTML parser, get back a list of iTextSharp elements
List<IElement> ies = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);
//Loop through each element and add to the document
foreach (IElement ie in ies)
{
doc.Add(ie);
}
}
//Close our document
doc.Close();
}
}
}
}
}
}

Chris Haas
- 53,986
- 12
- 141
- 274
-
Hello Chris, I have also the same problem to print ordered list in pdf using itext sharp. Here is my question link - http://stackoverflow.com/questions/32243210/itext-shatp-nested-ul-li-in-pdfpcell-not-printing-correctly-using-c-sharp?noredirect=1 . Please help me. Thanks. – Herin Aug 31 '15 at 12:06