-6

I am generating a PDF from data that is not HTML based, and I have HTML that data that I want to insert part way through creating the PDF i.e.

[Non HTML]
[HTML]

[Non HTML]
[HTML]

....

public class CreatePDF 
{
    private Document _document;

    private const int FontSizeHeading2 = 12;
    
    private void RegisterFonts()
    {
        // register non standard fonts....
    }

    public MemoryStream GeneratePDF(DataSet Data)
    {
        using (MemoryStream buffer = new MemoryStream())
        {
            using (PdfDocument document = new PdfDocument(new PdfWriter(buffer)))
            {
                RegisterFonts();
                foreach (DataRow row in Data.Tables[0])
                {
                    Text text = new Text(Text).SetFont(_fontRegular).SetFontSize(FontSizeHeading2);
        Paragraph title = new Paragraph(row.Field<string>("Heading"));

                    /* Insert HTML content here - note this is just what 
                       can be rendered, I am not looking for a faithful rendered. I just want the content displayed */
            }
        }
    }

}

I am using iText to generate the PDF so using the pdfHTML plugin seemed a logical choice. However, pdfHTML closes the document/stream. The only way round it (as far as I can ascertain) is to create a PDF, hold it in memory and then add merge the two PDFs. Is there a way that I can add the HTML content to the PDF without having to merge the documents?

3
  • 4
    You may consider using iText layout module for generating [Non HTML Text] and then it's very straightforward to embed [HTML] part with HtmlConverter.convertToElements from pdfHtml. Commented Oct 29 at 5:43
  • Not quite sure what you mean. However, I will edit my question to further clarify Commented Oct 29 at 12:01
  • @vitalipr can you add that as the answer? Thanks Commented Oct 31 at 22:52

1 Answer 1

2

Please, check this sample on how to use pdfHtml ConvertToElements, this is exactly what you are asking for. https://github.com/itext/itext-publications-samples-dotnet/blob/develop/itext/itext.publications/itext.publications.htmlsamples/itext/samples/htmlsamples/chapter07/C07E02_CombineHtml2.cs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.