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
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
Is there a way to add page numbers to the output PDF files (exportsettings doesnt seem to cover it)?
thanks
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
Thank you Nikolay, the reference to FieldInfo is undefined, what assembly am I missing:
" FieldInfo field = new FieldInfo(document);"
