Reputation: 42
I am trying to convert HTML content to PDF using itextSharp in .net application using c#. While doing so m gettting my content trucated after '<' symbol. For conversion I am using following line:
HTMLWorker.ParseToList(new StringReader(htmlContent, null);
This is the code snippet with need to add referece 'itextsharp.dll' reference
Following is Code Snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf.draw;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
CreatePDF();
}
static void CreatePDF()
{
string fileName = string.Empty;
DateTime fileCreationDatetime = DateTime.Now;
fileName = string.Format("{0}.pdf", fileCreationDatetime.ToString(@"yyyyMMdd") + "_New" + fileCreationDatetime.ToString(@"HHmmss"));
string pdfPath = "D:\\" + fileName;
using (FileStream msReport = new FileStream(pdfPath, FileMode.Create))
{
//step 1
using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 140f, 10f))
{
try
{
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport);
//pdfWriter.PageEvent = new ConsoleApplication1.ITextEvents();
//open the stream
pdfDoc.Open();
{
Paragraph para = new Paragraph("Hello world. Checking Header Footer", new Font(Font.FontFamily.HELVETICA, 16));
para.Alignment = Element.ALIGN_CENTER;
pdfDoc.NewPage();
string str = "<b>Try<</b>";
StringReader TheStream = new StringReader(str.ToString());
List<IElement> htmlElementsh = HTMLWorker.ParseToList(TheStream, null);
IElement htmlElementh = (IElement)htmlElementsh[0];
pdfDoc.Add((Paragraph)htmlElementh);
}
pdfDoc.Close();
}
catch (Exception ex)
{
//handle exception
}
finally
{
}
}
}
}
}
}
Upvotes: 0
Views: 1476