How Add HTML and plain text content to new PDF using itext7

91 Views Asked by At

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");
}
2

There are 2 best solutions below

1
Jeevan ebi On BEST ANSWER

you can use the HtmlConverter.ConvertToElements method to convert HTML strings into iText elements, and then add these elements to your PDF document

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));

    
        foreach (var item in col1)
        {
            doc.Add(new Paragraph(item.Name));
            PdfOutline root = pdf.GetOutlines(false);
            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>";
        List<IElement> elements1 = HtmlConverter.ConvertToElements(htmlString1);
        foreach (var element in elements1)
        {
            doc.Add(element);
        }

        string htmlString2 = "<p>My 2nd paragraph</p><hr><p>My 3rd paragraph</p>";
        List<IElement> elements2 = HtmlConverter.ConvertToElements(htmlString2);
        foreach (var element in elements2)
        {
            doc.Add(element);
        }

        doc.Close();
        doc.Flush();
        pdfBytes = stream.ToArray();
    }

    return File(pdfBytes, "application/pdf", "test.pdf");
}
1
citm09 On
    string htmlString1 = "<!DOCTYPE html><html><body><h1>My First Heading</ h1><p>My first paragraph.</p></body></html>";
    List<IElement> elements1 = (List<IElement>)HtmlConverter.ConvertToElements(htmlString1);
    foreach (var element in elements1)
    {
      doc.Add((IBlockElement)element);
    }

    string htmlString2 = "<p>My 2nd paragraph</p><hr><p>My 3rd paragraph</p>";
    List<IElement> elements2 = (List<IElement>)HtmlConverter.ConvertToElements(htmlString2);
    foreach (var element in elements2)
    {
       doc.Add((IBlockElement)element);
    }