Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / Visual Basic
Article

Method for Printing an OWC11 Chart from VB.NET

Rate me:
Please Sign up or sign in to vote.
3.75/5 (3 votes)
4 Sep 2008CPOL2 min read 42.6K   825   12   1
Printing an OWC chart from a Windows.Form in VB.NET
Image 1

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:

  1. get the byte array from ChartSpace.GetPicture()
  2. load an Image object from a resultant binary stream
  3. 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:

VB
' 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.

VB
' 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:

VB
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.

VB
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:

VB
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Kamonth20-Jan-13 20:56
Kamonth20-Jan-13 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.