Saturday, January 22, 2011

Display Html in RDLC file

I want to display HTML in RDLC of reporting service. Report Viewer support html but with limited tags, I want to display HTML generated from rich text editor. After investigation I found the best solution is displaying it as image. Steps to do that:

1. Create class library convert HTML to image.
2. Use this class with report viewer.
3. Display report with asp net.

1. Create class library convert HTML to image
a. In the following url http://webapplication02.blogspot.com/2011/01/convert-html-to-image.html you find the way to create class library to convert html to image.
It is better to create image as bmp for better image quality but this will not work with excel.
You can convert image to another type but check its quality.
b. Need to add [assembly: AllowPartiallyTrustedCallers()] to trust class library for using by report viewer.

2. Use Class with report viewer:
a. Replace your html field in report with Image with properties:
i. Mime type image/bmp
ii. Type of field Database
iii. Field Expression =Code.GetHtmlImage(HtmlField)

b. Add this function to report:


Public Function GetHtmlImage(ByVal body as String) As Byte()
return htmlConv.getWebPageBytes(body, nothing, nothing)
End Function


c. Add Assembly reference to 3 dlls:
i. System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
ii. System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
iii. Your class library
d. Add class library ImageConv class and instance htmlConv
e. To build your solution with report reference to your class library copy your class library to folder:
($Program Files Path)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies

3. Display Report with ASPNET:


ReportViewer1.LocalReport.ExecuteReportInCurrentAppDomain(System.Reflection.Assembly.GetExecutingAssembly().Evidence);
ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
ReportViewer1.LocalReport.AddTrustedCodeModuleInCurrentAppDomain(Full Assembly name of your class libarary);

Also need to add Full Trust in Web.Config

6 comments:

Unknown said...

Hi Dear,

I am implementing your code into my project for HTML conversion to image and then using it in report.

I am confused on few items.

Can you please elaborate the point

iii. Field Expression =Code.GetHtmlImage(HtmlField)

because I have added this code in the field expression of image box. But it was giving error on GetHtmlImage, I also added

Public Function GetHtmlImage(ByVal body as String) As Byte()
return htmlConv.getWebPageBytes(body, nothing, nothing)
End Function

to the report properties in the code tab.

I've also added the references to respective dlls but I m not sure where to create the instance of my image library class htmlConv, wether it is to be created in the aspx code file where I am using the report or it is to be created in the report code.

Please elaborate it a little bit more, waiting for your reply.

Unknown said...

Hi Dear Tarek Yehia,

This post has been very much helpful to me and Although I've converted the HTML into image and placed it on the report but there are two questions

1. Image is very much blur, can we control width, height, font name, font size
2. If there is an image in the HTML, then its not being displayed in the generated image, how can we do that?

Tarek Yehia Abd El Khalek said...

You can control width ,height, and quality by using Bitmap resizing function. but font must be controled from html.

Image will not appear if it took too long to load use small image or increase delay before capture html

Anonymous said...

I want to add image in textbox's placeholder.

In textbox data is coming from the custom code as below

Public Function GetDataYesNo(ByVal value As String) As String
Dim str As String
str = "ul li"
Dim val As String
Dim i As Integer
For i = 0 To 2
str = str & "span class='inner-label'div class='radio' span input type='radio' style='opacity: 0;'span div"
If (i = 0) Then
If (value = "Y") Then
val = "b font color='blue'" & "Yes" & "font b"
Else
val = "Yes"
End If
ElseIf (i = 1) Then
If (value = "N") Then
val = "b font color='blue'" & "No" & "font b"
Else
val = "No"
End If
Else
If (value = "X") Then
val = "bfont color='blue'" & "None" & "font b"
Else
val = "None"
End If
End If
str = str & val
str = str & "span"
Next
str = str & "li ul"
'str ="a href=C:\inetpub\wwwroot\Filesanywhere\Images\Flags\in.gif'" & "li input type='radio' style='opacity: 0;' >Male 'input type='radio' style='opacity: 0;'>Female <input type='radio' style='opacity: 0;'Child li"
Return str
End Function

I want to add the a image file in between text. As Textbox is supporting the font,li and other tags.

Can i use the img or a tag to show the image.

Thanks

Piyush

Tarek Yehia Abd El Khalek said...

To use image you need to convert html to image like post by converting html to image.

You can use Text Place holder if you only use limited html.

xname44 said...

can u upload an example it will be more helpful for me