About Me

My photo
Software Developer and ideas man!

Wednesday, January 5, 2011

Crystal Reports - How to insert a logo into the Report Header at runtime

Now how hard can it be to insert a picture into the report header of a Crystal Report at runtime - apparently this is not a straightforward exercise and I implore the folks at SAP to look at this as an area for improvement.

Anyway,  I wanted the users of our reports to be able to customize the business logo they used on the reports before they generate the report - I thought this would be easily done by passing the image as a report parameter in some way, but I thought wrong.  After much searching around and reading copious amounts of posts from other's many of which led to the backstreets of nowhere, I developed the following 8 easy steps.


1. Create a dataset,

2. Create a table in that dataset,

3. Add a column to the table that will store the image

4. Define an xml schema for that table

5. Read in the image as a byte array, to the dataset

6. Set the dataset as the datasource for the report.

7. Add in the logo->image field to the Crystal Report

8. Create the dataset connection in the Crystal Report Database Expert based on the xsd created in the code below [check out the last line] and insert logo blob field on report.


Below is the C# code developed using Visual Studio 2010 and SAP Crystal Reports for Visual Studio 2010:

private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ReportDS = new DataSet();

            //Create a "Logo" Table and insert an image column
            DataTable logoTable = new DataTable("Logo");
            logoTable.Columns.Add(new DataColumn("Image", typeof(System.Byte[])));

            //Add table to the dataset to be associated with the report
            ReportDS.Tables.Add(logoTable);

            //Add a row to our new table
            DataRow logoDR = ReportDS.Tables["Logo"].NewRow();

            //Only required to generate a matching xsd for use by the Crystal Report Field Explorer!
            ReportDS.WriteXmlSchema(@"c:\temp\test.xsd");

            //Read in the image file to a filestream
            FileStream fileStream = new FileStream(@"c:\temp\test.bmp", FileMode.Open);

            BinaryReader binaryReader = new BinaryReader(fileStream);
            logoDR["Image"] = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);

            ReportDS.Tables["Logo"].Rows.Add(logoDR);

            fileStream.Close();
            binaryReader.Close();

            this.CrystalReport11.SetDataSource(ReportDS);
        }




The Test.xsd generated by the .WriteXmlschema() above should look like this - 


Now it is time to place the Logo Blob field in the Report Header.
i.) From the Field Explorer->Database Fields->Database Expert, create a new ADO.NET(xml) connection. 
 
ii). From the Database Expert-> Available Datasources, select the newly created datasource and move the table over to the "Selected Tables" listbox.


iii). Under the "Database Fields" in the "Field Explorer" should be the new table called "logo"plus a field called "image" which can be dragged into the Report Header.


...And that's it...


More from this Blogger:

Oh Ma – why is CrystalReportViewer not visible in my Visual Studio 2010 ToolBox?

I thought I knew a thing or 2 about Crystal Reports but this little problem took me back to basics.
I thought I’ll just create a new C# app. in Visual Studio 2010 using Crystal Reports targeted to this version.  Simple right.  Yeah maybe.

Anyway, I created a new Winforms app, a new blank Crystal Report and defined a dataset for it.
I was then intending to put a Crystal Report Viewer on the form, so I went to the toolbox expecting to see “Reporting->CrystalReportViewer” and nothing! Doh!  I had a Microsoft Viewer but no CrystalReportViewer.  Hmm - What to do – what to do....


OK – lets build and see if we have some unresolved references.  Yep.  Sure do.  It was coming back with  the following.  The target platform was .Net Framework 4.0!

Ok – so by retargeting my project to .Net 3.5 resolved this problem and I was happily able to continue on my development path.  There are other alternatives but this worked for me and hopefully will work for others caught out by this.

P.S. And if you really want to target to .Net 4, check out this link for further info [ I came across this during my research]
   
http://forums.sdn.sap.com/thread.jspa?threadID=1802638



More from this Blogger:
Localizing Crystal Reports in C# - I demonstrate one way of doing it despite the hurdles!