I've got a two part question about using iTextSharp. I've built a simple MVC application to store information about "lessons learned" in a SQL Server database. When a user is looking at the details for a lesson I want them to be able to save the lesson as a PDF. I've got some working code but 1) I'm not sure how sound my approach is and 2) the MVC application uses a TinyMCE rich text editor and when I put the rich text into the PDF the html tags are being displayed. How do I get the PDF to honor the html formatting (bold fonts, unordered lists, paragraphs, etc.)?
Below is the code I'm using to generate the PDF. I would really appreciate feedback if I'm going about this incorrectly.
Thanks.
public FilePathResult GetPDF(int id)
{
Lesson lesson = lessonRepository.GetLesson(id);
string pdf = @"C:\Projects\Forms\LessonsLearned\PDF\template_test.pdf";
string outputFilePath = @"C:\Temp\pdf_output\test_template_filled.pdf";
PdfReader pdfReader = null;
try
{
pdfReader = new PdfReader(pdf);
using (FileStream pdfOutputFile = new FileStream(outputFilePath, FileMode.Create))
{
PdfStamper pdfStamper = null;
try
{
pdfStamper = new PdfStamper(pdfReader, pdfOutputFile);
AcroFields acroFields = pdfStamper.AcroFields;
acroFields.SetField("title", lesson.Title);
acroFields.SetField("owner", lesson.Staff.FullName);
acroFields.SetField("date", lesson.DateEntered.ToShortDateString());
// field with rich text
acroFields.SetField("situation", Server.HtmlDecode(lesson.Situation));
acroFields.SetField("description", Server.HtmlDecode(lesson.Description));
pdfStamper.FormFlattening = true;
}
finally
{
if (pdfStamper != null)
{
pdfStamper.Close();
}
}
}
}
finally
{
pdfReader.Close();
}
return File(outputFilePath, "application/pdf", "Lesson_" + lesson.ID + ".pdf");
}