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

Import HTML then Export to PDf

6 Answers 107 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
david
Top achievements
Rank 1
david asked on 26 Apr 2016, 10:16 PM

I'm sure this is possible but I cant quite get the provider usage figured out. I have a string  of HTML I want to output as a PDF . This does not work, what am i doing wrong?

    protected void test(string htmlString)

    {
        HtmlFormatProvider htmlDoc = new HtmlFormatProvider();
        htmlDoc.Import(htmlString);
        byte[] renderedBytes = null;

        IFormatProvider<RadFlowDocument> formatProvider = new PdfFormatProvider();
        using (MemoryStream ms = new MemoryStream())
        {
            formatProvider.Export(htmlDoc, ms); // this is invalid
            renderedBytes = ms.ToArray();
        }
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment; filename=ExportedFile" + ".pdf");
        Response.ContentType = "application/pdf";
        Response.BinaryWrite(renderedBytes);
        Response.End();

6 Answers, 1 is accepted

Sort by
0
Nikolay Demirev
Telerik team
answered on 28 Apr 2016, 07:27 AM
Hello David,

Try the following code snippet, it should work:
protected void test(string htmlString)
{
    HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();
    RadFlowDocument htmlDocument = htmlFormatProvider.Import(htmlString);
    byte[] renderedBytes = null;
 
    IFormatProvider<RadFlowDocument> formatProvider = new PdfFormatProvider();
    using (MemoryStream ms = new MemoryStream())
    {
        formatProvider.Export(htmlDocument, ms); // this is invalid
        renderedBytes = ms.ToArray();
    }
    Response.Clear();
    Response.AppendHeader("Content-Disposition", "attachment; filename=ExportedFile" + ".pdf");
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(renderedBytes);
    Response.End();
}

Hope this helps.

Regards,
Nikolay Demirev
Telerik
0
david
Top achievements
Rank 1
answered on 29 Apr 2016, 01:20 AM
excellent, thank you!
0
david
Top achievements
Rank 1
answered on 07 Sep 2016, 02:13 PM

Is there a way to add page numbers to the output PDF files (exportsettings doesnt seem to cover it)?

 

thanks

 

0
Nikolay Demirev
Telerik team
answered on 09 Sep 2016, 10:46 AM
Hello ,

You could use the following code snippet just before exporting the RadFlowDocument to PDF:
Section section = this.document.Sections.First();
Footer footer = section.Footers.Add(HeaderFooterType.Default);
Paragraph paragraph = footer.Blocks.AddParagraph();
FieldInfo field = new FieldInfo(document);
paragraph.Inlines.Add(field.Start);
paragraph.Inlines.AddRun("Page");
paragraph.Inlines.Add(field.Separator);
paragraph.Inlines.AddRun("0");
paragraph.Inlines.Add(field.End);
document.UpdateFields();

It will insert Page field in the footer of the each page.

Regards,
Nikolay Demirev
Telerik by Progress
0
david
Top achievements
Rank 1
answered on 09 Sep 2016, 11:50 AM

Thank you Nikolay, the reference to FieldInfo is undefined, what assembly am I missing:

"  FieldInfo field = new FieldInfo(document);"

0
Nikolay Demirev
Telerik team
answered on 13 Sep 2016, 10:09 AM
Hi David,

The class is in Telerik.Windows.Documents.Flow assembly in Telerik.Windows.Documents.Flow.Model.Fields namespace.

I hope this helps.

Regards,
Nikolay Demirev
Telerik by Progress
Tags
WordsProcessing
Asked by
david
Top achievements
Rank 1
Answers by
Nikolay Demirev
Telerik team
david
Top achievements
Rank 1
Share this question
or