Method for Printing an OWC11 Chart from VB.NET
Printing an OWC chart from a Windows.Form in VB.NET

Introduction
This article provides information on how to print an Office Web Component (v11) chart from the .NET Windows.Forms environment. There is another excellent article available relating to this subject which is definitely worth the read at Create Column Charts Using OWC11.
Background
I have always wanted to contribute to this forum and after spending the better part of the day working this out I saw it as a subject that was worthwhile. Printing OWC objects from Windows.Forms is not strikingly obvious (at least not to me!). There are examples using OWC from a web server and after some digging around you'll see examples using ChartSpace.GetPicture()
to extract a binary stream in order to capture the chart you wish to print. Within a web environment you would then use the result of this binary stream in a line, something like: Response.BinaryWrite Chartspace1.GetPicture(...)
.
So, what of a solution for Windows.Forms! I've only been fiddling with OWC for a couple of days so the method I'm describing here might not be the best, however, it does seem to work quite well.
Using the Code
The result I ended on can be outlined as follows:
- get the byte array from
ChartSpace.GetPicture()
- load an Image object from a resultant binary stream
- use a
Drawing.Printing.PrintDocument
to print the image
Of course you will need to have Office Web Components 11 installed. This is available on the Microsoft Website or from the Microsoft Office 2003 Installation Kit. Note, there are licensing issues relating to this install.
Office 2003 Web Components in .NET does not provide event handling by default however this can be overcome by following the instructions in this link: HOW TO: Handle Events for the Office 2003 Web Components in Visual Studio.NET.
Sample Code
On a Windows.Form draw out a AxChartSpace
object. To this form also add a System.Drawing.Image
object at the module level, a System.Drawing.Printing.PrintDocument
object, and lastly a Print button (of course!).
The sample code begins with creating the actual chart that will be printed. There are a number of articles around describing how to do this so I won't dwell on this subject.
So, under the Print button we declare a couple of variables:
' variable declaration
Dim ba() As Byte
Dim ms As IO.MemoryStream
We then extract the binary stream (byte array) from our AxChartSpace
object and load it up as a MemoryStream
.
' note, see OWC11 documentation for further information on PrintQuality3D property
AxChartSpace1.PrintQuality3D = 1.0#
' you can make the chart whatever width/height you want but for
' simplicity I'll leave it as what the form size is
ba = CType(AxChartSpace1.GetPicture("gif", _
AxChartSpace1.Width, _
AxChartSpace1.Height), _
Byte())
ms = New IO.MemoryStream(ba)
The next step is to load the stream into our System.Drawing.Image
object, as follows:
m_imgChart = Image.FromStream(ms, False, True)
To finish up on the print button code we use the System.Drawing.Printing.PrintDocument
object to print the System.Drawing.Image
object. There's obviously quite a number of ways the printing can occur, but this is for another article.
printdocChart.DocumentName = ""My OWC Chart""
printdocChart.DefaultPageSettings.Landscape = True
'
printdocChart.Print()
m_imgChart.Dispose()
We also need to set the System.Drawing.Image
object to the the print document. This is done within the System.Drawing.Printing.PrintDocument.PrintPage
event, as follows:
Private Sub printdocChart_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
Handles printdocChart.PrintPage
e.Graphics.DrawImage(m_imgChart, 30, 30, m_imgChart.Width, m_imgChart.Height)
e.HasMorePages = False
End Sub