Import HTML then Export to PDf

7 posts, 0 answers
  1. david
    david avatar
    210 posts
    Member since:
    May 2007

    Posted 26 Apr Link to this post

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

  2. Nikolay Demirev
    Admin
    Nikolay Demirev avatar
    103 posts

    Posted 28 Apr Link to this post

    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
  3. david
    david avatar
    210 posts
    Member since:
    May 2007

    Posted 28 Apr in reply to Nikolay Demirev Link to this post

    excellent, thank you!
  4. david
    david avatar
    210 posts
    Member since:
    May 2007

    Posted 07 Sep in reply to david Link to this post

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

     

    thanks

     

  5. Nikolay Demirev
    Admin
    Nikolay Demirev avatar
    103 posts

    Posted 09 Sep Link to this post

    Hello Davud,

    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
  6. david
    david avatar
    210 posts
    Member since:
    May 2007

    Posted 09 Sep in reply to Nikolay Demirev Link to this post

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

    "  FieldInfo field = new FieldInfo(document);"

  7. Nikolay Demirev
    Admin
    Nikolay Demirev avatar
    103 posts

    Posted 13 Sep Link to this post

    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
Back to Top