So let's see how to use Microsoft.Reporting.WebForms.
After referencing to Microsoft.Reporting.WebForms from your web form, you will be able to use ReportViewer control. As my project needs to render the report dynamically, I chose LocalReport object to create a report instance.
My colleague, Lukasz, has worked on crystal-report-alike projects and he helped me alot in creating the dynamic report.
What needs to be displayed on the page are label showing "part 1" or "part2" and Jpeg image.
In order to design for the dedicated report, I created report file, Report.rdlc. Then on the report designer view, you will see the report page and Website Data Sources view as well as Toolbox.
I need a resource object for report called as ReportDataSource. In that source, what I need is to have label and image. So, dataset is created by clicking Add New DataSource in Website Data Sources view. You can rename your DataSet by right clicking on the name in code behind and choose "Refactoring name" and also renaming in the Properties window.
I dragged and dropped the DataTable control named tblReport from the Toolbox onto DataSet.xsd. Then right click on the DataTable and chose "Add" and then "Column". I created 2 columns as dcLabel and dcImage. Changed the DataType of the dcImage to System.Byte[].
Then I went back to Report.rdlc and drag-and-dropped the Table onto the report page. The table will have 2 Rows (for label and image) and 1 Column. I dragged dcLabel from DataSet >> tblReport object into Row[0] Column[0] and dcImage into Row[1] Column[0]: they appeared as "=Fields!dcLabel.Value" and "=Fields!dcImage.Value". In image's properties window, set MIMEType as image/jpeg and Source as Database. Image's sizing property was set as FitProportional.
Report's layout can be adjusted by going to VS2008's Report menu, and, Table's layout can be adjusted by selecting the whole table and rightclicking to choose properties. Normal A4 page size is 21cm and 29.7cm. So you can adjust the page layout not more than the standard size and the contents within the boundaries.
Then in the [WebMethod] of the web service, report is dynamically created using LocalReport.
DataTable to add into the datasources of the LocalReport can be created by declaring an object as the name you give to the DataSet.axd. Then by using .Newxxx(), you can create a new data row and can add specific data into the columns of that row.
e.g.
LocalReport report = new LocalReport();
report.ReportEmbeddedResource = "{WebService project name}.{Report name}.rdlc;
report.DataSources.Add(new ReportDataSource({DataSet name}, {DataSet's DataTable}));
report.EnableExternalImages = true;
report.Refresh();
In report form's [Table]'s prperties, {General >> Dataset name:} should be the same as code behind's datasource's [ReportDataSource]'s name.
Rendering to PDF could be done as in the following example:
string mimeType;
string encoding;
string fileNameExtension;
string[] streams;
Microsoft.Reporting.WebForms.Warning[] warnings;
byte[] pdfContent = report.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
Page number can be added in the footer section by creating a text box and adding the following line in the text box:
=Globals.PageNumber & " of " & Globals.TotalPages
After you have converted your byte array to file using filestream, you can then check your file to see if it renders as it's supposed to be.
You can optimise the size and quality of the image file: http://www.glennjones.net/Post/799/Highqualitydynamicallyresizedimageswithnet.htm
No comments:
Post a Comment