Pushing HTML Into RadFlowDocument Header and Footer

2 posts, 0 answers
  1. Milton
    Milton avatar
    6 posts
    Member since:
    Feb 2017

    Posted 07 Dec 2018 Link to this post

         Hi,

    I'm trying to use the RadFlowDocument in order to create a PDF document. I have successfully managed to use the examples provided in the documentation in order to import the body of my HTML into the document, however the examples for the header and footer do not make it clear how I would go about importing HTML in.

    Below is the current state of my code. Unfortunately the header comes out as plain text, but the body is successfully processed. Any advise would be greatly received. 

     

    public static byte[] GeneratePDF(DocumentTemplate doc)
            {            
                string body = "";
                body = doc.DocumentBody;

                HtmlFormatProvider provider = new HtmlFormatProvider();
                RadFlowDocument rfd = provider.Import(body);

                rfd.Sections.AddSection();

                Header defaultHeader = rfd.Sections.First().Headers.Add();
                Paragraph defaultHeaderParagraph = defaultHeader.Blocks.AddParagraph();
                defaultHeaderParagraph.Inlines.AddRun(doc.DocumentHeader);

                PdfFormatProvider provider2 = new PdfFormatProvider();
                
                using (MemoryStream output = new MemoryStream())
                {
                    provider2.Export(rfd, output);
                    return ReadToEnd(output);
                }
            }

  2. Georgi
    Admin
    Georgi avatar
    32 posts

    Posted 12 Dec 2018 Link to this post

    Hello Milton,

    Thank you for your interest in RadWordsProcessing.

    If I am understanding the scenario correctly you would like to import HTML content into a Header of a Section, am I right? Headers and Footers are block containers and you can add content by inserting a Table or Paragraph instances to the Blocks property. Using the HtmlFormatProvider, you can import RadFlowDocument from HTML format which will create all the necessary document elements. However, parsing the HTML content to a Header or Footer elements directly is not supported out of the box from the provider. As possible solution you can (1) import the HTML content to a RadFlowDocument, (2) clone each of the imported block elements and (3) add the cloned instances to the Header. For example:
    HtmlFormatProvider provider = new HtmlFormatProvider();
    RadFlowDocument document = provider.Import(documentHtml);
     
    Header defaultHeader = document.Sections.First().Headers.Add();
    RadFlowDocument tempDocument = provider.Import(headerHtml);
     
    foreach (var block in tempDocument.Sections.First().Blocks)
    {
        if (block is Paragraph paragraph)
        {
            Paragraph clonedParagraph = paragraph.Clone(document);
            defaultHeader.Blocks.Add(clonedParagraph);
        }
        else if (block is Table table)
        {
            Table clonedTable = table.Clone(document);
            defaultHeader.Blocks.Add(clonedTable);
        }
    }

    I hope this helps.

    Regards,
    Georgi
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top