This is a migrated thread and some comments may be shown as answers.

Pushing HTML Into RadFlowDocument Header and Footer

1 Answer 821 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Milton
Top achievements
Rank 1
Milton asked on 07 Dec 2018, 09:26 AM

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

1 Answer, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 12 Dec 2018, 07:09 AM
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.
Tags
PdfProcessing
Asked by
Milton
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Share this question
or