I'm want to add plain text and HTML content to a PDF that I'm creating. I was able to add plain text but not sure how to add HTML content. I'm not sure how to use pdfHTML converter to combine htmlString1 and htmlString2 with plain text. Here is my code so far:
public ActionResult CreatePdf()
{
byte[] pdfBytes;
using (var stream = new MemoryStream())
using (var wri = new PdfWriter(stream))
using (var pdf = new PdfDocument(wri))
using (var doc = new Document(pdf))
{
doc.Add(new Paragraph("Working with iText7"));
doc.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
PdfOutline root = pdf.GetOutlines(false);
foreach (var item in col1)
{
doc.Add(new Paragraph(item.Name));
root.AddOutline(item.Name);
doc.Add(new AreaBreak());
}
string htmlString1 = " <!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";
string htmlString2 = "<p>My 2nd paragraph</p><hr><p>My 3rd paragraph</p>";
doc.Close();
doc.Flush();
pdfBytes = stream.ToArray();
}
return File(pdfBytes, "application/pdf", "test.pdf");
}
you can use the
HtmlConverter.ConvertToElementsmethod to convert HTML strings intoiTextelements, and then add these elements to your PDF document