C# – Insert HTML directly in PDF using itextsharp

citextsharp

I have WPF application in which user enters some text in rich text box(rtb), I convert that rtb string to HTML and then convert that HTML to image and then insert it in the PDF document

using (Stream inputPdfStream = new FileStream("sample.pdf", FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream outputPdfStream = new FileStream("result2.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
  var reader = new PdfReader(inputPdfStream);
  var stamper = new PdfStamper(reader, outputPdfStream);

  PdfContentByte pdfContentByte = null;

  int c = reader.NumberOfPages;
  iTextSharp.text.Image image =   TextSharp.text.Image.GetInstance(ConvertXamltohtmltoImage(xamlstring));

  foreach (var item in lst)
  {
      image.ScaleToFit(item._Size.Width, item._Size.Height);
      image.SetAbsolutePosition(item.Location.X, item.Location.Y);
      pdfContentByte = stamper.GetOverContent(item.pageNo);
      pdfContentByte.AddImage(image);
  }
  stamper.Close();
}

My question is can I insert HTML directly into PDF?

Best Answer

You need an extra DLL to do that: http://sourceforge.net/projects/itextsharp/files/xmlworker/ See the demo: http://demo.itextsupport.com/xmlworker/ Unfortunately, the documentation hasn't been updated recently. We're working on it.